-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcollatz.py
More file actions
39 lines (31 loc) · 905 Bytes
/
collatz.py
File metadata and controls
39 lines (31 loc) · 905 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
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
Written in Python 3.
This program is all yours now! Have fun experimenting!!
Collatz conjecture: Count steps to reach 1 from n. If even: divide n by 2
if odd: multiply n by 3 and add 1.
teaches: creating functions, function calling, loops, loops with else (Python's cool!).
"""
def collatz(n): # collatz function with n as parameter.
step = 0
num = n
while(True):
if n % 2 == 0:
n = n/2
step += 1
elif n % 2 != 0 and n != 1:
n = n *3 + 1
step += 1
elif n == 1:
step += 1
break
print ("It took %s steps to reach 1 with %s"%(step,num))
def input():
# takes input and rejects it to use it as an argument till it's neither 1 nor 0.
n= int(input("Enter a no. "))
while n==1 or n == 0:
print ("Nope, Another..")
input()
else:
collatz(n) # call collatz function with n as argument
input() #calls input function
# written by @shreydan. github.com/shreydan