# Programma 1: tel een groot aantal keer de
# waarde 1 bij een getal op.
import time
t0 = time.process_time()
HERH = 7400000
som = 0
for i in range(0, HERH):
som += 1
t1 = time.process_time()
print("Aantal secondes rekentijd", t1-t0)
# Programma 2: product, een groot aantal
# keer vermenigvuldigen met 1
import time
t0 = time.process_time()
HERH = 7400000
prod = 1
for i in range(0, HERH):
prod *= 1
t1 = time.process_time()
print("Aantal secondes rekentijd", t1-t0)
# Programma 3: product, een groot aantal
# keer vermenigvuldigen met 2
import time
import math
t0 = time.process_time()
HERH = 200000
prod = 1
for i in range(0, HERH):
prod *= 2
t1 = time.process_time()
print("Aantal secondes rekentijd", t1-t0)
print("Aantal cijfers macht van twee", \
math.ceil(math.log(prod,10)))
# Programma 4: rechtstreeks uitrekenen van
# macht van 2
import time
import math
t0 = time.process_time()
EXP = 230000
macht = 2**EXP
t1 = time.process_time()
print("Aantal secondes rekentijd", t1-t0)
print("Aantal cijfers macht van twee:", \
math.ceil(math.log(macht,10)))
# Programma 5: rechtstreeks uitrekenen van
# machten van grondtallen
import time
import matplotlib.pyplot as plt
EXP = 500000
Tijden = []
Xset = range(2,33)
for grondtal in Xset:
t0 = time.process_time()
macht = grondtal**EXP
t1 = time.process_time()
Tijden.append(t1-t0)
plt.scatter(Xset, Tijden, \
label='Aantal secondes rekentijd')
plt.title('Tijden uitgezet \
tegen grondtal')
plt.legend()
plt.show()
# Programma 6: som van kwadraten
import time
import math
t0 = time.process_time()
HERH = 4000000
som = 0
for i in range(0, HERH):
som += i*i
t1 = time.process_time()
print("Aantal secondes rekentijd", t1-t0)
print("Aantal cijfers van de som", \
math.ceil(math.log(som,10)))
# Programma 7: berekenen van faculteiten
import time
import math
t0 = time.process_time()
HERH = 53000
prod = 1
for i in range(1, HERH+1):
prod *= i
t1 = time.process_time()
print("Aantal secondes rekentijd", t1-t0)
print("Aantal cijfers van de \
faculteit", \
math.ceil(math.log(prod,10)))
# Programma 8: Rij van Fibonacci
import time
import math
t0 = time.process_time()
HERH = 240000
a = 0
b = 1
for i in range(0,HERH):
c = a + b
a = b
b = c
t1 = time.process_time()
print("Aantal secondes rekentijd", t1-t0)
print("Cijfers laatste fibonaccigetal", \
math.ceil(math.log(b, 10)))
# Programma 9: Exponentiële rij van
# Fibonacci
import time
import math
import matplotlib.pyplot as plt
Tijden = []
Lengten = []
t0 = time.process_time()
HERH = 33
a = 0
b = 1
Xset = range(0, HERH)
for i in Xset:
c = a * b + 1
a = b
b = c
Tijden.append(math.log( \
time.process_time()-t0,10))
if b > 1:
Lengten.append(math.log( \
math.ceil(math.log(b,10)),10))
else:
Lengten.append(0)
plt.scatter(Xset, Lengten, \
label='Log(aantal cijfers)')
plt.scatter(Xset, Tijden, \
label='Log(totale tijd in seconden)')
plt.title('Totale tijd en aantal \
cijfers')
plt.legend()
plt.show()
t1=time.process_time()
print("Aantal secondes rekentijd", t1-t0)