-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHailstone.py
More file actions
39 lines (34 loc) · 801 Bytes
/
Copy pathHailstone.py
File metadata and controls
39 lines (34 loc) · 801 Bytes
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
29
30
31
32
33
34
35
36
37
38
39
# -*- coding: utf-8 -*-
"""
Created on Thu May 24 19:25:51 2018
@author: suraj
"""
#*hailstone number : number of iterations it takes to reach 1
import time
start=time.process_time()
hailstone = [0]
for st in range(1,100):
x = st
count = 0
while x !=1:
if x < st:
count += hailstone[x] #Dynamic programming
break
if x%2==0:
x//=2
else:
x=(3*x+1)
count+=1
#print(st,count)
hailstone.append(count)
print(max(hailstone))
#print(hailstone[10])
print(time.process_time()-start) #time it takes to perform above oprn
def hailstone(x):
while x !=1:
if x%2==0:
x//=2
else:
x=(3*x+1)
return x
hailstone(7)