Complete Codingbat set3 (hailstone)
OLD HOMEWORK REMINDER: (I will not remind you in the future to keep up with codingbat)
To avoid penalties and understand the work we are doing next, you MUST complete ALL codingbat problems assigned.
This includes the following currently:
make10, sleep_in, monkey_trouble, diff21,
parrot_trouble, makes10, near_hundred, pos_neg
sorta_sum, near_ten, love6
lone_sum, lucky_sum, no_teen_sum
4) Custom Problems: *edit* you can access set2 any way you want. It works both ways.
http://firstname.lastname@example.org/set3 * this is new as of today *
Goal: Generating a sequence with a loop.
The hailstone sequence starts at any natural number, and always terminates with the value 1.
To generate the sequence you follow a simple set of rules:
if n = 1 : done
if n is even: the next number is n / 2
if n is odd: the next number is 3 * n + 1
So hailstone sequences starting with the first 6 values:
3 10 5 16 8 4 2 1
4 2 1
5 16 8 4 2 1
6 3 10 5 16 8 4 2 1
Write a function printHail(n) that will print the hailstone sequence (on one line for readability)
You can print with a comma at the end of the line to prevent the new line from printing
Normally we don't print out things, instead we return them. We know the starting/ending number of each sequence, so that isn't a useful thing to return. Something we cannot predict is the length of the sequence!
Write a function that calculates the length of a hailstone sequence that starts with n:
(look at the example sequences above to see why!)
n -> hailLen(n)
1 -> 1
2 -> 2
3 -> 8
4 -> 3
5 -> 6
6 -> 9