Published
Edited
Oct 21, 2021
1 star
Insert cell
Insert cell
Plot.barX(distanceFromSqrtToNearestFactor, { y: "i", x: "dist" }).plot()
Insert cell
distanceFromSqrtToNearestFactor = {
let i = 0;
const arr = [];
while (i < 150) {
const sqrt = Math.sqrt(i);
if (isPrime(i)) {
arr.push({ i, sqrt, dist: NaN });
} else {
let dist = 0;
while (dist < 10) {
const low = Math.floor(sqrt) - dist;
const high = Math.ceil(sqrt) + dist;
if ((i / low) % 1 === 0 && low > 1) break;
if ((i / high) % 1 === 0) break;
dist++;
}
arr.push({ i, sqrt, dist });
}
yield arr;
i++;
}
}
Insert cell
// https://stackoverflow.com/a/40200710
function isPrime(num) {
for (var i = 2; i < num; i++) if (num % i === 0) return false;
return num > 1;
}
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