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