Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Compute PI in an inefficient way
- import strutils, math, threadpool
- {.experimental: "parallel".}
- proc term(k: float): float = 4 * math.pow(-1, k) / (2*k + 1)
- proc pi(n: int): float = #new process, int in and float out
- var ch = newSeq[float](n+1) #create n long sequens of type float
- parallel:
- for k in 0..ch.high: #0..ch.high is a range, 0 to highest index
- ch[k] = spawn term(float(k)) # spawn for every index
- for k in 0..ch.high: #loop a range
- result += ch[k] # results from every spawn is added to results
- echo formatFloat(pi(5000)) #runs pi, formats the answer and prints it
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement