-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpi-spigot.js
More file actions
91 lines (71 loc) · 2.08 KB
/
pi-spigot.js
File metadata and controls
91 lines (71 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Original: https://github.com/NesHacker/NesPi/blob/main/pi-spigot.js
// Modified to output the highest value of each variable for 4800 digits.
// This helps test how many bits need to be allocated for each.
function toHex (number) {
return `${number.toString(16).toUpperCase()}`
}
/**
* JavaScript implementation of pi-spigot.
* @see https://www.maa.org/sites/default/files/pdf/pubs/amm_supplements/Monthly_Reference_12.pdf
* @param {number} n Number of digits to calculate.
* @returns {string} A string containing `n` digits of pi.
*/
function piSpigot (n) {
n += 1
const len = (10*n / 3) | 0
const A = []
const digits = []
for (let k = 0; k < len+1; k++) {
A.push(2)
}
let nines = 0
let predigit = 0
let highestLeft = 0, highestRight = 0, highestQ = 0, highestZ = 0;
for (let j = n; j >= 1; j--) {
let q = 0
let z = 0
for (let i = len; i >= 1; i--) {
let left = 10 * A[i]
let right = q * i
z = left + right
if (left > highestLeft) highestLeft = left;
if (right > highestRight) highestRight = right;
if (q > highestQ) highestQ = q;
if (z > highestZ) highestZ = z;
let twoI = 2 * i
let twoIMinusOne = twoI - 1
A[i] = z % twoIMinusOne
q = (z / twoIMinusOne) | 0
}
A[1] = q % 10
q = (q / 10) | 0
if (q == 9) {
nines++
} else if (q == 10) {
digits.push(predigit + 1)
for (let k = 1; k <= nines; k++) {
digits.push(0)
}
predigit = 0
nines = 0
} else {
digits.push(predigit)
predigit = q
//if (j == 6) console.log(predigit)
if (nines != 0) {
for (let k = 1; k <= nines; k++) {
digits.push(9)
}
nines = 0
}
}
}
console.log("Highest left: " + highestLeft);
console.log("Highest right: " + highestRight);
console.log("Highest q: " + highestQ);
console.log("Highest z: " + highestZ);
//const checksum = toHex(digits.reduce((a, b) => a + b) % 0x100)
//console.log('Checksum: ' + checksum)
return digits[1] + '.' + digits.slice(2).join('')
}
piSpigot(4800)