How to resolve the algorithm Hailstone sequence step by step in the Limbo programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Hailstone sequence step by step in the Limbo 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 Limbo programming language
Source code in the limbo programming language
implement Hailstone;
include "sys.m"; sys: Sys;
include "draw.m";
Hailstone: module {
init: fn(ctxt: ref Draw->Context, args: list of string);
};
init(nil: ref Draw->Context, nil: list of string)
{
sys = load Sys Sys->PATH;
seq := hailstone(big 27);
l := len seq;
sys->print("hailstone(27): ");
for(i := 0; i < 4; i++) {
sys->print("%bd, ", hd seq);
seq = tl seq;
}
sys->print("⋯");
while(len seq > 4)
seq = tl seq;
while(seq != nil) {
sys->print(", %bd", hd seq);
seq = tl seq;
}
sys->print(" (length %d)\n", l);
max := 1;
maxn := big 1;
for(n := big 2; n < big 100000; n++) {
cur := len hailstone(n);
if(cur > max) {
max = cur;
maxn = n;
}
}
sys->print("hailstone(%bd) has length %d\n", maxn, max);
}
hailstone(i: big): list of big
{
if(i == big 1)
return big 1 :: nil;
if(i % big 2 == big 0)
return i :: hailstone(i / big 2);
return i :: hailstone((big 3 * i) + big 1);
}
You may also check:How to resolve the algorithm Extreme floating point values step by step in the C programming language
You may also check:How to resolve the algorithm Sort an integer array step by step in the COBOL programming language
You may also check:How to resolve the algorithm Smith numbers step by step in the BASIC programming language
You may also check:How to resolve the algorithm Towers of Hanoi step by step in the Ursala programming language
You may also check:How to resolve the algorithm Sorting algorithms/Gnome sort step by step in the Sidef programming language