Public
Edited
Jul 17, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
sqrt2 = function(e) {
// for each precision e, we want to estimate sqrt2
// by applying the "secant method" on the function x^2-2
if (e<0) return undefined
let p=1, q=2
while (q-p>e) {
p=(2*p+2)/(p+2)
q=(2*q+2)/(q+2)
}
return [p, q]
}
Insert cell
sqrt2(1/10000)
Insert cell
pi = function(e) {
// Leibniz's formula for pi
// i.e., Taylor series of arctan
if (e<1e-8) return undefined
e/=4
let p=1, q=1-1/3, n=3
while (1/n>e) {
p=q+1/(n+2)
q=p-1/(n+4)
n+=4
}
return [q*4, p*4]
}
Insert cell
pi(1/10000000)
Insert cell
ln2 = function(e) {
// as integral of 1/x from 1 to 2
if (e<1e-7) return undefined
let lower=1/2, upper=1, n=2
while (upper-lower>e) {
for (let i=1; i<n; i+=2) {
lower+=(1/(1+i/n)-1/(1+(i+1)/n))/n
upper-=(1/(1+(i-1)/n)-1/(1+i/n))/n
}
n*=2
}
return [lower, upper]
}
Insert cell
ln2(1/10000)
Insert cell
Insert cell
Insert cell
Insert cell
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