-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcollatz.py
More file actions
26 lines (21 loc) · 866 Bytes
/
collatz.py
File metadata and controls
26 lines (21 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
25
26
# The Collatz conjecture is a conjecture in mathematics named after Lothar Collatz.
# The conjecture is also known as the "3n + 1 conjecture".
# The conjecture can be summarized as follows:
# Take any positive integer n. If n is even, divide it by 2 to get n / 2.
# If n is odd, multiply it by 3 and add 1 to obtain 3n + 1.
# Repeat the process (which has been called "Half Or Triple Plus One", or HOTPO) indefinitely.
# The conjecture is that no matter what number you start with, you will always eventually reach 1.
def collatz_conjecture(n):
"""Print the 3n+1 sequence from n, terminating when it reaches 1"""
seq = [n]
if n < 1:
return []
while n > 1:
if n % 2 == 0:
n //= 2
else:
n = n*3 + 1
seq.append(n)
return seq
n = int(input("Enter a number: "))
print(collatz_conjecture(n))