1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 15 23:41:45 2016
@author: Shruti
"""
#Iteration with while: using while, write a function which takes one argument,
#an integer n, and calculates the Hailstones sequence starting at n.
#If you don't know the Hailstones sequence:
#if hihi is even, then hi+1=hi/2hi+1=hi/2 ; but if hihi is odd,
#then hi+1=3hi+1hi+1=3hi+1 .
#The sequence will repeat when it reaches the subsequence 4, 2, 1, 4, …… ,
#so your loop should stop when it hits 1
import``math
def``hailstones(x):
while``x &gt;``1``:`<br>print("%d"%x)<br>```if``(x``%``2``=``=``0``):
x``=``x``/``2`<br>print("%d"%x)<br>```elif``(x``%``2``!``=``0``):
x``=``3``*``x``+``1`<br>print("%d"%x)<br>hailstones(15)`