Public
Edited
Jul 8, 2024
Paused
Comments locked
5 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Math.PI / 4 === Math.atan(1) // expected output: true
Insert cell
Insert cell
Insert cell
Insert cell
Math.PI / 2 === 2 * Math.atan(1 / Math.sqrt(2)) + Math.atan(1 / Math.sqrt(8)) // expected output: true
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
(4 * (4 * arccot(5) - arccot(239))).toPrecision(precision)
Insert cell
{
function* arccot(x) {
let i = 1,
x_squared = x * x,
dividend = 1 / x,
divisor = 1,
series = dividend;
for (let term; ; ) {
dividend = -dividend / x_squared;
divisor += 2;
if (
dividend === 0 ||
String(series) === String(series + (term = dividend / divisor))
) {
break;
}
let reset = yield {
iterations: i++,
term: term,
result: (series += term)
};
if (reset) {
(i = 1), (dividend = 1 / x), (divisor = 1), (series = dividend);
}
}
return i;
}

await visibility();

for (
let generator = pi(arccot, precision), series;
!(series && series.done);
series = generator.next()
) {
if (series && series.value) yield Promises.delay(duration, series.value);
}
}
Insert cell
function arccot(x) {
let x_squared = x * x,
dividend = 1 / x,
divisor = 1,
series = dividend;
for (let term; ; ) {
dividend = -dividend / x_squared;
divisor += 2;
if (
dividend === 0 ||
String(series) === String(series + (term = dividend / divisor))
) {
break;
}
series += term;
}
return series;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
if (!Number.MAX_SAFE_INTEGER) {
Number.MAX_SAFE_INTEGER = Math.pow(2, 53) - 1;
}

await visibility();

GREGORY_LEIBNIZ_SERIES: for (
let series = 0, dividend = 1, divisor = 1;
divisor < Number.MAX_SAFE_INTEGER && (series += dividend / divisor);
dividend *= -1, divisor += 2
) {
yield Promises.delay(100, (4 * series).toPrecision(precision));
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
(4 * (4 * arccotEuler(5) - arccotEuler(239))).toPrecision(precision)
Insert cell
{
function* arccot(x) {
let i = 1,
coefficient = 0,
x_squared_plus_1 = x * x + 1,
series = x / x_squared_plus_1;
for (let term = series; ; ) {
term *= (coefficient += 2) / ((coefficient + 1) * x_squared_plus_1);
if (term === 0 || String(series) === String(series + term)) {
break;
}
let reset = yield {
iterations: i++,
term: term,
result: (series += term)
};
if (reset) {
(i = 1), (coefficient = 0), (term = series = x / x_squared_plus_1);
}
}
return i;
}

await visibility();

for (
let generator = pi(arccot, precision), series;
!(series && series.done);
series = generator.next()
) {
if (series && series.value) yield Promises.delay(duration, series.value);
}
}
Insert cell
function arccotEuler(x) {
let coefficient = 0,
x_squared_plus_1 = x * x + 1,
series = x / x_squared_plus_1;
for (let term = series; ; ) {
term *= (coefficient += 2) / ((coefficient + 1) * x_squared_plus_1);
if (term === 0 || String(series) === String(series + term)) {
break;
}
series += term;
}
return series;
}
Insert cell
Insert cell
/*!
* In 1706 John Machin came up with the first really fast method of
* calculating π and used it to calculate π to 100 decimal places:
*
* π/4 = 4 * arctan(1/5) - arctan(1/239)
*
* Quite an achievement considering he did that entirely with pencil
* and paper.
*/
function* pi(arccot, precision = 20) {
let n = 1,
a = arccot(5),
b = arccot(239),
expansionOf = {};
for (let series; ; ) {
series = { a: a.next(), b: b.next() };
if (!series.a.done) expansionOf.a = series.a.value;
if (!series.b.done) expansionOf.b = series.b.value;
if (series.a.done && series.b.done) {
break;
}
let reset = yield {
n: n++,
"arccot(5)": expansionOf.a,
"arccot(239)": expansionOf.b,
pi: (4 * (4 * expansionOf.a.result - expansionOf.b.result)).toPrecision(
precision
)
};
if (reset) {
(n = 1), (a = arccot(5)), (b = arccot(239)), (expansionOf = {});
}
}
return n;
}
Insert cell
precision = 20
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Decimal = require("decimal.js-light@2.5.1/decimal.js")
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more