2014-02-11

posted Feb 11, 2014, 10:48 AM by Samuel Konstantinovich   [ updated Feb 11, 2014, 11:19 AM ]
GOAL: More recursion problems!!!



You should complete hail, hailLen, and longestHail recursively. 
You don't have to use hail(n) in the other parts; however, you probably DO want to use hailLen in longestHail.
1. Basic:
hail(n)

n=1 
  print n 
n=even 
  print n 
  hail(n/2)
n=odd 
  print n 
  hail(3n+1)

example:
hail(1) 1
hail(2) 2 1
hail(3) 3 10 5 16 8 4 2 1
hail(4) 4 2 1
hail(5) 5 16 8 4 2 1
hail(6) 6 3 10 5 16 8 4 2 1


2. hailLen(n) : What is the length of the sequence generated by hail(n)

example:
hailLen(1) : 1
hailLen(2) : 2
hailLen(3) : 8
hailLen(4) : 3
hailLen(5) : 6
hailLen(6) : 9

3. longestHail(n) : what starting value from 1 to n generates the longest length sequence.
examples:
longestHail(1)  : 1    
longestHail(2)  : 2
longestHail(3)  : 3  
longestHail(4)  : 3
longestHail(5)  : 3
longestHail(6)  : 6   





Comments