How to resolve the algorithm Klarner-Rado sequence step by step in the Julia programming language
How to resolve the algorithm Klarner-Rado sequence step by step in the Julia programming language
Table of Contents
Problem Statement
Klarner-Rado sequences are a class of similar sequences that were studied by the mathematicians David Klarner and Richard Rado. The most well known is defined as the thinnest strictly ascending sequence K which starts 1, then, for each element n, it will also contain somewhere in the sequence, 2 × n + 1 and 3 × n + 1.
So, the sequence K starts with 1. Set n equal to the first element 1; the sequence will also contain 2 × n + 1 and 3 × n + 1, or 3 and 4. Set n equal to the next element: 3, somewhere in the sequence it will contain 2 × n + 1 and 3 × n + 1, or 7 and 10. Continue setting n equal to each element in turn to add to the sequence.
Preferably without needing to find an over abundance and sorting.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Klarner-Rado sequence step by step in the Julia programming language
The provided code in Julia programming language implements the Klarner-Rado function, which generates a sequence of integers with certain properties. Here's a detailed explanation of the code:
-
KlarnerRado Function:
- The
KlarnerRado
function takes a positive integerN
as its input. - It initializes a list
K
with a single element,[1]
. - It iterates through the numbers from 1 to
N
:- For each
i
, it setsj
to thei
-th element of the listK
. - It computes two numbers:
firstadd
(2j + 1
) andsecondadd
(3j + 1
). - It checks if
firstadd
is less than the last element ofK
. If it is, it finds the first positionpos
inK
wherefirstadd
is less than the element at that position. - It checks if the element at
pos
is not equal tofirstadd
. If it's not, it insertsfirstadd
intoK
at positionpos
. - It repeats the above steps for
secondadd
.
- For each
- Finally, it returns the list
K
.
- The
-
Generating the First 1 Million Klarner-Rado Numbers:
- The code generates the first 1 million Klarner-Rado numbers by calling
KlarnerRado(1_000_000)
. It stores the result in the variablekr1m
. - It prints the first 100 Klarner-Rado numbers, formatted with right padding of 4 characters. It prints a newline after every 20 numbers.
- It also prints the 1000th, 10000th, 100000th, and 1000000th Klarner-Rado numbers, formatted with commas for readability.
- The code generates the first 1 million Klarner-Rado numbers by calling
-
KlamerRado Function:
- The
KlamerRado
function is another implementation of the Klarner-Rado function. - It initializes a boolean array
kr
of size 100 *N
. It sets the first element ofkr
to true. - It iterates through the numbers from 1 to 30*
N
. For eachi
, ifkr[i]
is true, it setskr[2i + 1]
andkr[3i + 1]
to true. - Finally, it returns a list of integers corresponding to the indices of the true elements in the
kr
array.
- The
-
Generating the First 1 Million Klarner-Rado Numbers Using
KlamerRado
:- The code generates the first 1 million Klarner-Rado numbers using the
KlamerRado(1000000)
function and stores the result inkr1m
. - It prints the first 100 Klarner-Rado numbers and the 1000th, 10000th, 100000th, and 1000000th Klarner-Rado numbers, similar to the previous approach.
- The code generates the first 1 million Klarner-Rado numbers using the
Overall, the code provides two different implementations of the Klarner-Rado function and showcases its usage to generate and print the first 1 million Klarner-Rado numbers.
Source code in the julia programming language
using Formatting
function KlarnerRado(N)
K = [1]
for i in 1:N
j = K[i]
firstadd, secondadd = 2j + 1, 3j + 1
if firstadd < K[end]
pos = findlast(<(firstadd), K) + 1
K[pos] != firstadd && insert!(K, pos, firstadd)
elseif K[end] != firstadd
push!(K, firstadd)
end
if secondadd < K[end]
pos = findlast(<(secondadd), K) + 1
K[pos] != secondadd && insert!(K, pos, secondadd)
elseif K[end] != secondadd
push!(K, secondadd)
end
end
return K
end
kr1m = KlarnerRado(1_000_000)
println("First 100 Klarner-Rado numbers:")
foreach(p -> print(rpad(p[2], 4), p[1] % 20 == 0 ? "\n" : ""), enumerate(kr1m[1:100]))
foreach(n -> println("The ", format(n, commas=true), "th Klarner-Rado number is ",
format(kr1m[n], commas=true)), [1000, 10000, 100000, 1000000])
using Formatting
function KlamerRado(N)
kr = falses(100 * N)
kr[1] = true
for i in 1:30N
if kr[i]
kr[2i + 1] = true
kr[3i + 1] = true
end
end
return [i for i in eachindex(kr) if kr[i]]
end
kr1m = KlamerRado(1000000)
println("First 100 Klarner-Rado numbers:")
foreach(p -> print(rpad(p[2], 4), p[1] % 20 == 0 ? "\n" : ""), enumerate(kr1m[1:100]))
foreach(n -> println("The ", format(n, commas=true), "th Klarner-Rado number is ",
format(kr1m[n], commas=true)), [1000, 10000, 100000, 1000000])
You may also check:How to resolve the algorithm Magic 8-ball step by step in the uBasic/4tH programming language
You may also check:How to resolve the algorithm Factors of an integer step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Roots of unity step by step in the Racket programming language
You may also check:How to resolve the algorithm Kaprekar numbers step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Pick random element step by step in the Prolog programming language