# Playing the Double Base

Here’s some mathematical nonsense:

10 > 12
100 > 122
1000 > 1222

How can 1000 > 1222? Well, it makes perfect sense in what you might call a double base. In this base, every number is identified by a unique string of digits, but the strings don’t behave as they do in a standard base.

To see how this double base works, first look at 9 in standard base 2. To generate the binary digits from right to left, you follow the procedure x mod 2 and x = x div 2, where (x mod 2) returns the remainder when x is divided by 2 and (x div 2) divides x by 2 and discards the remainder:

9 mod 2 = 1 → ...1
9 div 2 = 4
4 mod 2 = 0 → ..01
4 div 2 = 2
2 mod 2 = 0 → .001
2 div 2 = 1
1 mod 2 = 1 → 1001

So 9[b=10] = 1001[b=2]. To adapt the procedure to base 3, simply use x mod 3 and x = x div 3:

32 mod 3 = 2 → ...2
32 div 3 = 10
10 mod 3 = 1 → ..12
10 div 3 = 3
3 mod 3 = 0 → .012
3 div 3 = 1
1 mod 3 = 1 → 1012

So 32[b=10] = 1012[b=3].

But what happens if you mix bases and use (x mod 3) and (x div 2), like this?:

2 mod 3 = 2 → .2
2 div 2 = 1
1 mod 3 = 1 → 12

3 mod 3 = 0 → .0
3 div 2 = 1
1 mod 3 = 1 → 10

So 10 > 12, i.e. 10[b=3,2] > 12[b=3,2].

5 mod 3 = 2 → ..2
5 div 2 = 2
2 mod 3 = 2 → .22
2 div 2 = 1
1 mod 3 = 1 → 122

6 mod 3 = 0 → ..0
6 div 2 = 3
3 mod 3 = 0 → .00
3 div 2 = 1
1 mod 3 = 1 → 100

So 100 > 122.

11 mod 3 = 2 → ...2
11 div 2 = 5
5 mod 3 = 2 → ..22
5 div 2 = 2
2 mod 3 = 2 → .222
2 div 2 = 1
1 mod 3 = 1 → 1222

12 mod 3 = 0 → …0
12 div 2 = 6
6 mod 3 = 0 → ..00
6 div 2 = 3
3 mod 3 = 0 → .000
3 div 2 = 1
1 mod 3 = 1 → 1000

And 1000 > 1222. Here are numbers 1 to 32 in this double base:

1 = 1
12 = 2
10 = 3
121 = 4
122 = 5
100 = 6
101 = 7
1212 = 8
1210 = 9
1221 = 10
1222 = 11
1000 = 12
1001 = 13
1012 = 14
1010 = 15
12121 = 16
12122 = 17
12100 = 18
12101 = 19
12212 = 20
12210 = 21
12221 = 22
12222 = 23
10000 = 24
10001 = 25
10012 = 26
10010 = 27
10121 = 28
10122 = 29
10100 = 30
10101 = 31
121212 = 32

Given a number represented in this mixed base, how do you extract the underlying n? Suppose the number takes the form n = (digit[1]..digit[di]), where digit[1] is the first and leftmost digit and digit[di] the final and rightmost digit. Then this algorithm will extract n:

n = 1
for i = 2 to di
..n = n * 2
..while n mod 3 ≠ digit[i]
....n = n + 1
..endwhile
next i
print n

For example, suppose n = 12212[b=3,2]. Then di = 5 and the algorithm will work like this:

n = 1
n = n * 2 = 2.
2 mod 3 = 2 = digit[2]
2 * 2 = 4
4 mod 3 = 1 ≠ digit[3]
5 mod 3 = 2 = digit[3]
5 * 2 = 10
10 mod 3 = 1 = digit[4]
10 * 2 = 20
20 mod 3 = 2 = digit[5]

Therefore 12212[b=3,2] = 20[b=10].

Now try some more mathematical nonsense:

21 > 100
111 > 1,000
1,001 > 10,000
10,001 > 100,000

How can numbers with d digits be greater than numbers with d+1 digits? Easily. In this incremental base, the base adjusts itself as the digits are generated, like this:

