Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 0.60 KB | None | 0 0
  1.  
  2. # Compute PI in an inefficient way
  3. import strutils, math, threadpool
  4. {.experimental: "parallel".}
  5.  
  6. proc term(k: float): float = 4 * math.pow(-1, k) / (2*k + 1)
  7.  
  8. proc pi(n: int): float = #new process, int in and float out
  9.   var ch = newSeq[float](n+1) #create n long sequens of type float
  10.   parallel:
  11.     for k in 0..ch.high: #0..ch.high is a range, 0 to highest index
  12.       ch[k] = spawn term(float(k)) # spawn for every index
  13.   for k in 0..ch.high: #loop a range
  14.     result += ch[k] # results from every spawn is added to results
  15.  
  16. echo formatFloat(pi(5000)) #runs pi, formats the answer and prints it
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement