pseudocode pi
/**
This program computes Pi by the formula:
pi / 4 = 4 * ArcTan(1/5) - ArcTan(1/239)
Adapted from some program I found on the Internet a long time
ago that computed Pi by:
pi / 4 = ArcTan(1/2) + ArcTan(1/3)
I have no idea who wrote the original program nor remember where
I found it--there wasn't any credits in the original C source. This
one also no longer looks like the original as I've completely rewritten
it. I got a lot of my information from
http://www.boo.net/~jasonp/pipage.html when I rewrote the program. This
program will reliably compute upto 1,000,000 digits, that I've verified.
There are faster ways of computing Pi, and this program probably
could be optimized further, but it is sufficiently fast for computing
Pi using relatively portable and understandable routines that can
be easily converted into other languages.
Computing Pi using FFTs are much much faster, but also much more
complicated. Using the ArcTan methods are fairly simple to implement,
but not nearly as fast as the FFT methods. Also, the
pi / 4 = 4 * ArcTan(1/5) - ArcTan(1/239) formula is one of the faster
of the various ArcTan formulas for computing PI.
Author: M. Scott Reynolds
Date: 13 August 2016
*/
include console.log
/**
* Compute pi up to maxDigits long.
* Return result as a string.
*/
function computePi(maxDigits: Number): String
const SIZE = 1000
var precision: Number
var remainder1, remainder2, remainder3, remainder4: Number
var b, n, n2, carry: Number
var i, l: Number
var isZero: Boolean
var p, t: Array of Number
var text: String
precision := (maxDigits-1) div 3
p := new Array[precision+1]
t := new Array[precision+1]
// Initialize t.
for i := 0 to precision do
t[i] := 0
end for
// Note, borrows and carries from the addition and subtraction
// are postponed till last. See: http://www.boo.net/~jasonp/ord
// Compute arctan(1/5)
// t = t / 5, p = t
t[0] := 1
remainder1 := 0
for i := 0 to precision do
b := SIZE * remainder1 + t[i]
t[i] := b div 5
p[i] := t[i]
remainder1 := b mod 5
end for
// While t is not zero.
n := -1
n2 := 1
repeat
// t = t / 25, p = p - t / n, t = t / 25, p = p + t / (n+2)
remainder1 := 0
remainder2 := 0
remainder3 := 0
remainder4 := 0
isZero := true
n := n + 4
n2 := n2 + 4
for i := 0 to precision do
b := SIZE * remainder1 + t[i]
t[i] := b div 25
remainder1 := b mod 25
b := SIZE * remainder2 + t[i]
p[i] := p[i] - b div n
remainder2 := b mod n
b := SIZE * remainder3 + t[i]
t[i] := b div 25
remainder3 := b mod 25
b := SIZE * remainder4 + t[i]
p[i] := p[i] + b div n2
remainder4 := b mod n2
if isZero and t[i] != 0 then
isZero := false
end if
end for
until isZero
// p = p * 4
carry := 0
for i := precision downto 0 do
b := p[i] * 4 + carry
p[i] := b mod SIZE
carry := b div SIZE
end for
// Compute arctan(1/239)
// t = t / 239, p = p - t
t[0] := 1
remainder1 := 0
for i := 0 to precision do
b := SIZE * remainder1 + t[i]
t[i] := b div 239
p[i] := p[i] - t[i]
remainder1 := b mod 239
end for
// While t is not zero.
n := -1
n2 := 1
repeat
// t = t / 57121, p = p + t / n, t = t / 57121, p = p - t / (n+2)
remainder1 := 0
remainder2 := 0
remainder3 := 0
remainder4 := 0
isZero := true
n := n + 4
n2 := n2 + 4
for i := 0 to precision do
b := SIZE * remainder1 + t[i]
t[i] := b div 57121
remainder1 := b mod 57121
b := SIZE * remainder2 + t[i]
p[i] := p[i] + b div n
remainder2 := b mod n
b := SIZE * remainder3 + t[i]
t[i] := b div 57121
remainder3 := b mod 57121
b := SIZE * remainder4 + t[i]
p[i] := p[i] - b div n2
remainder4 := b mod n2
if isZero and t[i] != 0 then // is t zero?
isZero := false
end if
end for
until isZero
// p = p * 4
carry := 0
for i := precision downto 0 do
b := p[i] * 4 + carry
p[i] := b mod SIZE
carry := b div SIZE
end for
// Borrow and carry.
for i := precision downto 1 do
if p[i] < 0 then
b := p[i] div SIZE
p[i] := p[i] - (b - 1) * SIZE
p[i-1] := p[i-1] + b - 1
end if
if p[i] >= SIZE then
b := p[i] div SIZE
p[i] := p[i] - b * SIZE
p[i-1] := p[i-1] + b
end if
end for
// Store results in string buffer.
text := String(p[0]) + "."
for i := 1 to precision do
if p[i] < 10 then
text := text + "00" + String(p[i])
elsif p[i] < 100 then
text := text + "0" + String(p[i])
else
text := text + String(p[i])
end if
end for
return text
end function
var s: String
s := computePi(10000)
log(s)