FedorTsarev

Untitled

Mar 12th, 2025 (edited)
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. import random
  2.  
  3. def calc_loops(n):
  4.     cnt_loops = 0
  5.  
  6.     while n > 0:
  7.         ends = []
  8.         for i in range(0, n):
  9.             ends.append(i)
  10.             ends.append(i)
  11.         i = -1
  12.         j = -1
  13.         while i == j:
  14.             i = random.randint(0, len(ends) - 1)
  15.             j = random.randint(0, len(ends) - 1)
  16.         if ends[i] == ends[j]:
  17.             cnt_loops += 1
  18.         n -= 1
  19.  
  20.     return cnt_loops
  21.    
  22. # Simulation    
  23. n = 100
  24. n_trials = int(5e4)
  25. sum = 0.0
  26. for i in range(0, n_trials):
  27.     sum += calc_loops(n)
  28. print(sum / n_trials)
  29.  
  30. # Exact computation
  31. res = 0.0
  32. n = 100
  33. while n > 0:
  34.     res += 1.0 / (2 * n - 1)
  35.     n -= 1
  36. print(res)
Advertisement
Add Comment
Please, Sign In to add comment