5 mod 2 = 1 → .1
5 div 2 = 2
2 mod (2 + 1) = 2 mod 3 = 2 → 21

The first digit generated is 1, so the base increases to (2 + 1) = 3 for the second digit. Compare the procedure when n = 4:

4 mod 2 = 0 → ..0
4 div 2 = 2
2 mod 2 = 0 → .00
2 div 2 = 1
1 mod 2 = 1 → 100

So 21 > 100, because 4 is a power of 2 and all the digits generated by (x mod 2) are 0 except the final and leftmost. 2 + 0 = 2. Now try n = 33:

33 mod 2 = 1 → ...1
33 div 2 = 16
16 mod (2+1) = 16 mod 3 = 1 → ..11
16 div 3 = 5
5 mod (3+1) = 5 mod 4 = 1 → .111
5 div 4 = 1
1 mod (4+1) = 1 mod 5 = 1.

33[b=10] = 1111[b=2,3,4,5].

Here are numbers 1 to 60 in this incremental base (note how 21 > 100, 111 > 1000, 1001 > 10000 and 10001 > 100000):

1 = 1
10 = 2
11 = 3
100 = 4*
21 = 5*
110 = 6
101 = 7
1000 = 8*
111 = 9*
210 = 10
121 = 11
1100 = 12
201 = 13
1010 = 14
211 = 15
10000 = 16*
221 = 17
1110 = 18
1001 = 19*
2100 = 20
311 = 21
1210 = 22
321 = 23
11000 = 24
1101 = 25
2010 = 26
1011 = 27
10100 = 28
421 = 29
2110 = 30
1201 = 31
100000 = 32*
1111 = 33
2210 = 34
1021 = 35
11100 = 36
2001 = 37
10010 = 38
1211 = 39
21000 = 40
1121 = 41
3110 = 42
2101 = 43
12100 = 44
1311 = 45
3210 = 46
1221 = 47
110000 = 48
2201 = 49
11010 = 50
2011 = 51
20100 = 52
1321 = 53
10110 = 54
10001 = 55*
101000 = 56
2111 = 57
4210 = 58
1421 = 59
21100 = 60

And here are numbers 256 to 270 (Note how 8,421 > 202,100 > 100,000,000):

100000000 = 256*
11221 = 257
101110 = 258
32101 = 259
202100 = 260*
13311 = 261
41210 = 262
10321 = 263
1111000 = 264
24201 = 265
131010 = 266
23011 = 267
320100 = 268
8421 = 269*
52110 = 270

Extracting n from a number represented in this incremental base is trickier than for the double base using (x mod 3) and (x div 2). To see how to do it, examine 11221[b=incremental]. The fifth and rightmost digit is 1, so the base increases to (2 + 1) = 3 for the fourth digit, which is 2. The base increases to (3 + 2) = 5 for the third digit, which is 2 again. The base increases to (5 + 2) = 7 for the second digit, 1. But the first and rightmost digit, 1, represents (x div 7) mod (7 + 1 = 8). So n can be extracted like this:

digit[1] * 7 = 1 * 7 = 7
7 mod 7 = 0 ≠ digit[2]
8 mod 7 = 1 = digit[2]
8 * 5 = 40
40 mod 5 = 0 ≠ digit[3]
41 mod 5 = 1 ≠ digit[3]
42 mod 5 = 2 = digit[3]
42 * 3 = 126
126 mod 3 = 0 ≠ digit[4]
127 mod 3 = 1 ≠ digit[4]
128 mod 3 = 2 = digit[4]
128 * 2 = 256
256 mod 2 = 0 ≠ digit[5]
257 mod 2 = 1 = digit[5]

So 11221[b=8,7,5,3,2] = 257[b=10].

Now try 8421[b=incremental]. The fourth and rightmost digit is 1, so the base increases to (2 + 1) = 3 for the third digit, which is 2. The base increases to (3 + 2) = 5 for the second digit, 4. But the first and rightmost digit, 8, represents (x div 5) mod (5 + 4 = 9). So n can be extracted like this:

