Published
Edited
Dec 11, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
collapz(5)
Insert cell
Insert cell
collapz(7)
Insert cell
Insert cell
collapz(11)
Insert cell
collapz(27)
Insert cell
collapz(53)
Insert cell
Insert cell
Insert cell
Insert cell
collapzStepTally = (number) => {
const steps = {tripled: 0, halved: 0}
let updated = number;
while (updated !== 1) {
if (updated % 2 === 0) {
updated /= 2
steps.halved += 1
} else {
updated = 3 * updated + 1
steps.tripled += 1
}
}
return [steps.halved + steps.tripled, steps]
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
collapzWithBinaries = (number) => {
let steps = 0,
updated = number,
bins = [[number, number.toString(2)]],
longest = bins[0][1].length;
while (updated !== 1) {
updated = updated % 2 === 0 ? updated / 2 : 3 * updated + 1;
let bin = updated.toString(2);
longest = Math.max(longest, bin.length);
bins.push([updated, bin]);
steps++;
}
return [steps, bins.map(([num, bin]) => [num, bin.padStart(longest)])]
}
Insert cell
Insert cell
collapz27result = collapzWithBinaries(27)
Insert cell
collapzWithBinaries(53)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
collapzWithBinaries(89)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function bigCollapz(bignumber){
let steps = 0;
let updated = BigInt(bignumber); // just in case a regular-sized int is provided
while (updated !== 1n) {
updated = (updated % 2n === 0n ? (updated / 2n) : (3n * updated + 1n));
steps++;
}
return steps
}
Insert cell
function bigBinCollapz(bignumber){
let steps = 0;
let updated = bignumber;
let bins = [bignumber.toString(2)];
let longest = bins[0].length;
while (updated !== 1n) {
updated = updated % 2n === 0n ? (updated / 2n) : (3n * updated + 1n);
let bin = updated.toString(2);
longest = Math.max(longest, bin.length);
bins.push(bin);
steps++;
}
// bins = bins.map(bin => bin.padStart(longest, " "))
return [steps, bins]
}
Insert cell
viewof numberOfNines = slider({min: 1, max: 7500, value: 2000, step: 1})
Insert cell
toTheNines = BigInt("9".repeat(numberOfNines))
Insert cell
bigBinCollapz(toTheNines)
Insert cell
import {slider} from "@jashkenas/inputs"
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