I wrote possibly the most inefficient algorithm ever to calculate the user's choice for any number of digits of pi.
Code:
As you can probably see, it doesn't use any trigonometry or calculus. In fact, the inspiration for this algorithm comes from probably the most unexpected place possible: idealized elastic collisions in classical Newtonian Dynamics.
Basically, the math describes the velocities of two objects (one which is 100^(N-1) times the mass of the other, where N= the number of digits to calculate) as the larger one pushes the smaller one toward an immovable wall over a frictionless floor.
The only thing the algorithm outputs is the number of collisions (divided by powers of ten based off how many digits it will calculate).
A fantastic video illustrating this phenomenon by YouTuber 3blue1brown says it way better than I could:
https://www.youtube.com/watch?v=HEfHFsfGXjs
This is elegant and all, but this algorithm takes pi(10^(N-1)) loops of the repeat function to complete.
As such I made the program count up to the final value on the screen to make the process the tiniest bit more fun to watch as you wait patiently for an output only a few digits long.
I just found it very interesting that it's possible to calculate pi using only a ratio (even though pi is irrational) and iterations of very simple algebra. Hope you guys find it food for thought!
Code:
2->dim(|LM)
2->dim(|LV)
0->Z
0->C
Disp "DIGITS TO CALCULATE
Prompt N
100^(N-1)->|LM(2)
1->|LM(1)
0->|LV(1)
0-1->|LV(2)
Repeat Z
|LV(1)->U
|LV(2)->Y
(|LM(1)-|LM(2))/(|LM(1)+|LM(2))U+2|LM(2)/(|LM(1)+|LM(2))Y->|LV(1)
2|LM(1)/(|LM(1)+|LM(2))U+(|LM(2)-|LM(1))/(|LM(1)+|LM(2))Y->|LV(2)
If |LV(2)-|LV(1)<0 or |LV(1)<0
Then
C+1->C
Else
1->Z
End
(0-1)|LV(1)->|LV(1)
If |LV(2)-|LV(1)<0 or |LV(1)<0
Then
C+1->C
Else
C+1->C
1->Z
End
Disp C/(10^(N-1))
End
As you can probably see, it doesn't use any trigonometry or calculus. In fact, the inspiration for this algorithm comes from probably the most unexpected place possible: idealized elastic collisions in classical Newtonian Dynamics.
Basically, the math describes the velocities of two objects (one which is 100^(N-1) times the mass of the other, where N= the number of digits to calculate) as the larger one pushes the smaller one toward an immovable wall over a frictionless floor.
The only thing the algorithm outputs is the number of collisions (divided by powers of ten based off how many digits it will calculate).
A fantastic video illustrating this phenomenon by YouTuber 3blue1brown says it way better than I could:
https://www.youtube.com/watch?v=HEfHFsfGXjs
This is elegant and all, but this algorithm takes pi(10^(N-1)) loops of the repeat function to complete.
As such I made the program count up to the final value on the screen to make the process the tiniest bit more fun to watch as you wait patiently for an output only a few digits long.
I just found it very interesting that it's possible to calculate pi using only a ratio (even though pi is irrational) and iterations of very simple algebra. Hope you guys find it food for thought!