Instantly share code, notes, and snippets.

# GavalasDev/piG3.py

Created May 9, 2021 09:03
Show Gist options
• Save GavalasDev/d72f705629724db4f0e90ee8a57a8f20 to your computer and use it in GitHub Desktop.
A faster spigot algorithm for the digits of Pi.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 """ This algorithm is based on an unproven conjecture but successfully produces at least the first 1 million digits. Read more about it here: https://www.gavalas.dev/blog/spigot-algorithms-for-pi-in-python/ """ def gospers_pi_unproven(): q,r,t,i = 1, 180, 60, 2 while True: u,y = 3*(3*i+1)*(3*i+2), (q*(27*i-12)+5*r)//(5*t) yield y q,r,t,i = 10*q*i*(2*i-1),10*u*(q*(5*i-2)+r-y*t),t*u,i+1 pi = gospers_pi_unproven() print([next(pi) for _ in range(10)])

This is indeed significantly faster than any other Pi algorithm (in python) I've come across!
Any chance it could be modified, to get it to calculate the digits of Tau?
Lately I've been fascinated by tinkering around with different ways to visualize digits of Pi, as they generate, and this faster algorithm enables some really interesting results! So I'd love to do the same with Tau.. Unfortunately, my understanding of these formulas is nowhere near good enough to work that out myself. :/

### GavalasDev commented Aug 24, 2022

This is indeed significantly faster than any other Pi algorithm (in python) I've come across! Any chance it could be modified, to get it to calculate the digits of Tau? Lately I've been fascinated by tinkering around with different ways to visualize digits of Pi, as they generate, and this faster algorithm enables some really interesting results! So I'd love to do the same with Tau.. Unfortunately, my understanding of these formulas is nowhere near good enough to work that out myself. :/

Hello @Nikorasu,

I don't currently have access to my original notes regarding the inner workings of the different algorithms, so I had to result to reverse engineering. With the understanding that `τ = 2π` all that's needed to convert the last algorithm is a change of initial values. Specifically, if variable `t` is initialized to 30 (instead of 60) the resulting program successfully produces the digits of tau up to (at least) 600000 digits. The modified version of the code can be found here.

### Nikorasu commented Aug 24, 2022

Hello @Nikorasu,
I don't currently have access to my original notes regarding the inner workings of the different algorithms, so I had to result to reverse engineering. With the understanding that `τ = 2π` all that's needed to convert the last algorithm is a change of initial values. Specifically, if variable `t` is initialized to 30 (instead of 60) the resulting program successfully produces the digits of tau up to (at least) 600000 digits. The modified version of the code can be found here.

Ah, thank you so much! That works perfectly for the use I had in mind.
Even through trial and error, I probably wouldn't have guessed halving 60 was the solution. I would've assumed it had something more to do with the 180, since pi can be described as: pi = 3.14 radians = 180. So clearly such formulas are not my strong area. heh

Oh also, I can indeed confirm it accurately produces digits of Tau, at least up to 1 million. Took about 3.5 hours, but no differences detected when compared to the confirmed digits.