# Summation for an estimate of pi

**URL:** <https://discourse.julialang.org/t/summation-for-an-estimate-of-pi/73232>\
**Category:** New to Julia\
**Created:** [December 16, 2021, 10:47pm UTC](https://discourse.julialang.org/t/summation-for-an-estimate-of-pi/73232 "2021-12-16T22:47:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fbal091](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbal091/32/31976_2.png) [@fbal091](https://discourse.julialang.org/u/fbal091)\
**Post date:** [December 16, 2021, 10:47pm UTC](https://discourse.julialang.org/t/summation-for-an-estimate-of-pi/73232/1 "2021-12-16T22:47:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 16, 2021, 10:57pm UTC](https://discourse.julialang.org/t/summation-for-an-estimate-of-pi/73232/2 "2021-12-16T22:57:42Z")

</div>

The last term needs to be `big(396)^(4k)` since otherwise you get integer overflow.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 16, 2021, 11:20pm UTC](https://discourse.julialang.org/t/summation-for-an-estimate-of-pi/73232/4 "2021-12-16T23:20:17Z")

</div>

For me, this terminates after 3 iterations.

```julia
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

```

---

<div class="post-metadata">

**Author:** ![fbal091](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbal091/32/31976_2.png) [@fbal091](https://discourse.julialang.org/u/fbal091)\
**Post date:** [December 16, 2021, 11:20pm UTC](https://discourse.julialang.org/t/summation-for-an-estimate-of-pi/73232/5 "2021-12-16T23:20:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [December 17, 2021, 12:04am UTC](https://discourse.julialang.org/t/summation-for-an-estimate-of-pi/73232/6 "2021-12-17T00:04:14Z")

</div>

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.
