# Derive a spigot formula for pi using Julia

**URL:** <https://discourse.julialang.org/t/derive-a-spigot-formula-for-pi-using-julia/35970>\
**Category:** Community\
**Created:** [March 14, 2020, 2:48pm UTC](https://discourse.julialang.org/t/derive-a-spigot-formula-for-pi-using-julia/35970 "2020-03-14T14:48:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisvwx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisvwx/32/45289_2.png) [@chrisvwx](https://discourse.julialang.org/u/chrisvwx)\
**Post date:** [March 14, 2020, 2:48pm UTC](https://discourse.julialang.org/t/derive-a-spigot-formula-for-pi-using-julia/35970/1 "2020-03-14T14:48:53Z")

</div>

The [BBP formula](https://en.wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%93Plouffe_formula) for \pi is

\pi= \sum\_{k=0}^\infty \frac{1}{16^k} \left(\frac{4}{8k+1}-\frac{2}{8k+4}-\frac{1}{8k+5}-\frac{1}{8k+6}\right).

It was the first technique that could produce a digit (hexadecimal in this case) for \pi without calculating the prior digits. The package [PiBBP.jl](https://github.com/kalvotom/PiBBP.jl) from Tomáš Kalvoda can be used to calculate the millionth digit of \pi. Mosè Giordano has also used Julia to [evaluate](https://giordano.github.io/blog/2017-11-21-hexadecimal-pi/) the formula.

Can we use Julia to derive this formula? Or better yet, similar formulas for our favorite irrationals? Yes; the `spigotBBP` function in the [LLLplus.jl](https://github.com/christianpeel/LLLplus.jl) package can be used to look for a formula for an irrational \alpha of the form

\alpha = \sum\_{k=0}^\infty \frac{1}{b^k} \left( \frac{a\_1}{(nk+1)^s} + \ldots + \frac{a\_n}{(nk+n)^s} \right),

where s, b, and n are parameters. In particular, fixing s=1, b=16, n=8, and looking for an approximation to the first K=45 terms we can use the following command to find a\_1,...a\_n:

```julia
julia> Pkg.add("LLLplus"); using LLLplus
julia> spigotBBP(BigFloat(pi),1,16,8,45,true)'
  A solution was found w error -4.728672e-60. In LaTeX form it is
  \alpha= \sum_{k=0}^\infty \frac{1}{16^k} \left(\frac{4}{8k+1}-\frac{2}{8k+4}-\frac{1}{8k+5}-\frac{1}{8k+6}\right)
1×8 Adjoint{BigFloat,Array{BigFloat,1}}:
 4.0 0.0 0.0 -2.0 -1.0 -1.0 0.0 0.0

```

I.e. the BBP formula for \pi at the top of the post was found. If you want other irrational fun, it’s fairly straightforward to find other formulas with s=1; see the help text for `spigotBBP`. Unfortunately the function does not seem to work for s\>1; it’s not obvious if this is somehow fundamental to the [LLL](https://en.wikipedia.org/wiki/Lenstra%E2%80%93Lenstra%E2%80%93Lov%C3%A1sz_lattice_basis_reduction_algorithm)-based scheme I used, or there is a bug. This does help show that `spigotBBP` is not a robust tool, yet hopefully good enough for March 14.

flatten the curve! 🙂