digit[1] * 5 = 8 * 5 = 40
40 mod 5 = 0 ≠ digit[2]
41 mod 5 = 1 ≠ digit[2]
42 mod 5 = 2 ≠ digit[2]
43 mod 5 = 3 ≠ digit[2]
44 mod 5 = 4 = digit[2]
44 * 3 = 132
132 mod 3 = 0 ≠ digit[3]
133 mod 3 = 1 ≠ digit[3]
134 mod 3 = 2 = digit[3]
134 * 2 = 268
268 mod 2 = 0 ≠ digit[4]
269 mod 2 = 1 = digit[4]

So 8421[b=9,5,3,2] = 269[b=10].

# Summus

I’m interested in digit-sums and in palindromic numbers. Looking at one, I found the other. It started like this: 9^2 = 81 and 9 = 8 + 1, so digitsum(9^1) = digitsum(9^2). I wondered how long such a sequence of powers could be (excluding powers of 10). I quickly found that the digit-sum of 468 is equal to the digit-sum of its square and cube:

digsum(468) = digsum(219024) = digsum(102503232)

But I couldn’t find any longer sequence, although plenty of other numbers are similar to 468:

digsum(585) = digsum(342225) = digsum(200201625)
digsum(4680) = digsum(21902400) = digsum(102503232000)
digsum(5850) = digsum(34222500) = digsum(200201625000)
digsum(5851) = digsum(34234201) = digsum(200304310051)
digsum(5868) = digsum(34433424) = digsum(202055332032)
digsum(28845) = digsum(832034025) = digsum(24000021451125) […]
digsum(589680) = digsum(347722502400) = digsum(205045005215232000)

What about other bases? First came this sequence:

digsum(2) = digsum(11) (base = 3) (highest power = 2)

Then these:

digsum(4) = digsum(22) = digsum(121) (b=7) (highest power = 3)
digsum(8) = digsum(44) = digsum(242) = digsum(1331) (b=15) (hp=4)
digsum([16]) = digsum(88) = digsum(484) = digsum(2662) = digsum(14641) (b=31) (hp=5)

The pattern continues (a number between square brackets represents a single digit in the base):

digsum([32]) = digsum([16][16]) = digsum(8[16]8) = digsum(4[12][12]4) = digsum(28[12]82) = digsum(15[10][10]51) (b=63) (hp=6)
digsum([64]) = digsum([32][32]) = digsum([16][32][16]) = digsum(8[24][24]8) = digsum(4[16][24][16]4) = digsum(2[10][20][20][10]2) = digsum(16[15][20][15]61) (b=127) (hp=7)
digsum([128]) = digsum([64][64]) = digsum([32][64][32]) = digsum([16][48][48][16]) = digsum(8[32][48][32]8) = digsum(4[20][40][40][20]4) = digsum(2[12][30][40][30][12]2) = digsum(17[21][35][35][21]71) (b=255) (hp=8)
digsum([256]) = digsum([128][128]) = digsum([64][128][64]) = digsum([32][96][96][32]) = digsum([16][64][96][64][16]) = digsum(8[40][80][80][40]8) = digsum(4[24][60][80][60][24]4) = digsum(2[14][42][70][70][42][14]2) = digsum(18[28][56][70][56][28]81) (b=511) (hp=9)

After this, I looked at sequences in which n(i) = n(i-1) + digitsum(n(i-1)). How long could digitsum(n(i)) be greater than or equal to digitsum(n(i-1))? In base 10, I found these sequences:

1 (digitsum=1) → 2 → 4 → 8 → 16 (sum=7) (count=4) (base=10)
9 → 18 (sum=9) → 27 (s=9) → 36 (s=9) → 45 (s=9) → 54 (s=9) → 63 (s=9) → 72 (s=9) → 81 (s=9) → 90 (s=9) → 99 (s=18) → 117 (s=9) (c=11) (b=10)
801 (s=9) → 810 (s=9) → 819 (s=18) → 837 (s=18) → 855 (s=18) → 873 (s=18) → 891 (s=18) → 909 (s=18) → 927 (s=18) → 945 (s=18) → 963 (s=18) → 981 (s=18) → 999 (s=27) → 1026 (s=9) (c=13)

