Public
Edited
Jul 15, 2024
Insert cell
Insert cell
{
let height = 20000;
let marginBottom = 30;
let marginTop = 30;
const yAxis = Plot.plot({
width: 40,
height,
marginTop,
marginBottom
});

const chart = Plot.plot({
style: {
backgroundColor: "#222"
},
axis: null,
height,
width: 1200,
margin: 20,
marginLeft: 40,
marginRight: 120,
r: {
type: "log",
range: [2, 10],
transform: (d) => _.max(primeFactorsList[d])
},
color: {
type: "ordinal",
scheme: "Reds",
transform: (d) => primeFactorsList[d].length
},
marks: [
Plot.tree(totientPaths, {
path: "path",
r: "node:name",
fill: "node:name",
// fill: "white",
title: "node:name",
textStroke: "black"
})
]
});
chart.classList.add("chart");

const scrollbar = html`<div class="scrollbar">`;
scrollbar.append(chart);

const div = html`<div class="container">`;
div.append(yAxis, scrollbar);
return div;
}
Insert cell
Insert cell
totientPaths = {
let drawLimit = Math.min(limit, 2048);
let reverseMap = groupValues(totientList.slice(0, drawLimit));
let paths = [];
let addPaths = (num, prefix = "") => {
if (!reverseMap[num]) {
paths.push({
path: prefix + num,
name: num
});
} else {
for (let child of reverseMap[num]) {
if (+child !== +num) {
addPaths(child, "" + prefix + num + "/");
}
}
}
};
addPaths(1);
return paths;
}
Insert cell
pathLengths = {
let pathLengths = [0, 0];
for (let n of _.range(2, limit)) {
pathLengths[n] = pathLengths[totientList[n]] + 1;
}
return pathLengths;
}
Insert cell
limit = 1024
Insert cell
primeFactorsList = primeFactors(limit)
Insert cell
totientList = totients(limit)
Insert cell
function groupValues(map) {
let result = {};
for (let [key, value] of Object.entries(map)) {
if (!result[value]) {
result[value] = [];
}
result[value].push(key);
}
return result;
}
Insert cell
// All totients up to n
function totients(limit) {
let sieveLimit = Math.sqrt(limit);
let spf = _.range(0, limit).map((p) => (p % 2 === 0 ? 2 : p));
for (let p of _.range(3, sieveLimit, 2)) {
for (let m of _.range(p**2, limit, 2 * p)) {
if (spf[m] === m) {
spf[m] = p;
}
}
}
let phis = [0, 1];
for (let n of _.range(2, limit)) {
if (spf[n] === n) {
phis.push(n - 1);
} else {
let p = spf[n];
let m = n / p;
let factor = spf[m] === p ? p : p - 1;
phis.push(phis[m] * factor)
}
}
return phis;
}
Insert cell
Insert cell
import { getPrimes, primeFactors } from "@tesseralis/math"
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