-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollatZ.py
More file actions
56 lines (45 loc) · 1.47 KB
/
CollatZ.py
File metadata and controls
56 lines (45 loc) · 1.47 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
This python code simulates the collatz conjecture.
more on this https://en.wikipedia.org/wiki/Collatz_conjecture
"""
def collatz_sequence(num : int) -> list[int]:
"""
It takes one parameter as the initial number. if the number is even then it
divides the number by 2 and assign the number to initial number.
if the number is odd then it multiplies by 3 and adds 1 and assigns it to initial number.
this conjecture states that the simulation stops when the number gets to 1.
This is also known as 3+1 conjecture.
This returns the list of numbers reaching to 1.
Args:
num (int): Intitial Number
Returns:
list[int]: Numbers reaching to 1.
"""
list = [num]
while num != 1:
if num%2==0:
num = num//2
list.append(num)
else:
num = 3*num +1
list.append(num)
return list
def collatz_sequence_recur(num : int) -> list[int]:
"""
Same as fuction collatz_sequence() but done recursively.
Args:
num (int): _description_
Returns:
list[int]: _description_
"""
list = [num]
if num == 1 :
return list
elif num % 2 == 0 :
list.extend(collatz_sequence_recur(num//2))
else:
list.extend(collatz_sequence_recur(num*3+1))
return list
if __name__ == "__main__":
print(collatz_sequence(7))
print(collatz_sequence_recur(7))