Skip to content

Instantly share code, notes, and snippets.

@spytheman
Created July 15, 2021 14:58
Show Gist options
  • Save spytheman/609d212518968bd0bc5f571784cb0165 to your computer and use it in GitHub Desktop.
Save spytheman/609d212518968bd0bc5f571784cb0165 to your computer and use it in GitHub Desktop.
// This program is a direct port of https://crypto.stanford.edu/pbc/notes/pi/code.html
// which in turn is an implementation of (Beeler et al. 1972, Item 120),
// see https://mathworld.wolfram.com/PiFormulas.html
// It calculates and prints the first 800 digits of pi, using only integer math.
const maxr = 2800
fn main() {
mut r := [maxr + 1]int{}
mut i, mut k, mut b, mut d, mut c := 0, 0, 0, 0, 0
for i = 0; i < maxr; i++ {
r[i] = 2000
}
for k = maxr; k > 0; k -= 14 {
d = 0
i = k
for {
d += r[i] * 10000
b = 2 * i - 1
r[i] = d % b
d /= b
i--
if i == 0 {
break
}
d *= i
}
print('${c + d / 10000:04d}')
c = d % 10000
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment