tpi.nim 582 B

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