-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
180 lines (165 loc) · 10.7 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body {
font-family: arial;
height: 100%;
}
#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;
top: 20%;
left: 50%;
font-size: 10em;
font-weight: bold;
color: grey;
opacity: 0.25;
}
.controls {
position: fixed;
bottom: 0px;
}
input {
width: 4em;
height: 2em;
font-size: 4em;
}
button {
font-size: 4em;
height: 2em;
width: 10em;
}
#output {
height: 75vh;
overflow-y: scroll;
}
#output div {
font-size: 4em;
}
</style>
</head>
<body>
<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> -->
<input id="n" type="number" pattern="[0-9]*" onClick="this.select();" max="1000" />
<button onClick="javascript:clicked()">Next</button>
</div>
<div id="output"></div>
<script type="text/javascript">
const pi =
"1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380";
// Array of (964 - <count of appearances across all pi chains>) for all digits 1-1000 (i.e., 964 for numbers that never show up in a pi chain
// and 1 for digits that show up in the max of 963 pi chains).
const weight = [1, 964, 962, 964, 465, 962, 964, 964, 964, 962, 957, 961, 964, 929, 964, 964, 964, 964, 862, 964, 958, 964, 964, 962, 964, 950, 960, 947, 964, 964, 875, 964, 964, 946, 919, 957, 964, 957, 964, 964, 964, 959, 964, 964, 952, 961, 964, 964, 963, 963, 958, 964, 957, 963, 960, 960, 962, 931, 907, 964, 962, 963, 929, 964, 939, 914, 960, 964, 964, 960, 964, 960, 964, 963, 964, 964, 964, 938, 961, 964, 953, 960, 955, 964, 964, 960, 964, 964, 963, 961, 959, 933, 959, 962, 960, 898, 961, 964, 963, 962, 1, 963, 964, 963, 906, 964, 964, 964, 943, 960, 935, 961, 546, 964, 942, 959, 944, 894, 957, 963, 960, 952, 964, 964, 964, 964, 952, 961, 953, 909, 964, 934, 740, 950, 964, 963, 954, 629, 963, 964, 1, 941, 964, 964, 960, 954, 962, 963, 964, 964, 962, 963, 942, 964, 963, 964, 964, 964, 949, 962, 964, 964, 964, 962, 961, 964, 964, 964, 963, 959, 1, 628, 956, 961, 964, 961, 963, 963, 962, 964, 963, 964, 936, 961, 768, 963, 903, 961, 964, 962, 964, 960, 958, 958, 958, 956, 963, 963, 963, 937, 950, 964, 964, 963, 964, 944, 964, 962, 806, 964, 944, 963, 951, 961, 964, 963, 948, 963, 962, 964, 964, 964, 962, 1, 964, 960, 958, 964, 767, 921, 962, 964, 766, 962, 958, 964, 962, 962, 964, 964, 964, 964, 964, 959, 950, 964, 962, 963, 961, 964, 964, 948, 928, 919, 964, 964, 964, 942, 962, 963, 913, 964, 962, 953, 961, 962, 964, 963, 964, 953, 953, 962, 962, 818, 963, 964, 958, 961, 898, 954, 748, 959, 964, 959, 964, 961, 963, 762, 964, 960, 964, 963, 963, 962, 964, 964, 958, 963, 964, 963, 962, 951, 956, 964, 951, 941, 963, 926, 957, 759, 930, 964, 963, 964, 962, 961, 946, 954, 964, 957, 708, 964, 963, 964, 964, 816, 959, 933, 964, 906, 964, 1, 964, 961, 964, 959, 920, 961, 958, 964, 964, 1, 963, 943, 964, 909, 964, 955, 962, 964, 751, 963, 963, 963, 964, 963, 964, 963, 939, 961, 964, 548, 958, 908, 954, 964, 960, 964, 964, 961, 918, 961, 964, 964, 961, 964, 964, 556, 963, 964, 630, 964, 963, 960, 958, 964, 962, 956, 964, 964, 964, 963, 963, 958, 960, 964, 964, 964, 963, 964, 964, 964, 963, 964, 944, 958, 964, 959, 955, 963, 964, 963, 964, 959, 961, 964, 964, 942, 956, 960, 933, 964, 964, 964, 1, 964, 960, 962, 964, 946, 964, 961, 962, 964, 964, 954, 954, 964, 964, 954, 760, 949, 964, 964, 903, 850, 964, 964, 964, 963, 964, 920, 964, 952, 904, 963, 964, 955, 890, 962, 962, 819, 949, 964, 963, 963, 723, 551, 752, 956, 964, 964, 954, 964, 961, 964, 963, 964, 964, 961, 956, 952, 964, 964, 918, 961, 964, 660, 956, 937, 961, 964, 959, 959, 960, 964, 964, 964, 961, 964, 964, 872, 962, 964, 963, 964, 963, 964, 964, 962, 956, 964, 940, 964, 964, 964, 964, 948, 928, 963, 625, 472, 963, 964, 964, 959, 962, 963, 964, 950, 964, 961, 951, 934, 1, 963, 958, 964, 952, 960, 964, 939, 961, 964, 964, 963, 964, 959, 718, 908, 964, 964, 963, 955, 962, 964, 964, 961, 962, 963, 962, 953, 964, 946, 964, 961, 962, 963, 964, 964, 964, 963, 964, 964, 962, 964, 912, 964, 964, 964, 964, 732, 964, 964, 960, 964, 962, 960, 957, 962, 960, 960, 944, 961, 945, 961, 963, 940, 964, 938, 964, 466, 961, 964, 942, 758, 960, 955, 941, 962, 921, 961, 963, 964, 964, 964, 962, 964, 963, 963, 964, 960, 903, 963, 964, 963, 961, 1, 961, 964, 961, 964, 964, 964, 963, 964, 960, 963, 963, 962, 964, 959, 961, 934, 964, 964, 893, 915, 964, 964, 963, 554, 963, 963, 964, 963, 962, 946, 962, 956, 870, 964, 964, 941, 942, 964, 964, 964, 930, 964, 964, 964, 720, 963, 963, 961, 964, 960, 963, 963, 941, 964, 964, 962, 963, 964, 960, 964, 956, 961, 957, 941, 963, 943, 961, 964, 964, 964, 964, 940, 963, 962, 963, 964, 962, 897, 963, 964, 962, 961, 964, 958, 961, 940, 962, 963, 959, 964, 959, 964, 1, 964, 964, 960, 962, 761, 943, 964, 960, 955, 961, 873, 963, 964, 550, 963, 962, 963, 964, 964, 964, 964, 964, 919, 963, 964, 963, 955, 943, 964, 958, 904, 962, 964, 963, 1, 960, 964, 955, 964, 964, 963, 964, 964, 964, 948, 961, 964, 964, 961, 962, 964, 964, 963, 964, 963, 964, 895, 964, 950, 908, 964, 1, 964, 959, 963, 874, 964, 749, 964, 964, 962, 958, 964, 963, 955, 964, 956, 954, 964, 964, 964, 963, 962, 892, 963, 962, 905, 964, 963, 962, 754, 962, 939, 957, 958, 952, 951, 961, 962, 1, 949, 504, 964, 959, 964, 962, 964, 960, 953, 963, 949, 871, 964, 964, 964, 963, 948, 964, 964, 915, 964, 964, 961, 964, 957, 963, 963, 964, 963, 964, 1, 963, 963, 964, 963, 876, 964, 955, 567, 1, 950, 963, 1, 962, 964, 765, 964, 964, 947, 964, 914, 963, 964, 1, 959, 964, 964, 964, 964, 940, 942, 964, 945, 963, 963, 964, 964, 964, 964, 955, 937, 958, 964, 963, 962, 956, 964, 937, 964, 945, 939, 962, 937, 964, 964, 938, 938, 960, 964, 964, 950, 964, 956, 964, 964, 961, 964, 963, 932, 943, 963, 958, 964, 954, 464, 963, 964, 964, 957, 947, 925, 960, 964, 964, 958, 949, 948, 958, 959, 963, 964, 960, 930, 961, 963, 964, 902, 960, 962, 885, 950, 917, 961, 952, 893, 962, 962, 709, 952, 964, 960, 963, 961, 964, 964, 964, 960, 964, 964, 956, 964, 1, 959, 961, 964, 962, 963, 960, 964, 872, 935, 959, 964, 964, 716, 964, 964, 942, 964, 964, 964, 963, 964, 960, 887, 964, 943, 959, 964];
// Create array with digits 1-1000 repeated based on weights, so digits rarely seen in pichains are weighted more heavily (so randomly picking
// digits from this list will balance likelihood of choosing digit against likelihood of digit appearing in a pichain).
const weightedDigits = weight.flatMap((e, i) => { return Array(e).fill(i+1); });
var counter;
var currValue = -1;
var numDigits = 3;
function size() {
return document.getElementById("size");
}
function inp() {
return document.getElementById("n");
}
function updateCounter() {
return (document.getElementById("counter").innerText = counter);
}
function output() {
return document.getElementById("output");
}
// Get digit following p for chain size s (1, 2, or 3)
function next(p, s) {
if (p < 1) return 0;
if (![1,2,3].includes(s)) s = 3;
return parseInt(pi.substring(p - 1, p + (s - 1)));
}
// 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)];
}
function setValue(x) {
inp().value = x;
updateCounter();
}
function scrollDown() {
var div = document.getElementById("output");
div.scrollTop = div.scrollHeight;
}
function openPiChain(event) {
window.open('pi-chain.html?q=' + (event ? event.target.innerText : inp().value) + '&s=' + numDigits);
}
function reset(keepValue) {
counter = 1;
updateCounter();
output().innerHTML = "";
for (var i = 0; i < 26; i++) {
var emptyDiv = document.createElement("div");
emptyDiv.innerHTML = " ";
output().appendChild(emptyDiv);
}
if (!keepValue) setValue(rand(numDigits));
}
function log(x) {
var div = document.createElement("div");
div.innerText = x;
div.onclick = openPiChain;
output().appendChild(div);
scrollDown();
}
function toggleSize() {
numDigits = (numDigits % 3) + 1;
size().innerText = numDigits;
reset(false);
}
setTimeout(reset, 10);
function clicked() {
if (inp().value.trim() === "") {
reset();
return;
}
var n = parseInt(inp().value);
if (n != currValue) reset(1);
log(n);
var nextValue = next(n, numDigits);
counter++;
updateCounter();
inp().value = nextValue;
currValue = nextValue;
for (elem of output().children) {
if (elem.innerText == nextValue) {
elem.style.backgroundColor = "yellow";
} else {
elem.style.backgroundColor = "white";
}
}
}
</script>
</body>
</html>