fork download
  1. #!/usr/bin/python3
  2.  
  3. from math import sin, cos, atan2, pi
  4. import random
  5.  
  6. rnd = random.Random(1)
  7.  
  8. def rndpt(a):
  9. while True:
  10. x = rnd.uniform(-1, 1)
  11. y = rnd.uniform(-1, 1)
  12. if x*x + y*y > 1:
  13. continue
  14. ang = atan2(y, x)
  15. if ang < 0:
  16. ang += 2*pi
  17. if ang > a:
  18. continue
  19. return (x, y)
  20.  
  21. def trial(a, rep=10000):
  22. asum = 0
  23. for iteration in range(rep):
  24. x1, y1 = rndpt(a)
  25. x2, y2 = rndpt(a)
  26. asum += abs(x1*y2 - x2*y1)
  27. return (asum/rep/2, (4*(a-sin(a)))/(9*a*a))
  28.  
  29. for n in range(1, 8):
  30. r, c = trial(2*pi/n)
  31. print("2*pi/{}: {:+.6f} = {:.6f} - {:.6f}".format(n, r-c, r, c))
  32.  
Success #stdin #stdout 2.03s 11984KB
stdin
Standard input is empty
stdout
2*pi/1: +0.071004 = 0.141740 - 0.070736
2*pi/2: -0.000270 = 0.141201 - 0.141471
2*pi/3: -0.000717 = 0.123743 - 0.124460
2*pi/4: -0.001508 = 0.101307 - 0.102816
2*pi/5: -0.000530 = 0.085475 - 0.086005
2*pi/6: -0.000001 = 0.073426 - 0.073426
2*pi/7: -0.000481 = 0.063380 - 0.063861