-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathcollatz_sequence.py
31 lines (24 loc) · 1.17 KB
/
collatz_sequence.py
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
#IN collatz sequence you perform few mathematical operation depending on whether a number is even or odd , no matter what the integer is eventually you will get 1.
#operation performed is the number is even = number // 2 and if the number is odd = number * 3 + 1
#example if the number is 5 , out put is 5 16 8 4 2 1
from sys import exit # really no need to do that but as a precaution is good
def collatz(num): # defining the function
if (num % 2 == 0):
res = num //2
elif (num % 2 != 0):
res = num * 3 + 1
else:
return 'Enter a valid integer'
while True:
if (res == 1): #we need to exit the function after 1 is obtained
print(res)
exit(0)
elif (res != 1):
print(res)
collatz(res) #the function should be executing until 1 is obtained
try:
num = int(input('Enter an integer: ')) #asking for input and then calling the function also change input to raw_input in python 2x versions
except Exception as e:
print("Try again...")
print("\nReaching one here....")
collatz(num)