Solution to This one is a fairly simple program, but I like it a lot. It is … - Sikademy

June 20, 2021

Archangel Macsika Sikademy Image

Archangel Macsika

This one is a fairly simple program, but I like it a lot. It is called the 'hailstone sequence' problem. There is a sequence of numbers in mathematics that is sometimes known as the hailstone sequence. The German mathematician, Lothar Collatz, proposed that for any number it's possible to make a sequence of numbers that will eventually end in one by following a simple rule; if the number is even halve it by two, if it's odd times it by three and add one (e.g., starting with the number 5 the sequence would be 5 16 8 4 2 1). The name hailstone comes from the way the pattern of numbers rise and fall, like a hailstone in a weather cloud before it drops to the ground. Write a program that asks for a number and then displays its hailstone sequence. If the number is even, then the next number in the sequence is that number divided by two. If the number is odd, then the next number in the sequence is (number * 3) plus one. Display the sequence so that every line you display has at most ten numbers (this is the tricky part of the program). After the sequence reaches one, display the number of items in the sequence. Here is the output for the number 51.

Solution in Python Programming Language

hailstone_number = int(input("Enter a Number: "))
nums_box = []
counter = nums_box[-1]
while (counter != 1):
    next_num = nums_box[-1] % 2
    if (next_num == 0):
        next_num = nums_box[-1] / 2
        next_num = (nums_box[-1] * 3) + 1
    counter = nums_box[-1]

total_num = len(nums_box)
line_num = 10
for i in range(total_num/line_num+1):
    ans = nums_box[i*line_num:(i+1)*line_num]
    print ans
print "This sequence had",total_num, "items."

If you would like to see a different solution or an implementation in a different programming language Kindly let us know