Base 2 does better:

1 → 10 (s=1) → 11 (s=2) → 101 (s=2) → 111 (s=3) → 1010 (s=2) (c=5) (b=2)
16 = 10000 (s=1) → 10001 (s=2) → 10011 (s=3) → 10110 (s=3) → 11001 (s=3) → 11100 (s=3) → 11111 (s=5) → 100100 (s=2) (c=7) (b=2)
962 = 1111000010 (s=5) → 1111000111 (s=7) → 1111001110 (s=7) → 1111010101 (s=7) → 1111011100 (s=7) → 1111100011 (s=7) → 1111101010 (s=7) → 1111110001 (s=7) → 1111111000 (s=7) → 1111111111 (s=10) → 10000001001 (s=3) (c=10) (b=2)
524047 = 1111111111100001111 (s=15) → 1111111111100011110 (s=15) → 1111111111100101101 (s=15) → 1111111111100111100 (s=15) → 1111111111101001011 (s=15) → 1111111111101011010 (s=15) → 1111111111101101001(s=15) → 1111111111101111000 (s=15) → 1111111111110000111 (s=15) → 1111111111110010110 (s=15) → 1111111111110100101 (s=15) → 1111111111110110100 (s=15) → 1111111111111000011 (s=15) → 1111111111111010010 (s=15) → 1111111111111100001 (s=15) → 1111111111111110000 (s=15) → 1111111111111111111 (s=19) → 10000000000000010010 (s=3) (c=17) (b=2)

The best sequence I found in base 3 is shorter than in base 10, but there are more sequences:

1 → 2 → 11 (s=2) → 20 (s=2) → 22 (s=4) → 110 (s=2) (c=5) (b=3)
31 = 1011 (s=3) → 1021 (s=4) → 1102 (s=4) → 1120 (s=4) → 1201 (s=4) → 1212 (s=6) → 2002 (s=4) (c=6) (b=3)
54 = 2000 (s=2) → 2002 (s=4) → 2020 (s=4) → 2101 (s=4) → 2112 (s=6) → 2202 (s=6) → 2222 (s=8) → 10021(s=4) (c=7) (b=3)
432 = 121000 (s=4) → 121011 (s=6) → 121101 (s=6) → 121121 (s=8) → 121220 (s=8) → 122012 (s=8) → 122111 (s=8) → 122210 (s=8) → 200002 (s=4) (c=8) (b=3)
648 = 220000 (s=4) → 220011 (s=6) → 220101 (s=6) → 220121 (s=8) → 220220 (s=8) → 221012 (s=8) → 221111 (s=8) → 221210 (s=8) → 222002 (s=8) → 222101 (s=8) → 222200 (s=8) → 222222 (s=12) → 1000102 (s=4) (c=12) (b=3)

And what about sequences in which digitsum(n(i)) is always greater than digitsum(n(i-1))? Base 10 is disappointing:

1 → 2 → 4 → 8 → 16 (sum=7) (count=4) (base=10)
50 (s=5) → 55 (s=10) → 65 (s=11) → 76 (s=13) → 89 (s=17) → 106 (s=7) (c=5) (b=10)

Some other bases do better:

2 = 10 (s=1) → 11 (s=2) → 101 (s=2) (c=2) (b=2)
4 = 100 (s=1) → 101 (s=2) → 111 (s=3) → 1010 (s=2) (c=3) (b=2)
240 = 11110000 (s=4) → 11110100 (s=5) → 11111001 (s=6) → 11111111 (s=8) → 100000111 (s=4) (c=4) (b=2)

1 → 2 → 11 (s=2) (c=2) (b=3)
19 = 201 (s=3) → 211 (s=4) → 222 (s=6) → 1012 (s=4) (c=3) (b=3)
58999 = 2222221011 (s=15) → 2222221201 (s=16) → 2222222022 (s=18) → 2222222222 (s=20) → 10000000201 (s=4) (c=4) (b=3)

