Skip to content

Commit 93742b7

Browse files
committed
Create collatz_sequence.py
Collatez Conjecture
1 parent 48f2a9e commit 93742b7

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

collatz_sequence.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# The Collatz Conjecture is a famous math problem.
2+
# The conjecture is that after applying a sequence of one of two transformations,
3+
# every positive integer will eventually transform into 1.
4+
# The transformations are: divide by 2 if the number is even, multiply by 3 and add 1 if its odd.
5+
# You can see more about it here https://en.wikipedia.org/wiki/Collatz_conjecture
6+
7+
def collatz(initial_number):
8+
num = initial_number
9+
print(f'Initial number is: {initial_number}')
10+
while num != 1:
11+
print(num)
12+
if num % 2 == 0:
13+
num = int(num / 2)
14+
else:
15+
num = int(3 * num + 1)
16+
else:
17+
print(num)
18+
print('Finally!')

0 commit comments

Comments
 (0)