How to resolve the algorithm Hailstone sequence step by step in the Io programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Hailstone sequence step by step in the Io programming language
Table of Contents
Problem Statement
The Hailstone sequence of numbers can be generated from a starting positive integer, n by:
The (unproven) Collatz conjecture is that the hailstone sequence for any starting number always terminates.
This sequence was named by Lothar Collatz in 1937 (or possibly in 1939), and is also known as (the):
The hailstone sequence is also known as hailstone numbers (because the values are usually subject to multiple descents and ascents like hailstones in a cloud).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Hailstone sequence step by step in the Io programming language
Source code in the io programming language
makeItHail := method(n,
stones := list(n)
while (n != 1,
if(n isEven,
n = n / 2,
n = 3 * n + 1
)
stones append(n)
)
stones
)
out := makeItHail(27)
writeln("For the sequence beginning at 27, the number of elements generated is ", out size, ".")
write("The first four elements generated are ")
for(i, 0, 3,
write(out at(i), " ")
)
writeln(".")
write("The last four elements generated are ")
for(i, out size - 4, out size - 1,
write(out at(i), " ")
)
writeln(".")
numOfElems := 0
nn := 3
for(x, 3, 100000,
out = makeItHail(x)
if(out size > numOfElems,
numOfElems = out size
nn = x
)
)
writeln("For numbers less than or equal to 100,000, ", nn,
" has the longest sequence of ", numOfElems, " elements.")
You may also check:How to resolve the algorithm 9 billion names of God the integer step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Sum of a series step by step in the Julia programming language
You may also check:How to resolve the algorithm Number names step by step in the F# programming language
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the Go programming language
You may also check:How to resolve the algorithm Walk a directory/Non-recursively step by step in the Icon and Unicon programming language