2015-10-27 LabHailstone

posted Oct 27, 2015, 4:59 AM by Samuel Konstantinovich   [ updated Oct 27, 2015, 7:06 AM ]
Goal: Hailstone Sequence

Do Now: 
1. Take out your mySqrt homework.
2. On paper write the function (sumAtoB a b) that adds up all of the integers from a to b. Assume a and b are integers.

a. Start by assuming a<=b and complete it that way.

(sumAtoB 5 5) -> 5
(sumAtoB 5 7) -> 18   (5 + 6 + 7)

b. Now how can you add a new case such that the numbers can be in any order? (just add onto it)
(sumAtoB 7 5) -> 18   (7 + 6 + 5)



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? _________________________________

Pre-Lab:
Write a function (nextHail n) that calculates the next number in the sequence. This will make the lab much easier. 


Verify your grades here:
LabHailstone / 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