fork(7) download
  1. from numpy import *
  2. import time
  3.  
  4. def f(x):
  5. return sin(pi*x)
  6.  
  7. n = 100000
  8. t0 = time.time()
  9. # range(1, 11)は間違いではない
  10. product = prod( [sum((1/n) * f(k/n)**m for k in range(n)) for m in range(1, 11)] )
  11. result = 1/product
  12. t1 = time.time()
  13. elapsed0 = t1-t0
  14. print(result,elapsed0)
  15. # 36722.3624763
  16.  
  17. t0 = time.time()
  18. k = arange(n)/n
  19. product = prod( [sum(f(k)**m)/n for m in range(1, 11)] )
  20. t1 = time.time()
  21. elapsed1 = t1-t0
  22. print(result,elapsed1)
  23.  
  24. t0 = time.time()
  25. k = arange(n)/n
  26. product = exp(sum(log([sum(f(k)**m)/n for m in range(1, 11)]) ))
  27. result = 1/product
  28. t1 = time.time()
  29. elapsed2 = t1-t0
  30. print(result,elapsed2)
  31.  
Success #stdin #stdout 1.26s 95296KB
stdin
Standard input is empty
stdout
36722.3621773 1.0643641948699951
36722.3621773 0.08314156532287598
36722.3621773 0.06482529640197754