Skip to content

Commit

Permalink
Support 1, 2, and 3-digit chains
Browse files Browse the repository at this point in the history
  • Loading branch information
bmamlin committed Dec 11, 2023
1 parent 9e74576 commit c1d4f3b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 17 deletions.
42 changes: 36 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
#graph {
opacity: 0.5;
float: right;
display: block;
}
#size {
opacity: 0.1;
clear: right;
float: right;
width: 12%;
text-align: center;
font-size: 4em;
cursor: pointer;
}
#counter {
position: absolute;
Expand Down Expand Up @@ -46,6 +56,9 @@
<div id="graph">
<a href="javascript:openPiChain()"><img src="pi-chain.png" /></a>
</div>
<div id="size" onclick="javascript:toggleSize()">
3
</div>
<div id="counter"></div>
<div class="controls">
<!-- <button onClick="javascript:reset()">Reset</button> -->
Expand All @@ -65,6 +78,11 @@

var counter;
var currValue = -1;
var numDigits = 3;

function size() {
return document.getElementById("size");
}

function inp() {
return document.getElementById("n");
Expand All @@ -78,12 +96,18 @@
return document.getElementById("output");
}

function next(p) {
// Get digit following p for chain size s (1, 2, or 3)
function next(p, s) {
if (p < 1) return 0;
return parseInt(pi.substring(p - 1, p + 2));
if (![1,2,3].includes(s)) s = 3;
return parseInt(pi.substring(p - 1, p + (s - 1)));
}

function rand() {
// Get random number with up to s digits
function rand(s) {
if (![1,2,3].includes(s)) s = 3;
if (s == 1) return Math.floor(Math.random() * 9)+1;
if (s == 2) return Math.floor(Math.random() * 99)+1;
// Pick randomly from weighted digits (favors digits that are less often encountered in pichains)
return weightedDigits[Math.floor(Math.random() * weightedDigits.length)];
}
Expand All @@ -99,7 +123,7 @@
}

function openPiChain(event) {
window.open('pi-chain.html?q=' + (event ? event.target.innerText : inp().value));
window.open('pi-chain.html?q=' + (event ? event.target.innerText : inp().value) + '&s=' + numDigits);
}

function reset(keepValue) {
Expand All @@ -111,7 +135,7 @@
emptyDiv.innerHTML = "&nbsp;";
output().appendChild(emptyDiv);
}
if (!keepValue) setValue(rand());
if (!keepValue) setValue(rand(numDigits));
}

function log(x) {
Expand All @@ -122,6 +146,12 @@
scrollDown();
}

function toggleSize() {
numDigits = (numDigits % 3) + 1;
size().innerText = numDigits;
reset(false);
}

setTimeout(reset, 10);

function clicked() {
Expand All @@ -132,7 +162,7 @@
var n = parseInt(inp().value);
if (n != currValue) reset(1);
log(n);
var nextValue = next(n);
var nextValue = next(n, numDigits);
counter++;
updateCounter();
inp().value = nextValue;
Expand Down
34 changes: 23 additions & 11 deletions pi-chain.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,38 @@
const pi =
"14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019893";

const mainLoop = [1, 141, 535, 224, 756, 721, 864, 875, 332, 628, 342, 171, 852, 101, 821, 425, 861, 783, 973];
const mainLoop = [
[],
[2, 41, 69, 64, 30, 95, 17, 38, 97, 6, 26, 83, 28, 27, 32],
[1, 141, 535, 224, 756, 721, 864, 875, 332, 628, 342, 171, 852, 101, 821, 425, 861, 783, 973]
];

const groupColors = {
"main": "red",
0: "pink",
1: "grey",
2: "grey",
4: "grey",
7: "blue",
19: "grey",
46: "yellow",
79: "grey",
92: "blue"
}

function next(p) {
function next(p, s) {
if (p < 1) return 0;
return parseInt(pi.substring(p - 1, p + 2));
return parseInt(pi.substring(p - 1, p + (s-1)));
}

function group(p) {
function group(p, s) {
// Trace until chain then assign lowest value in chain
let sequence = [p];
let found = false;
let chain;
let n = p;
while (!found) {
n = next(n);
n = next(n, s);
if (!sequence.includes(n)) {
sequence.push(n);
} else {
Expand All @@ -59,22 +68,25 @@

let params = new URLSearchParams(document.location.search);
let q = params.get('q');
let s = parseInt(params.get('s'));
console.log(s);
if (![1,2,3].includes(s)) s = 3;
console.log(s);

let nodes = [];
let links = [];
let groups = [];
for (var i=1; i<1000; i++) {
let g = group(i);
if (mainLoop.includes(i)) {
g = "main";
}
for (var i=1; i<Math.pow(10,s); i++) {
let g = group(i, s);
if (mainLoop[s-1].includes(i)) g = "main";
let node = { "id":i, "group":g };
nodes.push(node);
let nextValue = next(i)
let nextValue = next(i, s)
links.push({"source":i, "target":nextValue, "width":10, "distance":Math.abs(i-nextValue)})
}

let data = { "nodes": nodes, "links": links };
console.log(nodes);

const graph = ForceGraph3D()
(document.getElementById("3d-graph"))
Expand Down

0 comments on commit c1d4f3b

Please sign in to comment.