Public
Edited
Mar 17
2 forks
4 stars
Insert cell
Insert cell
sqrt2 = function(e) {
// for each precision e, we want to compute the sqrt of 2
// by applying the "secant-tangent method" on the function x^2-2
let a=1, b=2;
while (b-a > e) {
a = (2+a*b)/(a+b)
b = (b+2/b)/2
}
return [a, b]
}
Insert cell
Insert cell
sqrt2(0.000001)
Insert cell
Insert cell
Insert cell
// QQ is the class for rational numbers
new QQ(355n, 113n)
Insert cell
Insert cell
Math.PI
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
sqrt_(2n).approx(1000n)
Insert cell
Insert cell
function sqrt_(d) {
d = QQ.toQQ(d);
if (d.p < 0n) throw Error(`Cannot take sqrt of a negative number: ${d.p}/${d.q}`);
return new RR((N) => { // (N)=> is short for function(N)
const eps = new QQ(1n, N);
let a = new QQ(0n), b = d.add(1n).divide(2n);
while (b.subtract(a).greaterThan(eps)) {
a = d.add(a.multiply(b)).divide(a.add(b));
b = b.add(d.divide(b)).divide(2n);
a = a.relax(-N*4n); // Relax a slightly lower
b = b.relax(N*4n); // Relax b slightly higher
}
return [a, b];
});
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
phi.power(20n).approx(100000n)
Insert cell
Insert cell
function Archimedes(iter) {
// initilize hexagons
if (iter == 0) return [RR.toRR(3n), sqrt(new QQ(1n, 3n)).multiply(6n)];
let [p, P] = Archimedes(iter-1);
P = p.multiply(P).multiply(2n).divide(p.add(P));
p = sqrt(p.multiply(P));
return [p, P];
}
Insert cell
// Archimedes(3).map(x=>x.approx(100n))
Insert cell
Insert cell
pi_Archimedes = new RR((N) => {
const eps = new QQ(1n,N);
let a1 = new QQ(3n), a2 = a1;
let [b1, b2] = sqrt(new QQ(1n,3n)).multiply(6n).approx(N*N);
while (b2.subtract(a1).greaterThan(eps)) {
[b1, b2] = [a2.multiply(b2).multiply(2n).divide(a1.add(b1)), a1.multiply(b1).multiply(2n).divide(a2.add(b2))];
[a1, a2] = [sqrt(a1.multiply(b1)).approx(N*N)[0], sqrt(a2.multiply(b2)).approx(N*N)[1]];
}
return [a1, b2];
})
Insert cell
pi_Archimedes.approx(1000n)
Insert cell
Insert cell
pi_Leibniz = new RR((N) => {
let a = new QQ(0n), b = new QQ(1n);
let k = 3n;
while (k < N*2n) {
if (k%4n == 3n) a = b.subtract(new QQ(1n, k)).relax(-N*N);
if (k%4n == 1n) b = a.add(new QQ(1n, k)).relax(N*N);
k += 2n;
}
return [a, b];
}).multiply(4n)
Insert cell
pi_Leibniz.approx(1000n)
Insert cell
Insert cell
arctan(new QQ(1n,5n)).multiply(4n).subtract(arctan(new QQ(1n,239n))).multiply(4n).approx(1000000000000n)
Insert cell
function arctan(x) {
x = QQ.toQQ(x);
return new Alternating((k) => x.power(2n*k+1n).divide(2n*k+1n));
}
Insert cell
Insert cell
pi_Machin = arctan(new QQ(1n,5n)).multiply(4n).subtract(arctan(new QQ(1n,239n))).multiply(4n)
Insert cell
Insert cell
function exp_(x) {
if (x instanceof RR) {
return new RR((N)=> {
const eps = new QQ(1n, N);
while (true) {
let [a, b] = x.approx(N);
[a, b] = [exp_(a).approx(N)[0], exp_(b).approx(N)[1]];
if (b.subtract(a).lessThan(eps)) return [a, b];
N *= 2n;
}
});
} else {
x = QQ.toQQ(x);
return new RR((N)=> {
const eps = new QQ(1n, N);
let sum = new QQ(1n);
let term = new QQ(1n), k = 1n;
while (true) {
term = term.multiply(x).divide(k);
sum = sum.add(term).relax(-N*N);
if (x.lessThan(k)) {
// estimate via geometric series
const delta = term.multiply(x).divide(x.subtract(k)).multiply(-1n)
if (delta.lessThan(eps)) return [sum, sum.add(delta)];
}
k++;
}
});
}
}
Insert cell
exp_(1n).approx(100000000n)
Insert cell
Insert cell
x = pi_Machin.multiply(sqrt(163n)).divide(32n)
Insert cell
exp_(x).power(32n).approx(10000n)
Insert cell
Insert cell
Insert cell
Insert cell
arctan_integral = new MonoIntegral((x) => x.power(2n).add(1n).inverse(), 0n, 1n)
Insert cell
arctan_integral.multiply(4n).approx(1000n)
Insert cell
Insert cell
Insert cell
pi_AGM = new RR((N) => {
const eps = new QQ(1n,N);
let [a1, a2] = sqrt(new QQ(2n)).approx(N*N);
let b1 = new QQ(2n), b2 = b1;
let t1 = new QQ(4n), t2 = t1;
let k = 0n;
while (true) {
const [pi1, pi2] = [a1.power(2n).divide(t2), b2.power(2n).divide(t1)];
if (pi2.subtract(pi1).lessThan(eps)) return [pi1, pi2];
[t1, t2] = [t1.subtract(b2.subtract(a1).power(2n).multiply(2n**k)), t2.subtract(b1.subtract(a2).power(2n).multiply(2n**k))];
[a1, a2, b1, b2] = [sqrt(a1.multiply(b1)).approx(N*N)[0], sqrt(a2.multiply(b2)).approx(N*N)[1], a1.add(b1).divide(2n), a2.add(b2).divide(2n)];
k++;
}
}).multiply(4n)
Insert cell
pi_AGM.approx(10000000000n)
Insert cell
Insert cell
QQ.from_cfrac([6,3,1,1,7,2])
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