Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is CsCircles, section 16:Recursions, Excercise: Hailstone. This is CsCircle

ID: 3779292 • Letter: T

Question

This is CsCircles, section 16:Recursions, Excercise: Hailstone. This is CsCircles, section 16:Recursions, Excercise: Hailstone.
Python language Check answer Coding Exercise: Hailstone The hailstone sequence starting at a positive integer n is generated by following two simple rules. If n is even, the next number in the sequence is n/2. lf n is odd, the next number in the sequence is 3*n+1. Repeating this process, we generate the hailstone sequence. Write a recursive function hailstoneCn) which prints the hailstone sequence beginning at n. Stop when the sequence reaches the number 1 (since otherwise, we would loop forever 1, 4,2, 1,4, 2, For example, when n 5, your program should output the following sequence: 16 1 delete this comment and enter your code here Enter test statements Open in console Visualize Run program. More actions...

Explanation / Answer

def hailstone(n):
if n==1 :
print 1
return
elif n > 1 and n%2 :
print n
hailstone( 3*n+1 )
elif n > 1 and n%2 == 0:
print n
hailstone(n/2)
else:
return
  
hailstone(5)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote