Published
Edited
Jan 21, 2022
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
Math.log(10000)
Insert cell
Insert cell
lowerBound = 1e4
Insert cell
upperBound = 1e5
Insert cell
avgGap = (Math.log(lowerBound) + Math.log(upperBound)) / 2
Insert cell
Insert cell
Insert cell
Insert cell
nCandidates = (upperBound - lowerBound)
Insert cell
Insert cell
nPrimes = nCandidates / avgGap
Insert cell
Insert cell
pctEstimate = nPrimes / nCandidates
Insert cell
Insert cell
// https://stackoverflow.com/a/40200710/120290
isPrime = (num) => {
for (let i = 2, s = Math.sqrt(num); i <= s; i++)
if (num % i === 0) return false;
return num > 1;
}
Insert cell
pctActual = {
let prime = 0;
let composite = 0;
for (let i = lowerBound; i < upperBound; i++) {
if (isPrime(i)) {
prime++;
} else {
composite++;
}
if (!(i % 1000) || i === upperBound - 1)
yield [i, (prime / (prime + composite)), prime];
}
}
Insert cell
Insert cell
Insert cell
f = d3.format(".1%")
Insert cell
import {tweet} from "@mbostock/tweet"
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