-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhailstones.py
More file actions
32 lines (25 loc) · 747 Bytes
/
Copy pathhailstones.py
File metadata and controls
32 lines (25 loc) · 747 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
"""
File: hailstones.py
-------------------
This is a file for the optional Hailstones problem, if
you'd like to try solving it.
"""
def main():
count = 0
n = int(input("Enter a positive number: "))
while n != 1:
if n % 2 == 0:
y = n/2
count += 1
print(str(int(n)) + " is even, so I take half: " + (str(int(y))))
n = y
else:
y = n*3+1
count += 1
print(str(int(n)) + " is odd, so I make 3n + 1: " + (str(int(y))))
n = y
print("The process took " + str(count) + " steps to reach 1 ")
# This provided line is required at the end of a Python file
# to call the main() function.
if __name__ == '__main__':
main()