-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyracuse.py
73 lines (64 loc) · 1.83 KB
/
syracuse.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
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
reseach_mode = False
# Mode de recherche, afin de voir toutes les itérations en cours. Cela peut prendre plus de temps à faire la recherche, je vous déconseille de passer cela sur vrai. (Sauf si vous en avez besoin)
import time as t
import matplotlib.pyplot as plt
def convert(sec):
mins = sec // 60
sec = round(sec%60,0)
hours = mins //60
mins = mins %60
return int(hours),int(mins),sec
def syracuse(x,iteration):
"""
Parameters
----------
x : INT
X de base.
iteration : INT
Nombres de vol au quel on souhaite dépasser.
Returns
-------
val : INT
x au quel le nombre de vol > vol recherché.
i : INT
Nombre de vol trouvé..
"""
if x == 0 or x == 1:
return
i = 0
val = x
start = t.time()
while i <= iteration:
i = 0
iteration_liste = []
result = []
if x != val:
x = val + 1
val = val + 1
if reseach_mode != False:
print('new x: ', val)
while x!=1:
if x%2==0:
x = x/2
i = i+1
iteration_liste.append(i)
result.append(x)
if reseach_mode != False:
print("intermediate result: ",x)
else:
x = (3*x)+1
i = i+1
iteration_liste.append(i)
result.append(x)
if reseach_mode != False:
print("intermediate result: ",x)
stop = t.time()
temps_passe_s = stop - start
temps_final = convert(temps_passe_s)
plt.plot(iteration_liste, result, label="Résultat/Nb d'itération")
plt.ylabel("résultat")
plt.xlabel("itération")
plt.legend()
plt.show()
print('time_elapsed : ', temps_final)
return val,i