tpi.nim 560 B

123456789101112131415161718192021222324252627
  1. discard """
  2. output: '''3.141792613595791
  3. 3.141792613595791'''
  4. """
  5. import strutils, math, threadpool
  6. proc term(k: float): float = 4 * math.pow(-1, k) / (2*k + 1)
  7. proc piU(n: int): float =
  8. var ch = newSeq[FlowVar[float]](n+1)
  9. for k in 0..ch.high:
  10. ch[k] = spawn term(float(k))
  11. for k in 0..ch.high:
  12. result += ^ch[k]
  13. proc piS(n: int): float =
  14. var ch = newSeq[float](n+1)
  15. parallel:
  16. for k in 0..ch.high:
  17. ch[k] = spawn term(float(k))
  18. for k in 0..ch.high:
  19. result += ch[k]
  20. echo formatFloat(piU(5000))
  21. echo formatFloat(piS(5000))