1 → 2 → 10 (s=1) (c=2) (b=4)
4 = 10 (s=1) → 11 (s=2) → 13 (s=4) → 23 (s=5) → 100 (s=1) (c=4) (b=4)
977 = 33101 (s=8) → 33121 (s=10) → 33203 (s=11) → 33232 (s=13) → 33323 (s=14) → 100021 (s=4) (c=5) (b=4)

1 → 2 → 4 → 13 (s=4) (c=3) (b=5)
105 = 410 (s=5) → 420 (s=6) → 431 (s=8) → 444 (s=12) → 1021 (s=4) (c=4) (b=5)

1 → 2 → 4 → 12 (s=3) (c=3) (b=6)
13 = 21 (s=3) → 24 (s=6) → 34 (s=7) → 45 (s=9) → 102 (s=3) (c=4) (b=6)
396 = 1500 (s=6) → 1510 (s=7) → 1521 (s=9) → 1534 (s=13) → 1555 (s=16) → 2023 (s=7) (c=5) (b=6)

1 → 2 → 4 → 11 (s=2) (c=3) (b=7)
121 = 232 (s=7) → 242 (s=8) → 253 (s=10) → 266 (s=14) → 316 (s=10) (c=4) (b=7)
205 = 412 (s=7) → 422 (s=8) → 433 (s=10) → 446 (s=14) → 466 (s=16) → 521 (s=8) (c=5) (b=7)

1 → 2 → 4 → 10 (s=1) (c=3) (b=8)
8 = 10 (s=1) → 11 (s=2) → 13 (s=4) → 17 (s=8) → 27 (s=9) → 40 (s=4) (c=5) (b=8)
323 = 503 (s=8) → 513 (s=9) → 524 (s=11) → 537 (s=15) → 556 (s=16) → 576 (s=18) → 620 (s=8) (c=6) (b=8)

1 → 2 → 4 → 8 → 17 (s=8) (c=4) (b=9)
6481 = 8801 (s=17) → 8820 (s=18) → 8840 (s=20) → 8862 (s=24) → 8888 (s=32) → 10034 (s=8) (c=5) (b=9)

1 → 2 → 4 → 8 → 16 (s=7) (c=4) (b=10)
50 (s=5) → 55 (s=10) → 65 (s=11) → 76 (s=13) → 89 (s=17) → 106 (s=7) (c=5) (b=10)

1 → 2 → 4 → 8 → 15 (s=6) (c=4) (b=11)
1013 = 841 (s=13) → 853 (s=16) → 868 (s=22) → 888 (s=24) → 8[10][10] (s=28) → 925 (s=16) (c=5) (b=11)

1 → 2 → 4 → 8 → 14 (s=5) (c=4) (b=12)
25 = 21 (s=3) → 24 (s=6) → 2[10] (s=12) → 3[10] (s=13) → 4[11] (s=15) → 62 (s=8) (c=5) (b=12)
1191 = 833 (s=14) → 845 (s=17) → 85[10] (s=23) → 879 (s=24) → 899 (s=26) → 8[11][11] (s=30) → 925 (s=16) (c=6) (b=12)

1 → 2 → 4 → 8 → 13 (s=4) (c=4) (b=13)
781 = 481 (s=13) → 491 (s=14) → 4[10]2 (s=16) → 4[11]5 (s=20) → 4[12][12] (s=28) → 521 (s=8) (c=5) (b=13)
19621 = 8[12]14 (s=25) → 8[12]33 (s=26) → 8[12]53 (s=28) → 8[12]75 (s=32) → 8[12]9[11] (s=40) → 8[12][12][12] (s=44) → 9034 (s=16) (c=6) (b=13)

1 → 2 → 4 → 8 → 12 (s=3) (c=4) (b=14)
72 = 52 (s=7) → 59 (s=14) → 69 (s=15) → 7[10] (s=17) → 8[13] (s=21) → [10]6 (s=16) (c=5) (b=14)
1275 = 671 (s=14) → 681 (s=15) → 692 (s=17) → 6[10]5 (s=21) → 6[11][12] (s=29) → 6[13][13] (s=32) → 723 (s=12) (c=6) (b=14)
19026 = 6[13]10 (s=20) → 6[13]26 (s=27) → 6[13]45 (s=28) → 6[13]65 (s=30) → 6[13]87 (s=34) → 6[13][10][13] (s=42) → 6[13][13][13] (s=45) → 7032 (s=12) (c=7) (b=14)

