ploffie

Car park problem

Dec 29th, 2012
84
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """
  2. Car park problem: http://mathworld.wolfram.com/RenyisParkingConstants.html
  3. theoretical solution: 0.7475979202...
  4. """
  5. import random
  6.  
  7. uni = random.uniform
  8. THEORY = 0.7475979202 # for large L
  9.  
  10. def park(L):
  11.     """ How many cars can you park on a street with length L,
  12.            if drivers park their car at random spots
  13.        L : length of parking space - length of car=1
  14.        Recursive solution
  15.    """
  16.     if L < 1:
  17.         return 0
  18.     r = uni(0, L - 1)
  19.     return 1 + park(r) + park(L - r - 1)
  20.  
  21. def main():
  22.     L = 1000000.0
  23.     cars = park(L)
  24.     sol = cars / L
  25.     print L, sol, "Theory:", THEORY, "Diff %:", (sol - THEORY) / THEORY * 100
  26.  
  27. main()
RAW Paste Data