Commit 40e65d98 authored by cermak's avatar cermak
Browse files

Solution of last lesson

parent 36bfc8ab
Pipeline #25039 passed with stage
in 1 minute and 38 seconds
#!/usr/bin/python3
import random
N = 10
pokusu = 1000
bez_pevnych = 0
celkem_pevnych = 0
for _ in range(pokusu):
perm = random.sample(range(N), N)
pevne = 0
for i in range(N):
if perm[i] == i:
pevne += 1
celkem_pevnych += pevne
if pevne == 0:
bez_pevnych += 1
print("Celkem pokusů:", pokusu)
print("Pravděpodobnost permutace bez pevného bodu:", bez_pevnych / pokusu)
print("Průměrný počet pevných bodů:", celkem_pevnych / pokusu)
#!/usr/bin/python3
# %%
import random
N = 100
def is_probably_prime(p):
for _ in range(N):
a = random.randrange(1e10)
if a % p != pow(a, p, p):
return False
return True
# %%
print(f"{is_probably_prime(13)=}")
print(f"{is_probably_prime(1453453)=}")
print(f"{is_probably_prime(2396279)=}")
#!/usr/bin/python3
import random
N = 10
max_steps = 99999999
repeats = 10000
def go():
poloha = 0
for i in range(max_steps):
krok = random.choice([-1,1])
poloha = max(0, poloha + krok) # hranice na nule
#poloha = abs(poloha + krok) # pruchod nulou
if poloha == N:
return i
total_steps = 0
for _ in range(repeats):
total_steps += go()
print(f"Průměrně jsem do polohy {N} došel za {total_steps/repeats} kroků.")
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment