2014-10-23 LAB

posted Oct 23, 2014, 5:19 AM by Samuel Konstantinovich   [ updated Oct 23, 2014, 7:07 AM ]
Hailstone:

Hailstone is a numerical sequence that ends at 1 and starts at any positive integer.

The next number in the sequence depends on the current number.
hailstone of n
When n is 1 that is the last number, so stop.
When n is even -> the next number is  n/2, keep going to the next number
When n is odd  -> the next number is  3n+1, keep going to the next number


hailstone of 1 -> 1
hailstone of 2 -> 2 1
hailstone of 3 -> 3 10 5 16 8 4 2 1
hailstone of 4 -> 4 2 1
hailstone of 5 -> 5 16 8 4 2 1
hailstone of 6 -> 6 3 10 5 16 8 4 2 1
etc.

What is the hailstone of 7? _________________________________

Lab / Complete at home:
You will write functions that use this sequence.
1. (hailLen n) will tell you the length of the hailstone sequence started by n.
(hailLen 1) -> 1
(hailLen 2) -> 2
(hailLen 3) -> 8
(hailLen 5) -> 6
etc.
Since hailstone of 6 -> 6 3 10 5 16 8 4 2 1
The (hailLen 6) -> 9  , there are 9 numbers in the sequence

2. (maxHail n) will tell you the largest number in the hailstone sequence started by n.
(maxHail 3) -> 16
(maxHail 4) -> 4
(maxHail 5) -> 16
(maxHail 7) -> 52
Comments