Hello, How can I optimize the speed of the following proc: import times, math proc leibniz(terms: int): float = var res = 0.0 for n in 0..terms: res += pow(-1.0,float(n))/(2.0*float(n)+1.0) return 4*res let t0 = cpuTime() echo(leibniz(100_000_000)) let t1 = cpuTime() echo "Elapsed time: ", $(t1 - t0)
I have the following result in my computer: 3.141592663589326 Elapsed time: 8.23 This result is almost 5x faster than my CPython counter party, but on the other hand it is around 6x slower than Julia, given the following code: function leibniz(terms) res = 0.0 for i in 0:terms res += (-1.0)^i/(2.0*i+1.0) end return res *= 4.0 end println("Pi: ", @time leibniz(100_000_000)) 1.374829 seconds (1.72 k allocations: 90.561 KiB) Pi: 3.141592663589326