forked from souravjain540/Basic-Python-Programs
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollatz_sequence.py
More file actions
Latest commit
18 lines (17 loc) · 677 Bytes
/
Copy pathcollatz_sequence.py
File metadata and controls
18 lines (17 loc) · 677 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# The Collatz Conjecture is a famous math problem.
# The conjecture is that after applying a sequence of one of two transformations,
# every positive integer will eventually transform into 1.
# The transformations are: divide by 2 if the number is even, multiply by 3 and add 1 if its odd.
# You can see more about it here https://en.wikipedia.org/wiki/Collatz_conjecture
defcollatz(initial_number):
num=initial_number
print(f'Initial number is: {initial_number}')
whilenum!=1:
print(num)
ifnum%2==0:
num=int(num/2)
else:
num=int(3*num+1)
else:
print(num)
print('Finally!')