forked from FirmanKurniawan/Python-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollatz-problem.py
More file actions
49 lines (43 loc) · 1.18 KB
/
Copy pathcollatz-problem.py
File metadata and controls
49 lines (43 loc) · 1.18 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
import numpy as np
import matplotlib.pyplot as plt
def collatz(n):
global yvalues, xvalues
yvalues = []
xvalues = []
count = 0
while n > 1:
xvalues.append(count)
count += 1
yvalues.append(n)
if n % 2:
n = 3 * n + 1
else:
n = n // 2
print(yvalues, '\n', "Iterations = ", count, '\n')
def result():
for i in range(1, 100001):
print("Sequence = ", end="")
collatz(i)
plt.plot(np.array(xvalues),
np.array(yvalues),
color='black',
marker='o',
markerfacecolor='yellow',
markersize=3)
try:
mvalues = np.max(np.array(yvalues))
plt.annotate(mvalues,
(np.array(yvalues).argmax(), mvalues),
textcoords="offset points",
xytext=(0, 5),
ha='center')
except ValueError:
pass
plt.title("Collatz")
plt.xlabel("Iterations/Steps")
plt.ylabel("Numbers")
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['right'].set_visible(False)
result()
plt.grid()
plt.show()