File: tpi.nim

package info (click to toggle)
nim 1.6.10-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,165,312 kB
  • sloc: sh: 20,999; ansic: 1,716; makefile: 958; python: 461; sql: 298; asm: 141; xml: 13
file content (26 lines) | stat: -rw-r--r-- 560 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
discard """
  output: '''3.141792613595791
3.141792613595791'''
"""

import strutils, math, threadpool

proc term(k: float): float = 4 * math.pow(-1, k) / (2*k + 1)

proc piU(n: int): float =
  var ch = newSeq[FlowVar[float]](n+1)
  for k in 0..ch.high:
    ch[k] = spawn term(float(k))
  for k in 0..ch.high:
    result += ^ch[k]

proc piS(n: int): float =
  var ch = newSeq[float](n+1)
  parallel:
    for k in 0..ch.high:
      ch[k] = spawn term(float(k))
  for k in 0..ch.high:
    result += ch[k]

echo formatFloat(piU(5000))
echo formatFloat(piS(5000))