1 → 2 → 4 → 8 → 11 (s=2) (c=4) (b=15)
603 = 2[10]3 (s=15) → 2[11]3 (s=16) → 2[12]4 (s=18) → 2[13]7 (s=22) → 2[14][14] (s=30) → 31[14] (s=18) (c=5) (b=15)
1023 = 483 (s=15) → 493 (s=16) → 4[10]4 (s=18) → 4[11]7 (s=22) → 4[12][14] (s=30) → 4[14][14] (s=32) → 521 (s=8) (c=6) (b=15)
1891 = 861 (s=15) → 871 (s=16) → 882 (s=18) → 895 (s=22) → 8[10][12] (s=30) → 8[12][12] (s=32) → 8[14][14] (s=36) → 925 (s=16) (c=7) (b=15)

1 → 2 → 4 → 8 → 10 (s=1) (c=4) (b=16)
16 = 10 (s=1) → 11 (s=2) → 13 (s=4) → 17 (s=8) → 1[15] (s=16) → 2[15] (s=17) → 40 (s=4) (c=6) (b=16)
1396 = 574 (s=16) → 584 (s=17) → 595 (s=19) → 5[10]8 (s=23) → 5[11][15] (s=31) → 5[13][14] (s=32) → 5[15][14] (s=34) → 620 (s=8) (c=7) (b=16)
2131 = 853 (s=16) → 863 (s=17) → 874 (s=19) → 887 (s=23) → 89[14] (s=31) → 8[11][13] (s=32) → 8[13][13] (s=34) → 8[15][15] (s=38) → 925 (s=16) (c=8) (b=16)

1 → 2 → 4 → 8 → [16] (s=16) → 1[15] (s=16) (c=5) (b=17)

1 → 2 → 4 → 8 → [16] (s=16) → 1[14] (s=15) (c=5) (b=18)
5330 = [16]82 (s=26) → [16]9[10] (s=35) → [16][11]9 (s=36) → [16][13]9 (s=38) → [16][15][11] (s=42) → [16][17][17] (s=50) → [17]2[13] (s=32) (c=6) (b=18)

1 → 2 → 4 → 8 → [16] (s=16) → 1[13] (s=14) (c=5) (b=19)
116339 = [16][18]52 (s=41) → [16][18]75 (s=46) → [16][18]9[13] (s=56) → [16][18][12][12] (s=58) → [16][18][15][13] (s=62) → [16][18][18][18] (s=70) → [17]03[12] (s=32) (c=6) (b=19)

1 → 2 → 4 → 8 → [16] (s=16) → 1[12] (s=13) (c=5) (b=20)
100 = 50 (s=5) → 55 (s=10) → 5[15] (s=20) → 6[15] (s=21) → 7[16] (s=23) → 8[19] (s=27) → [10]6 (s=16) (c=6) (b=20)
135665 = [16][19]35 (s=43) → [16][19]58 (s=48) → [16][19]7[16] (s=58) → [16][19][10][14] (s=59) → [16][19][13][13] (s=61) → [16][19][16][14] (s=65) → [16][19][19][19] (s=73) → [17]03[12] (s=32) (c=7) (b=20)

# N-route

In maths, one thing leads to another. I wondered whether, in a spiral of integers, any number was equal to the digit-sum of the numbers on the route traced by moving to the origin first horizontally, then vertically. To illustrate the procedure, here is a 9×9 integer spiral containing 81 numbers:

