-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollatz_Sequence.py
More file actions
25 lines (24 loc) · 866 Bytes
/
Copy pathCollatz_Sequence.py
File metadata and controls
25 lines (24 loc) · 866 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
import sys, time
print('''
Collatz Sequence, or, the 3n + 1 Problem by sinistergeek
The Collatz sequence is a sequence of numbers produced from a starting number n, following three rules:
1) If n is even, the next number n is n / 2.
2) If n is odd, next number n is n *3 + 1.
3) If n is 1, stop.Otherwise, repeat.
It is generally thought, but so far not mathematically proven,that every starting number eventually terminates at 1.
''')
print('Enter a starting number (greater than 0) or QUIT:')
response = input('> ')
if not response.isdecimal() or response == '0':
print('You must enter an integer greater than 0.')
sys.exit()
n = int(response)
print(n,end='',flush=True)
while n != 1:
if n%2 == 0:
n = n // 2
else:
n = 3 * n + 1
print(', ' + str(n),end='',flush=True)
time.sleep(0.1)
print()