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