Informatique/obligatoire/TP3_bonus/main.py

85 lines
1.5 KiB
Python

# Question 1:
def tempsdevol(c):
u, n = c, 0
while u != 1:
if u % 2:
u = 3*u + 1
else:
u = u//2
n += 1
return n
# Question 2
def altitude(c):
u, umax = c, c
while u != 1:
if u % 2:
u = 3*u + 1
else:
u = u//2
if u > umax:
umax = u
return umax
# Question 3
# a)
def tempsdarret(c):
u, n, nmax = c, 0, 0
while u != 1:
n += 1
if u % 2:
u = 3*u + 1
else:
u = u//2
if u >= c:
nmax = n
return nmax
# b)
import time
def verification(m):# m=1000000 -> 60 sec
start = time.time()
for c in range(0, m+1, ):
tempsdarret(c)
return time.time() - start
# c)
# Question 4
# a)
def altitudemax(borne):
res = {"c":0, "altitude":0}
for c in range(3, borne+1, 4):
current = altitude(c)
if current > res["altitude"]:
res.update({"c":c, "altitude":current})
return res
def tempsdarretmax(borne): # {'c': 665215, 'temps': 354}
res = {"c": 0, "temps": 0}
for c in range(3, borne+1, 4):
current = tempsdarret(c)
if current > res["temps"]:
res.update({"c": c, "temps": current})
return res
def tempsdarretrecord(borne):
res = {"c": 0, "temps": 0}
L=[]
for c in range(2, borne+1):
current = tempsdarret(c)
if current > res["temps"]:
res.update({"c": c, "temps": current})
L.append({"c": c, "temps": current})
return L