-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollatz Conjecture.py
More file actions
77 lines (54 loc) · 1.49 KB
/
Collatz Conjecture.py
File metadata and controls
77 lines (54 loc) · 1.49 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Collatz Conjecture - Start with a number n > 1.
# Find the number of steps it takes to reach one
# using the following process: If n is even, divide
# it by 2. If n is odd, multiply it by 3 and add 1.
import matplotlib.pyplot as plt
def evenOrOdd(n):
n = n / 2
if n.is_integer() == False:
nValue = 1
return nValue
else:
nValue = 2
return nValue
def graph(x, y):
plt.plot(x, y)
plt.xlabel("Number of Steps")
plt.ylabel("Number Value")
plt.title("Collatz Conjecture")
plt.show()
def run():
running = True
startup = True
step = 0
stepList = []
nList = []
while startup:
n = input("Enter a number greater than 1: ")
n = int(n)
if n == 1:
print("Please input a number greater than 1.")
print("Try again.")
else:
startup = False
while running:
if n == 1:
running = False
break
nValue = evenOrOdd(n)
if n == 1:
running = False
break
elif nValue == 2:
n = n /2
print(int(n))
elif nValue == 1:
n = n * 3
n += 1
print(int(n))
step += 1
stepList.append(step)
nList.append(n)
print("It took " + str(step) + " steps to get to 1")
graph(stepList, nList)
run()