Public
Edited
Jul 8, 2024
Paused
Comments locked
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
compute = function (precision) {
// Calculate number of digits per iteration
let dpi = Decimal.set({ precision: 100 })("640_320")
.pow(3)
.div(Decimal("1_728" /* = 24 * 6 * 2 * 6 */))
.log();

Decimal.precision = "3.".length + (precision = +precision || 1);

function chudnovsky(n, C) {
// C = 426880 * 10005^0.5
C = Decimal("426_880").times(Decimal("10_005").sqrt());

Object.assign(C, {
"545_140_134": Decimal("545_140_134"),
"-262_537_412_640_768_000": Decimal("-262_537_412_640_768_000")
});

// See https://www.sitepoint.com/javascript-functions-that-define-and-rewrite-themselves/
chudnovsky = function (n, pi, q, Mq, Lq, Xq, Kq) {
for (
q = 0,
pi = Decimal(0),
Kq = Decimal(-6),
Lq = Decimal("13_591_409"),
Mq = Xq = Decimal(1);
q++ < n;

) {
/*!
* Multinomial term: Mq = (6q)! / ((3q)! * (q)!^3)
* Linear term: Lq = 545140134q + 13591409
* Exponential term: Xq = -262537412640768000^q
* Pi series partial summation:
*/
pi = pi.plus(Mq.times(Lq).div(Xq));
Kq = Kq.plus(12);
Mq = Mq.times(Kq.pow(3).minus(Kq.times(16)).div(Decimal(q).pow(3)));
Lq = Lq.plus(C["545_140_134"]);
Xq = Xq.times(C["-262_537_412_640_768_000"]);
}

// π = C / pi
return C.div(pi).valueOf();
};

return chudnovsky(n);
}

let result = [];

for (let n of Array.from(
{ length: Math.max(Math.ceil(precision / +dpi), 1) },
(_, i) => ++i
)) {
result[n - 1] = chudnovsky(n);
}

return result;
}
Insert cell
Insert cell
runAsWorker
.call(
clone(compute, 'importScripts("' + (await require.resolve(library)) + '")'),
1000
)
.then(async function* (result) {
await visibility();
yield* traverse(result, inspect);
})
.catch((failure) => failure)
Insert cell
Insert cell
inspect(decimalPlaces)
Insert cell
decimalPlaces = Decimal.set({ precision: 100 })("640_320")
.pow(3)
.div(Decimal("1_728" /* = 24 * 6 * 2 * 6 */))
.log()
Insert cell
inspect(C)
Insert cell
Decimal.set({ precision: 100 })("640_320").pow(1.5).div(12).equals(C)
Insert cell
C = Decimal.set({ precision: 100 })("426_880").times(Decimal("10_005").sqrt())
Insert cell
545_140_134 === 163 * 127 * 19 * 11 * 7 * 3 ** 2 * 2
Insert cell
13_591_409 === 1_045_493 * 13
Insert cell
Decimal("262_537_412_640_768_000").equals(Decimal("640_320").pow(3))
Insert cell
Decimal.set({ precision: 100 })
// = exp(π * sqrt(163)) - 640320^3
.acos(-1)
.times(Decimal(163).sqrt())
.naturalExponential()
.minus(Decimal("640_320").pow(3))
.toFixed(21)
.slice(0, -1) + "…"
Insert cell
Insert cell
Insert cell
Insert cell
Decimal = require(library)
Insert cell
library = "decimal.js@10.3.1/decimal.js"
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more