hi there,

i’m trying to do exercise 7.4 of the ThinkJulia book:

The mathematician Srinivasa Ramanujan found an infinite series that can be used to generate a numerical approximation of 1/pi. I wrote the following code, but when i run it i get no output:

function estimatepi()

k = 0

sum = 0

q = 2*(sqrt(2))/9801

while true

w = ((factorial(big(4k)))*(1103+26390*k))/(((factorial(big(k)))^(4))*((396)^(4*k)))

term = q*w

sum = sum + term

if abs(term)<1e-15

break

end

k = k + 1

end

println(1/sum)

end

why don’t i get any output? And is there a faster way to calculate summation in Julia? Maybe using some built-in functions or adding some particular packages?

The last term needs to be `big(396)^(4k)`

since otherwise you get integer overflow.

For me, this terminates after 3 iterations.

```
function estimatepi()
k = 0
sum = 0
q = 2*(sqrt(2))/9801
while true
w = (factorial(big(4k))*(1103+26390k))/(factorial(big(k))^4*big(396)^(4k))
println(w)
term = q*w
sum = sum + term
if abs(term)<1e-15
break
end
k = k + 1
end
println(1/sum)
end
```

2 Likes

thank you. What about the second question? is there a way to calculate any summation with a particular package?

The written code is perfectly fine. Of course you can try to tune such statements (LoppVectorization.jl for example) more.

But comparing to languages like Python, Matlab, etc. There is nothing wrong with `while`

or `for`

patterns.

1 Like