Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def calc_loops(n):
- cnt_loops = 0
- while n > 0:
- ends = []
- for i in range(0, n):
- ends.append(i)
- ends.append(i)
- i = -1
- j = -1
- while i == j:
- i = random.randint(0, len(ends) - 1)
- j = random.randint(0, len(ends) - 1)
- if ends[i] == ends[j]:
- cnt_loops += 1
- n -= 1
- return cnt_loops
- # Simulation
- n = 100
- n_trials = int(5e4)
- sum = 0.0
- for i in range(0, n_trials):
- sum += calc_loops(n)
- print(sum / n_trials)
- # Exact computation
- res = 0.0
- n = 100
- while n > 0:
- res += 1.0 / (2 * n - 1)
- n -= 1
- print(res)
Advertisement
Add Comment
Please, Sign In to add comment