```| 65 | 64 | 63 | 62 | 61 | 60 | 59 | 58 | 57 |
| 66 | 37 | 36 | 35 | 34 | 33 | 32 | 31 | 56 |
| 67 | 38 | 17 | 16 | 15 | 14 | 13 | 30 | 55 |
| 68 | 39 | 18 | 05 | 04 | 03 | 12 | 29 | 54 |
| 69 | 40 | 19 | 06 | 01 | 02 | 11 | 28 | 53 |
| 70 | 41 | 20 | 07 | 08 | 09 | 10 | 27 | 52 |
| 71 | 42 | 21 | 22 | 23 | 24 | 25 | 26 | 51 |
| 72 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 |
| 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 |
```

Take the number 21, which is three places across and up from the bottom left corner of the spiral. The route to the origin contains the numbers 21, 22, 23, 8 and 1, because first you move right two places, then up two places. And 21 is what I call a route number, because 21 = 3 + 4 + 5 + 8 + 1 = digitsum(21) + digitsum(22) + digitsum(23) + digitsum(8) + digitsum(1). Beside the trivial case of 1, there are two more route numbers in the spiral:

58 = 13 + 14 + 6 + 7 + 7 + 6 + 4 + 1 = digitsum(58) + digitsum(59) + digitsum(60) + digitsum(61) + digitsum(34) + digitsum(15) + digitsum(4) + digitsum(1).

74 = 11 + 12 + 13 + 14 + 10 + 5 + 8 + 1 = digitsum(74) + digitsum(75) + digitsum(76) + digitsum(77) + digitsum(46) + digitsum(23) + digitsum(8) + digitsum(1).

Then I wondered about other possible routes to the origin. Think of the origin as one corner of a rectangle and the number being tested as the diagonal corner. Suppose that you always move away from the starting corner, that is, you always move up or right (or up and left, and so on, depending on where the corners lie). In a x by y rectangle, how many routes are there between the diagonal corners under those conditions?

It’s an interesting question, but first I’ve looked at the simpler case of an n by n square. You can encode each route as a binary number, with 0 representing a vertical move and 1 representing a horizontal move. The problem then becomes equivalent to finding the number of distinct ways you can arrange equal numbers of 1s and 0s. If you use this method, you’ll discover that there are two routes across the 2×2 square, corresponding to the binary numbers 01 and 10:

Across the 3×3 square, there are six routes, corresponding to the binary numbers 0011, 0101, 0110, 1001, 1010 and 1100:

Across the 4×4 square, there are twenty routes:

(Please open in new window if it fails to animate)

Across the 5×5 square, there are 70 routes:

(Please open in new window etc)

Across the 6×6 and 7×7 squares, there are 252 and 924 routes:

After that, the routes quickly increase in number. This is the list for n = 1 to 14:

1, 2, 6, 20, 70, 252, 924, 3432, 12870, 48620, 184756, 705432, 2704156, 10400600… (see A000984 at the Online Encyclopedia of Integer Sequences)

After that you can vary the conditions. What if you can move not just vertically and horizontally, but diagonally, i.e. vertically and horizontally at the same time? Now you can encode the route with a ternary number, or number in base 3, with 0 representing a vertical move, 1 a horizontal move and 2 a diagonal move. As before, there is one route across a 1×1 square, but there are three across a 2×2, corresponding to the ternary numbers 01, 2 and 10:

There are 13 routes across a 3×3 square, corresponding to the ternary numbers 0011, 201, 021, 22, 0101, 210, 1001, 120, 012, 102, 0110, 1010, 1100:

And what about cubes, hypercubes and higher?

# Prime Climb Time

The third prime is equal to the sum of the first and second primes: 2 + 3 = 5. After that, for obvious reasons, the prime-sum climbs much more rapidly than the primes themselves:

```2, 3, 05, 07, 11, 13, 17, 19, 023, 029...
2, 5, 10, 17, 28, 41, 58, 77, 100, 129...
```

But what if you use digit-sum(p1..pn), i.e., the sum of the digits of the primes from the first to the nth? For example, the digit-sum(p1..p5) = 2 + 3 + 5 + 7 + 1+1 = 19, whereas the sum(p1..p5) = 2 + 3 + 5 + 7 + 11 = 28. Using the digit-sums of the primes, the comparison now looks like this:

