-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnumber_wall.html
1029 lines (923 loc) · 26.8 KB
/
number_wall.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>Number Wall</title>
<link rel="icon" href="res/favicon.ico">
<script type="text/javascript">
/*
a
bcd
o
bd + ao = c2
o = (c2 - bd) / a
When a is 0:
e
f
gh0ij
c
o
gi2 + h2j = ec2 + f2o
o = (gi2 + h2j - ec2) / f2
When f is also 0, 0s will form a square:
yyyy
xxxxxx
yx0000xy
yx0000xy
yx0000xy
yx0000xy
xaaaax
bbbb
Calculate a's from x's:
x > X > x
v ^
Y Z
v ^
x > A > x
All 4 sides of the square will form a geometric sequence. Multipliers (X,Y,Z,A)
are all rational, and related like this:
YZ = XA A = YZ/X if square of 0s has an even size
YZ = -XA A = -YZ/X if square of 0s has an odd size
Calculate b's using x's, y's, and a's, and the multipliers from before:
f>
e>
hg0000
vv0000
0000^^
0000cd
<a
<b
Yf/e - Xh/g = Zb/a - Ad/c
b = (Yf/e - Xh/g + Ad/c)a/Z
*/
let wallWidth = 30;
let wallHeight = 30;
let wall = null;
let testMode = false;
let domWall = null;
let domColorMod = null;
let domColorModVal = null;
let domSeqSel = null;
let domRecur = null;
let domRecurFormula = null;
let domRecurVals = null;
let domNoRecur = null;
let domCustom = null;
function safeBigInt(x) {
try {
return BigInt(x);
} catch (e) {}
return null;
}
function safeGet(node, defaultValue = null) {
return safeBigInt(node.value) ?? defaultValue;
}
function clamp(x, lo, hi) {
return x <= lo ? lo : (x >= hi ? hi : x);
}
function lerp(x0, x1, k) {
return x0 * (1 - k) + x1 * k;
}
function gcd(a, b) {
if (a < b) return gcd(b, a);
while (b > 0) {
const c = a % b;
a = b;
b = c;
}
return a;
}
function genSeed() {
let x = 0n;
for (let i = 0; i < 16; ++i) {
x <<= 8n;
x |= BigInt(~~(0x100 * Math.random()));
}
return x;
}
class Random {
constructor(seed = null) {
this.x = BigInt(seed ?? genSeed());
}
rand() {
const r = Number(this.x & 0xFFFFFFFFn);
for (let i = 0; i < 32; ++i) {
const b = (this.x >> 128n) ^ (this.x >> 126n) ^ (this.x >> 101n) ^
(this.x >> 99n);
this.x = (this.x >> 1n) | ((b & 1n) << 127n);
}
return r / (1 << 16) / (1 << 16);
}
randf(h = 1, l = 0) {
return l + this.rand() * (h - l);
}
randi(h = 1, l = 0) {
return Math.floor(this.randf(h + 1, l));
}
}
class Frac {
constructor(u, v, skipReduction = false) {
console.assert(v != 0, u, v);
if (u == 0) {
this.u = 0n;
this.v = 1n;
return;
}
this.u = BigInt(v < 0 ? -u : u);
this.v = BigInt(v < 0 ? -v : v);
if (!skipReduction) this.reduce();
}
reduce() {
const negative = this.u < 0;
const u = negative ? -this.u : this.u;
const w = gcd(u, this.v);
this.u = (negative ? -u : u) / w;
this.v = this.v / w;
}
mul(o) {
// Assuming this and o are already reduced, the only reducing that needs to
// happen looks like (ab/c)*(d/ae), which we can do like (d/c)*(ab/ae).
// This is a win because (ab/ae) is faster to reduce than (adb/ace), as it
// lets us reduce before we multiply.
const x = new Frac(this.u, o.v);
const y = new Frac(o.u, this.v);
return new Frac(x.u * y.u, x.v * y.v, true);
}
add(o) {
// If the denominators of this and o are ax and ay, then the new denominator
// can be axy, with the numerators being multiplied by y and x respectively.
// We still have to reduce the final fraction, but this saves a bit of work.
const z = new Frac(this.v, o.v);
return new Frac(this.u * z.v + o.u * z.u, this.v * z.v);
}
neg() { return new Frac(-this.u, this.v, true); }
abs() { return this.isNeg() ? this.neg() : this; }
inv() { return new Frac(this.v, this.u, true); }
div(o) { return this.mul(o.inv()); }
sub(o) { return this.add(o.neg()); }
equals(o) { return this.u == o.u && this.v == o.v; }
isZero() { return this.u == 0; }
isOne() { return this.u == this.v; }
isNeg() { return this.u < 0; }
isFrac() { return true; }
isInt() { return this.v == 1; }
intMul(x) {
const ux = x * this.u;
console.assert(ux % this.v == 0, this, x);
return ux / this.v;
}
intMod(n) {
const nv = this.v * n;
return new Frac(((this.u % nv) + nv) % nv, this.v, true);
}
toInt() {
console.assert(this.v == 1, this.u, this.v);
return this.u;
}
toNum() {
return Number(this.u) / Number(this.v);
}
toString() {
let s = '' + this.u;
if (this.v != 1) s += '/' + this.v;
return s;
}
}
class Polynomial {
constructor(coeffs) {
this.a = coeffs;
console.assert(this.a.every((e) => e.isFrac()));
while (this.a.length > 0 && this.a[this.a.length - 1].isZero()) {
this.a.pop();
}
}
mul(o) {
const b = [];
for (let i = 0; i < this.a.length; ++i) {
for (let j = 0; j < o.a.length; ++j) {
const k = i + j;
const y = this.a[i].mul(o.a[j]);
if (k >= b.length) {
console.assert(k == b.length);
b.push(y);
} else {
b[k] = b[k].add(y);
}
}
}
return new Polynomial(b);
}
add(o) {
const b = [];
const an = this.a.length;
const on = o.a.length;
const n = an > on ? an : on;
for (let i = 0; i < n; ++i) {
b.push(i < an ? (i < on ? this.a[i].add(o.a[i]) : this.a[i]) : o.a[i]);
}
return new Polynomial(b);
}
toFrac() {
console.assert(this.a.length <= 1);
return this.a.length == 0 ? new Frac(0n, 1n, true) : this.a[0];
}
toInt() { return this.toFrac().toInt(); }
degree() { return this.a.length == 0 ? 0 : this.a.length - 1; }
term(i) { return i < this.a.length ? this.a[i] : 0; }
isZero() { return this.a.length == 0; }
sub(o) { return this.add(o.neg()); }
neg() { return new Polynomial(this.a.map(f => f.neg())); }
isPoly() { return true; }
equals(o) {
if (this.a.length != o.a.length) return false;
for (let i = 0; i < this.a.length; ++i) {
if (!this.a[i].equals(o.a[i])) return false;
}
return true;
}
divExpectFrac(o) {
// Divide by a polynomial. Expect a simple fraction result.
console.assert(o.isPoly());
console.assert(!o.isZero());
if (this.isZero()) return this;
console.assert(this.a.length == o.a.length);
const y = this.a[0].div(o.a[0]);
if (testMode) {
for (let i = 1; i < this.a.length; ++i) {
const z = this.a[i].div(o.a[i]);
console.assert(y.equals(z));
}
}
return y;
}
divExpectPoly(o) {
// Divide by a polynomial. Expect a polynomial result.
console.assert(o.isPoly());
console.assert(!o.isZero());
if (this.isZero()) return this;
const q = this.a.map(e => new Frac(0, 1));
const r = this.a.slice();
// Maintain the invariant that o * q + r == this.
const rlen = () => {
for (let i = r.length - 1; i >= 0; --i) {
if (!r[i].isZero()) return i + 1;
}
return 0;
};
const on = o.a.length - 1;
const oc = o.a[on];
while (rlen() >= o.a.length) {
const rn = rlen() - 1;
const t = r[rn].div(oc);
const tn = rn - on;
console.assert(tn >= 0);
q[tn] = q[tn].add(t);
for (let i = 0; i <= on; ++i) {
const m = t.mul(o.a[i]);
const mn = i + tn;
console.assert(mn < r.length);
r[mn] = r[mn].sub(m);
}
}
console.assert(rlen() == 0);
return new Polynomial(q);
}
fracDiv(o) {
// Divide by a fraction.
console.assert(o.isFrac());
return new Polynomial(this.a.map(f => f.div(o)));
}
fracMul(o) {
// Multiply by a fraction.
console.assert(o.isFrac());
return new Polynomial(this.a.map(f => f.mul(o)));
}
toString() {
if (this.a.length == 0) return '0';
let s = '';
for (let i = this.a.length - 1; i >= 0; --i) {
let f = this.a[i];
if (f.isZero()) continue;
if (s.length > 0) {
s += f.isNeg() ? ' - ' : ' + ';
f = f.abs();
}
if (i == 0 || !f.isOne()) s += f.toString();
if (i > 0) s += 'x';
if (i > 1) s += '^' + i;
}
return s;
}
toRecurrenceString(rn) {
if (this.a.length == 0) return '0';
let s = '';
for (let i = this.a.length - 1; i >= 0; --i) {
let f = this.a[i];
if (f.isZero()) continue;
if (s.length > 0) {
s += f.isNeg() ? ' - ' : ' + ';
f = f.abs();
}
if (!f.isOne()) s += f.toString();
s += 'Sn-' + (rn - i);
}
return s;
}
applyRecurrence(seq, rn, from, to) {
const o = [];
const s = (i) => i < from ? seq(i) : o[i - from];
for (let i = from; i < to; ++i) {
let y = 0;
for (let j = 0; j < rn; ++j) {
y += this.term(rn - 1 - j) * s(i - 1 - j);
}
o.push(y);
}
return o;
}
}
function intToPoly(x0) {
return new Polynomial([new Frac(BigInt(x0), 1)]);
}
function linearPoly(x0, x1) {
return new Polynomial([new Frac(BigInt(x0), 1), new Frac(BigInt(x1), 1)]);
}
function fracToPoly(f) {
console.assert(f.isFrac());
return new Polynomial([f]);
}
function cache1D(fn) {
const cache = new Map();
return (i) => {
if (!cache.has(i)) cache.set(i, fn(i));
return cache.get(i);
};
}
function cache2D(fn) {
const cache = new Map();
return (ri, ci) => {
if (!cache.has(ri)) cache.set(ri, new Map());
const row = cache.get(ri);
if (!row.has(ci)) row.set(ci, fn(ri, ci));
return row.get(ci);
};
}
class Window {
constructor(boundUp = false, boundLeft = false, boundRight = false) {
this.rlo = null;
this.rhi = null;
this.clo = null;
this.chi = null;
this.x = null;
this.y = null;
this.z = null;
this.a = null;
this.boundUp = boundUp;
this.boundDown = false;
this.boundLeft = boundLeft;
this.boundRight = boundRight;
}
minSize() {
return Math.max(this.rhi - this.rlo, this.chi - this.clo);
}
updateBounds() {
const s = this.minSize();
if (this.boundUp) this.rhi = this.rlo + s;
if (this.boundDown) this.rlo = this.rhi - s;
if (this.boundLeft) this.chi = this.clo + s;
if (this.boundRight) this.clo = this.chi - s;
if (this.boundLeft && this.boundRight) {
this.boundUp = this.boundDown = (this.boundUp || this.boundDown);
}
if (this.boundUp && this.boundDown) {
this.boundLeft = this.boundRight = (this.boundLeft || this.boundRight);
}
}
add(ri, ci) {
if (this.rlo == null || ri < this.rlo) this.rlo = ri;
if (this.rhi == null || ri > this.rhi) this.rhi = ri;
if (this.clo == null || ci < this.clo) this.clo = ci;
if (this.chi == null || ci > this.chi) this.chi = ci;
this.updateBounds();
}
isInside(ri, ci) {
if (ri >= this.rlo && ri <= this.rhi &&
ci >= this.clo && ci <= this.chi) {
return 1; // Definitely inside.
}
if (this.boundLeft && this.boundRight && this.boundUp && this.boundDown) {
return 0; // Definitely outside.
}
return -1; // Unknown.
}
expandHorizontally(cellFn) {
console.assert(!this.boundLeft || !this.boundRight);
const expandRight = this.boundLeft;
const ciexp = expandRight ? this.chi + 1 : this.clo - 1;
if (cellFn(this.rlo, ciexp) == 0) {
this.add(this.rlo, ciexp);
} else {
if (expandRight) {
this.boundRight = true;
} else {
this.boundLeft = true;
}
this.updateBounds();
}
}
toString(cellFn) {
let s = `(${this.rlo}, ${this.clo}) -> (${this.rhi}, ${this.chi})\n`;
for (let ri = this.rlo - 2; ri <= this.rhi + 2; ++ri) {
for (let ci = this.clo - 2; ci <= this.chi + 2; ++ci) {
if (ri <= this.rhi) {
s += cellFn(ri, ci).toString();
} else if (ci >= this.clo && ci <= this.chi) {
s += '?';
} else if (
ri == this.rhi + 1 && (ci == this.clo - 1 || ci == this.chi + 1)) {
s += cellFn(ri, ci).toString();
} else {
s += ' ';
}
s += '\t';
}
s += '\n';
}
return s;
}
fillMultipliers(cellFn) {
console.assert(
this.boundLeft && this.boundRight && this.boundUp && this.boundDown);
if (this.a != null) return;
const fn = (rlo, clo, rhi, chi, dr, dc) => {
let m = null;
let r = rlo;
let c = clo;
let v = cellFn(r, c);
while (true) {
const nr = r + dr;
const nc = c + dc;
const nv = cellFn(nr, nc);
const nm = nv.divExpectFrac(v);
if (m == null) {
m = nm;
if (!testMode) break;
} else {
console.assert(m.equals(nm), m, nm);
}
r = nr;
c = nc;
v = nv;
if (r == rhi && c == chi) break;
}
return m;
};
this.x = fn(this.rlo - 1, this.clo - 1, this.rlo - 1, this.chi + 1, 0, 1);
this.y = fn(this.rlo - 1, this.clo - 1, this.rhi + 1, this.clo - 1, 1, 0);
this.z = fn(this.rhi + 1, this.chi + 1, this.rlo - 1, this.chi + 1, -1, 0);
this.a = this.y.mul(this.z).div(this.x);
if (this.minSize() % 2 == 1) this.a = this.a.neg();
}
horseshoeRule(ri, ci, cellFn) {
console.assert(
this.boundLeft && this.boundRight && this.boundUp && this.boundDown);
this.fillMultipliers(cellFn);
if (ri == this.rhi + 1) {
return cellFn(ri, ci - 1).fracMul(this.a);
} else {
console.assert(ri == this.rhi + 2);
const dc = this.chi - ci;
const a = cellFn(this.rhi + 1, this.chi - dc);
const c = cellFn(this.rhi - dc, this.chi + 1);
const d = cellFn(this.rhi - dc, this.chi + 2);
const e = cellFn(this.rlo - 1, this.clo + dc);
const f = cellFn(this.rlo - 2, this.clo + dc);
const g = cellFn(this.rlo + dc, this.clo - 1);
const h = cellFn(this.rlo + dc, this.clo - 2);
return (
f.fracMul(this.y).divExpectPoly(e).sub(
h.fracMul(this.x).divExpectPoly(g)).add(
d.fracMul(this.a).divExpectPoly(c))
).mul(a).fracDiv(this.z);
}
}
}
function makeWallGenerator(inputRowFn) {
let cellFn = null, winFn = null;
const firstWinInColumn = new Map();
const upperWin = new Window();
const winFnInner = (ri, ci) => {
// We want to walk to find the upper right corner of the window. But it's
// possible that this is a lower infinite window. So first, walk up until
// we find the top edge of the window.
const winUp = winFn(ri - 1, ci);
if (winUp != null) return winUp;
// Next, check if there are any known windows on this row, so that we can
// dedupe with them.
if (!firstWinInColumn.has(ri)) {
// This is the first window on this row, so start a new window.
return new Window(true);
}
// Walk towards that window (could be to our left or right). If we hit a
// wall, this is a seperate window. If we hit a window, dedupe with it.
const dc = firstWinInColumn.get(ri) < ci ? -1 : 1;
const winLr = winFn(ri, ci + dc);
if (winLr != null) return winLr;
// The cell in the direction of the first window is a wall. So start a
// new window.
return new Window(true, dc == -1, dc == 1);
};
winFn = cache2D((ri, ci) => {
// console.log('winFn', ri, ci);
// The upper window is infinite, so need to special case it.
if (ri <= -3) return upperWin;
// Check that this is actually a window cell.
if (!cellFn(ri, ci).isZero()) return null;
// Main window hunting logic.
const win = winFnInner(ri, ci);
win.add(ri, ci);
// Update the firstWinInColumn.
if (!firstWinInColumn.has(ri)) firstWinInColumn.set(ri, ci);
return win;
});
const cellFnInner = (ri, ci) => {
if (ri <= -3) return intToPoly(0n);
if (ri == -2) return intToPoly(1n);
if (ri == -1) return inputRowFn(ci);
const a = cellFn(ri - 2, ci);
const c = cellFn(ri - 1, ci);
if (!a.isZero()) {
// Cross rule.
const b = cellFn(ri - 1, ci - 1);
const d = cellFn(ri - 1, ci + 1);
return (c.mul(c).sub(b.mul(d))).divExpectPoly(a);
}
const f = cellFn(ri - 3, ci);
if (!f.isZero()) {
// Long cross rule.
const e = cellFn(ri - 4, ci);
const g = cellFn(ri - 2, ci - 2);
const h = cellFn(ri - 2, ci - 1);
const i = cellFn(ri - 2, ci + 1);
const j = cellFn(ri - 2, ci + 2);
return (g.mul(i).mul(i).add(
h.mul(h).mul(j)).sub(
e.mul(c).mul(c))
).divExpectPoly(f.mul(f));
}
// Find the window that a and f are in.
const win = winFn(ri - 2, ci);
console.assert(win != null);
console.assert(win != upperWin);
console.assert(win == winFn(ri - 2, ci));
console.assert(win.boundUp);
while (true) {
// Are we inside the window? If so, return 0.
const inside = win.isInside(ri, ci);
if (inside == 1) return intToPoly(0n);
if (inside == 0) break;
// If we don't know yet, expand the window left or right.
win.expandHorizontally(cellFn);
}
// Horseshoe rule.
return win.horseshoeRule(ri, ci, cellFn);
};
cellFn = cache2D((ri, ci) => {
const val = cellFnInner(ri, ci);
// console.log('cellFn', ri, ci, val.toString());
return val;
});
return cellFn;
}
function newElement(type, parent, classes = [], text = null) {
const n = document.createElement(type);
if (text != null) n.innerText = text;
for (const cls of classes) n.classList.add(cls);
if (parent != null) parent.appendChild(n);
return n;
}
function newDiv(parent, classes = [], text = null) {
return newElement('div', parent, classes, text);
}
function newTextEdit(parent, classes = [], initValue = null) {
const n = newElement('input', parent, classes);
n.type = 'text';
n.value = initValue;
return n;
}
function emptyDiv(n) {
while (n.hasChildNodes()) n.removeChild(n.lastChild);
}
let fibGenerator = null;
fibGenerator = cache1D((i) => {
if (i == 0) return 0;
if (i == 1) return 1;
if (i > 0) return fibGenerator(i - 1) + fibGenerator(i - 2);
return fibGenerator(i + 2) - fibGenerator(i + 1);
});
function squareGenerator(i) { return i * i; }
function cubeGenerator(i) { return i * i * i; }
function pow2Generator(i) { return 1n << BigInt(Math.abs(i)); }
function pagodaImpl(n) { return (n & ((n & -n) << 1)) ? 1 : 0; }
function pagodaGenerator(n) { return pagodaImpl(n + 1) - pagodaImpl(n - 1); }
class RandomSequence {
constructor() {
this.rand = new Random();
this.seq = [];
}
get(i) {
const j = i >= 0 ? 2 * i : -2 * i - 1;
while (j >= this.seq.length) this.seq.push(this.rand.randi(10, -10));
return this.seq[j];
}
}
function customGenerator(eqn) {
const fn = new Function('n', `return ${eqn};`);
return (i) => fn(i);
}
const randSeq = new RandomSequence();
function getSequenceGenerator() {
if (domSeqSel.value == 'Custom') {
domCustom.classList.remove('hidden');
return customGenerator(domCustom.value);
}
domCustom.classList.add('hidden');
if (domSeqSel.value == 'Fibonacci') return fibGenerator;
if (domSeqSel.value == 'Squares') return squareGenerator;
if (domSeqSel.value == 'Cubes') return cubeGenerator;
if (domSeqSel.value == 'Pow2') return pow2Generator;
if (domSeqSel.value == 'Pagoda') return pagodaGenerator;
if (domSeqSel.value == 'Random') return (i) => randSeq.get(i);
if (domSeqSel.value == 'Zeros') return (i) => 0;
}
const gradient = [
[0, 0, 0],
[94, 53, 177],
[233, 30, 99],
[255, 87, 34],
[255, 193, 7],
[255, 241, 118],
];
function modColorGradient(x) {
const y = x * (gradient.length - 1);
const i = clamp(Math.floor(y), 0, gradient.length - 1);
const j = clamp(i + 1, 0, gradient.length - 1);
const f = y - i;
const r = lerp(gradient[i][0], gradient[j][0], f);
const g = lerp(gradient[i][1], gradient[j][1], f);
const b = lerp(gradient[i][2], gradient[j][2], f);
return `rgb(${r}, ${g}, ${b})`;
}
let userInputs = [];
function buildWall(inputs) {
if (inputs == null) {
inputs = userInputs = [];
}
emptyDiv(domWall);
const withInputs = (fn) => (i) => safeBigInt(inputs[i]) ?? fn(i);
const sequenceGenerator = withInputs(getSequenceGenerator());
const clrMod = safeGet(domColorMod, 1n);
domColorModVal.innerText = clrMod == 1 ? 'Off' : clrMod;
if (clrMod > 1) {
domWall.classList.add('colorModEnabled');
} else {
domWall.classList.remove('colorModEnabled');
}
const inputConv = (fn) => (i) => intToPoly(fn(i));
const wallGen = makeWallGenerator(inputConv(sequenceGenerator));
const wallVals = [];
for (let ri = -1; ri < wallHeight; ++ri) {
const row = [];
for (let ci = 0; ci < wallWidth; ++ci) {
row.push(wallGen(ri, ci).toFrac());
}
wallVals.push(row);
}
// Truncate the wall so there's only one all-zero row at the end.
let finalWallRow = wallHeight;
for (let rip1 = wallVals.length - 1; rip1 >= 1; --rip1) {
if (!wallVals[rip1].every((e) => e.isZero())) break;
if (rip1 + 1 < wallVals.length) {
console.assert(rip1 + 2 == wallVals.length);
wallVals.pop();
finalWallRow = rip1 - 2;
}
}
for (let rip1 = 0; rip1 < wallVals.length; ++rip1) {
const row = newDiv(domWall, ['row']);
for (let ci = 0; ci < wallWidth; ++ci) {
const val = wallVals[rip1][ci];
const str = val.toString();
const isBig = str.length > 3;
const shownVal = isBig ? 'big' : str;
if (rip1 == 0) {
const cell = newTextEdit(row, ['cell', 'in'], str);
cell.addEventListener('change', () => {
userInputs[ci] = safeGet(cell);
buildWall(userInputs);
});
} else {
const cell = newDiv(row, ['cell', 'out'], shownVal);
cell.title = str;
if (clrMod > 1) {
const modVal = Number(val.intMod(clrMod).toNum()) / Number(clrMod);
cell.style.backgroundColor = modColorGradient(modVal);
}
}
}
}
if (finalWallRow == wallHeight) {
domRecur.classList.add('hidden');
domNoRecur.classList.remove('hidden');
} else {
// Run the wall generator using polynomial inputs, to get the recurrence.
const polyInputConv = (fn) => (i) => linearPoly(fn(i), -fn(i - 1));
const wallPolyGen = makeWallGenerator(polyInputConv(sequenceGenerator));
// The recurrence polynomial is any element of the final row. Choose the
// cell on the diagonal, so that we avoid cells off the edge of the grid.
const recurrence = wallPolyGen(finalWallRow, finalWallRow);
const rn = recurrence.degree();
const r2 = recurrence.isZero() ? recurrence : new Polynomial(
recurrence.a.slice(0, rn)).fracDiv(recurrence.term(rn).neg());
domRecur.classList.remove('hidden');
domNoRecur.classList.add('hidden');
domRecurFormula.innerText = 'Sn = ' + r2.toRecurrenceString(rn);
domRecurVals.innerText = r2.applyRecurrence(
sequenceGenerator, rn, wallWidth, wallWidth + 8).join(', ');
}
}
function growWall(dr, dc) {
wallHeight = clamp(wallHeight + dr, 1, 100);
wallWidth = clamp(wallWidth + dc, 1, 100);
buildWall(userInputs);
}
function onLoad() {
domWall = document.getElementById('wall');
domColorMod = document.getElementById('colorMod');
domColorModVal = document.getElementById('colorModVal');
domSeqSel = document.getElementById('sequenceSelect');
domRecur = document.getElementById('recur');
domRecurFormula = document.getElementById('recurFormula');
domRecurVals = document.getElementById('recurVals');
domNoRecur = document.getElementById('noRecur');
domCustom = document.getElementById('custom');
buildWall(null);
domColorMod.addEventListener('change', () => buildWall(userInputs));
domSeqSel.addEventListener('change', () => buildWall(null));
domCustom.addEventListener('change', () => buildWall(userInputs));
}
window.addEventListener('load', onLoad);
</script>
<style>
body {
background-color: #212121;
margin: 0;
}
#head {
background-color: #424242;
width: 100%;
display: flex;
justify-content: space-around;
margin-bottom: 16px;
}
h1, #index {
color: #ffc107;
text-align: center;
font-family: monospace;
font-size: 42px;
flex-grow: 1;
padding: 16px;
margin: 0;
}
#index {
color: #ff5722;
text-decoration: none;
flex-grow: 0;
}
h2 {
color: #ff5722;
font-family: monospace;
}
a {
color: #ffc107;
font-family: monospace;
font-size: 16px;
cursor: pointer;
text-decoration: underline;
}
#wrap {
padding: 0 16px;
color: #f5f5f5;
font-family: monospace;
font-size: 16px;
display: flex;
flex-direction: row;
gap: 16px;
}
#left {
max-width: 400px;
width: 25%;
}
#right {
flex-grow: 1;
display: flex;
justify-content: space-around;
}
.shh {
opacity: 15%;
}
#wall {
display: flex;
flex-direction: column;
border-top: 1px solid #424242;
}
.row {
height: 24px;
display: flex;
border-right: 1px solid #424242;
}
.cell {
width: 32px;
padding: 1px;
border-bottom: 1px solid #424242;
border-left: 1px solid #424242;
border-top: 0;
border-right: 0;
}
.colorModEnabled .cell.out {
color: #aaaaaa66;
}
.hidden {
display: none;
}
#recurFormula {
color: #8bc34a;
}
#recurVals {
color: #2196f3;
}
.button {
color: #ffc107;
cursor: pointer;
user-select: none;
}
</style>
</head>
<body>
<div id="head">
<a id="index" href="index.html"><</a>
<h1>Number Wall</h1>
</div>
<div id="wrap">
<div id="left">
Number walls are a tool for analyzing integer sequences. The sequence is
input into the top row, and the other rows of numbers are calculated
according to the cross rule (by solving for D):<br/><br/>
<img src="res/cross_rule.svg"/><br/><br/>
There are other more elaborate rules for cases where zeros make the cross
rule useless (when C is 0). For more details, see this
<a href="https://youtu.be/NO1_-qptr6c">Mathologer video</a>.<br/><br/>
UI note: For numbers that don't fit, I just write "big", but you can hover
over the cell to see the actual value.<br/><br/>
Sequence:
<select id="sequenceSelect">
<option value="Fibonacci">Fibonacci</option>
<option value="Squares">Squares</option>
<option value="Cubes">Cubes</option>
<option value="Pagoda">Pagoda</option>
<option value="Random">Random</option>
<option value="Custom">Custom JS</option>
</select>
<input type="text" id="custom" value="n ^ 4"/>
<br/><br/>
Color modulo:
<span id="colorModVal">Off</span>
<input type="range" id="colorMod" min="1" max="10" value="1"/>
<br/><br/>
Grow/shrink wall:
<span class="button" onclick="growWall(-1, 0)">🡅</span>