```2, 3, 05, 07, 11, 13, 17, 19, 23, 29...
2, 5, 10, 17, 19, 23, 31, 41, 46, 57...
```

The sum climbs more slowly, but still too fast. So what about a different base? In base-2, the digit-sum(p1..p3) = (1+0) + (1+1) + (1+0+1) = 1 + 2 + 2 = 5. The comparison looks like this:

```2, 3, 05, 07, 11, 13, 17, 19, 23, 29...
1, 3, 05, 08, 11, 14, 16, 19, 23, 27...
```

For primes 3, 5, 11, 19, and 23, p = digit-sum(primes <= p) in base-2. But the cumulative digit-sum soon begins to climb too slowly:

```2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271...

1, 3, 5, 8, 11, 14, 16, 19, 23, 27, 32, 35, 38, 42, 47, 51, 56, 61, 64, 68, 71, 76, 80, 84, 87, 091, 096, 101, 106, 110, 117, 120, 123, 127, 131, 136, 141, 145, 150, 155, 160, 165, 172, 175, 179, 184, 189, 196, 201, 206, 211, 218, 223, 230, 232, 236, 240, 245...```

```2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59...
2, 3, 6, 9, 12, 15, 20, 23, 28, 31, 34, 37, 42, 47, 52, 59, 64...
```

In base-3, for p = 2, 3 and 37, p = digit-sum(primes <= p), while for p = 23, 31, 47 and 59, p = digit-sum(primes < p), like this:

2 = 2.
3 = 2 + (1+0).
37 = 2 + (1+0) + (1+2) + (2+1) + (1+0+2) + (1+1+1) + (1+2+2) + (2+0+1) + (2+1+2) + (1+0+0+2) + (1+0+1+1) + (1+1+0+1) = 2 + 1 + 3 + 3 + 3 + 3 + 5 + 3 + 5 + 3 + 3 + 3.

23 = 2 + (1+0) + (1+2) + (2+1) + (1+0+2) + (1+1+1) + (1+2+2) + (2+0+1) = 2 + 1 + 3 + 3 + 3 + 3 + 5 + 3.
31 = 2 + (1+0) + (1+2) + (2+1) + (1+0+2) + (1+1+1) + (1+2+2) + (2+0+1) + (2+1+2) + (1+0+0+2) = 2 + 1 + 3 + 3 + 3 + 3 + 5 + 3 + 5 + 3.
47 = 2 + (1+0) + (1+2) + (2+1) + (1+0+2) + (1+1+1) + (1+2+2) + (2+0+1) + (2+1+2) + (1+0+0+2) + (1+0+1+1) + (1+1+0+1) + (1+1+1+2) + (1+1+2+1) = 2 + 1 + 3 + 3 + 3 + 3 + 5 + 3 + 5 + 3 + 3 + 3 + 5 + 5.
59 = 2 + (1+0) + (1+2) + (2+1) + (1+0+2) + (1+1+1) + (1+2+2) + (2+0+1) + (2+1+2) + (1+0+0+2) + (1+0+1+1) + (1+1+0+1) + (1+1+1+2) + (1+1+2+1) + (1+2+0+2) + (1+2+2+2) = 2 + 1 + 3 + 3 + 3 + 3 + 5 + 3 + 5 + 3 + 3 + 3 + 5 + 5 + 5 + 7.

This carries on for a long time. For these primes, p = digit-sum(primes < p):

23, 31, 47, 59, 695689, 698471, 883517, 992609, 992737, 993037, 1314239, 1324361, 1324571, 1326511, 1327289, 1766291, 3174029

And for these primes, p = digit-sum(primes <= p):

3, 37, 695663, 695881, 1308731, 1308757, 1313153, 1314301, 1326097, 1766227, 3204779, 14328191

Now try the cumulative digit-sum in base-4:

```2, 3, 5, 07, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59...
2, 5, 7, 11, 16, 20, 22, 26, 31, 36, 43, 47, 52, 59, 67, 72, 80...
```

The sum of digits climbs too fast. Base-3 is the Goldilocks base, climbing neither too slowly, like base-2, nor too fast, like all bases greater than 3.