# God Give Me Benf’

In “Wake the Snake”, I looked at the digits of powers of 2 and mentioned a fascinating mathematical phenomenon known as Benford’s law, which governs — in a not-yet-fully-explained way — the leading digits of a wide variety of natural and human statistics, from the lengths of rivers to the votes cast in elections. Benford’s law also governs a lot of mathematical data. It states, for example, that the first digit, d, of a power of 2 in base b (except b = 2, 4, 8, 16…) will occur with the frequency logb(1 + 1/d). In base 10, therefore, Benford’s law states that the digits 1..9 will occur with the following frequencies at the beginning of 2^p:

1: 30.102999%
2: 17.609125%
3: 12.493873%
4: 09.691001%
5: 07.918124%
6: 06.694678%
7: 05.799194%
8: 05.115252%
9: 04.575749%

Here’s a graph of the actual relative frequencies of 1..9 as the leading digit of 2^p (open images in a new window if they appear distorted):

And here’s a graph for the predicted frequencies of 1..9 as the leading digit of 2^p, as calculated by the log(1+1/d) of Benford’s law:

The two graphs agree very well. But Benford’s law applies to more than one leading digit. Here are actual and predicted graphs for the first two leading digits of 2^p, 10..99:

And actual and predicted graphs for the first three leading digits of 2^p, 100..999:

But you can represent the leading digit of 2^p in another way: using an adaptation of the famous Ulam spiral. Suppose powers of 2 are represented as a spiral of squares that begins like this, with 2^0 in the center, 2^1 to the right of center, 2^2 above 2^1, and so on:

←←←⮲
432↑
501↑
6789

If the digits of 2^p start with 1, fill the square in question; if the digits of 2^p don’t start with 1, leave the square empty. When you do this, you get this interesting pattern (the purple square at the very center represents 2^0):

Ulam-like power-spiral for 2^p where 1 is the leading digit

Here’s a higher-resolution power-spiral for 1 as the leading digit:

Power-spiral for 2^p, leading-digit = 1 (higher resolution)

And here, at higher resolution still, are power-spirals for all the possible leading digits of 2^p, 1..9 (some spirals look very similar, so you have to compare those ones carefully):

Power-spiral for 2^p, leading-digit = 1 (very high resolution)

Power-spiral for 2^p, leading-digit = 2

Power-spiral for 2^p, ld = 3

Power-spiral for 2^p, ld = 4

Power-spiral for 2^p, ld = 5

Power-spiral for 2^p, ld = 6

Power-spiral for 2^p, ld = 7

Power-spiral for 2^p, ld = 8

Power-spiral for 2^p, ld = 9

Power-spiral for 2^p, ld = 1..9 (animated)

Now try the power-spiral of 2^p, ld = 1, in some other bases:

Power-spiral for 2^p, leading-digit = 1, base = 9

Power-spiral for 2^p, ld = 1, b = 15

You can also try power-spirals for other n^p. Here’s 3^p:

Power-spiral for 3^p, ld = 1, b = 10

Power-spiral for 3^p, ld = 2, b = 10

Power-spiral for 3^p, ld = 1, b = 4

Power-spiral for 3^p, ld = 1, b = 7

Power-spiral for 3^p, ld = 1, b = 18

Elsewhere Other-Accessible…

Wake the Snake — an earlier look at the digits of 2^p

# Spiral Artefact

What’s the next number in this sequence of integers?

5, 14, 19, 23, 28, 32, 37, 41, 46, 50, 55... (A227793 at the OEIS)

It shouldn’t be hard to work out that it’s 64 — the sum-of-digits of n is divisible by 5, i.e., digsum(n) mod 5 = 0. Now try summing the numbers in that sequence:

5 + 14 = 19
19 + 19 = 38
38 + 23 = 61
61 + 28 = 89
89 + 32 = 121
121 + 37 = 158
158 + 41 = 199
199 + 46 = 245
[...]

Here are the cumulative sums as another sequence:

5, 19, 38, 61, 89, 121, 158, 199, 245, 295, 350, 414, 483, 556, 634, 716, 803, 894, 990, 1094, 1203, 1316, 1434, 1556, 1683, 1814, 1950, 2090, 2235, 2389, 2548, 2711, 2879, 3051, 3228, 3409, 3595, 3785, 3980, 4183, 4391, 4603, 4820, 5041, 5267, 5497, 5732, 5976, 6225...

And there’s that cumulative-sum sequence represented as a spiral:

Spiral for cumulative sum of n where digsum(n) mod 5 = 0

You can see how the spiral is created by following 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E… from the center:

ZYXWVU
GFEDCT
H432BS
I501AR
J6789Q
KLMNOP

What about other values for the cumulative sums of digsum(n) mod m = 0? Here’s m = 2,3,4,5,6,7:

Spiral for cumulative sum of n where digsum(n) mod 2 = 0
s1 = 2, 4, 6, 8, 11, 13, 15, 17, 19, 20, 22…
s2 = 2, 6, 12, 20, 31, 44, 59, 76, 95, 115… (cumulative sum of s1)

sum of digsum(n) mod 3 = 0
s1 = 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33…
s2 = 3, 9, 18, 30, 45, 63, 84, 108, 135, 165…

sum of digsum(n) mod 4 = 0
s1 = 4, 8, 13, 17, 22, 26, 31, 35, 39, 40, 44…
s2 = 4, 12, 25, 42, 64, 90, 121, 156, 195, 235…

sum of digsum(n) mod 5 = 0
s1 = 5, 14, 19, 23, 28, 32, 37, 41, 46, 50, 55…
s2 = 5, 19, 38, 61, 89, 121, 158, 199, 245, 295…

sum of digsum(n) mod 6 = 0
s1 = 6, 15, 24, 33, 39, 42, 48, 51, 57, 60, 66…
s2 = 6, 21, 45, 78, 117, 159, 207, 258, 315, 375…

sum of digsum(n) mod 7 = 0
s1 = 7, 16, 25, 34, 43, 52, 59, 61, 68, 70, 77…
s2 = 7, 23, 48, 82, 125, 177, 236, 297, 365, 435…

The spiral for m = 2 is strange, but the spirals are similar after that. Until m = 8, when something strange happens again:

sum of digsum(n) mod 8 = 0
s1 = 8, 17, 26, 35, 44, 53, 62, 71, 79, 80, 88…
s2 = 8, 25, 51, 86, 130, 183, 245, 316, 395, 475…

sum of digsum(n) mod 9 = 0
s1 = 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99…
s2 = 9, 27, 54, 90, 135, 189, 252, 324, 405, 495…

sum of digsum(n) mod 10 = 0
s1 = 19, 28, 37, 46, 55, 64, 73, 82, 91, 109, 118…
s2 = 19, 47, 84, 130, 185, 249, 322, 404, 495, 604…

Here’s an animated gif of m = 8 at higher and higher resolution:

sum of digsum(n) mod 8 = 0 (animated gif)

You might think this strange behavior is dependant on the base in which the dig-sum is calculated. It isn’t. Here’s an animated gif for other bases in which the mod-8 spiral behaves strangely:

sum of digsum(n) mod 8 = 0 in base b = 5, 6, 7, 9, 11, 12, 13 (animated gif)

But the mod-8 spiral stops behaving strangely when the spiral is like this, as a diamond:

W
XIV
YJ8HU
ZK927GT
LA3016FS
MB45ER
NCDQ
OP

Now the mod-8 spiral looks like this:

sum of digsum(n) mod 8 = 0 (diamond spiral)

But the mod-4 and mod-9 spirals look like this:

sum of digsum(n) mod 4 = 0 (diamond spiral)

sum of digsum(n) mod 9 = 0 (diamond spiral)

You can also construct the spirals as a triangle, like this:

U
VCT
WD2CS
XE301AR
YF456789Q
ZGHIJKLMNOP

Here’s the beginning of the mod-5 triangular spiral:

sum of digsum(n) mod 5 = 0 (triangular spiral) (open in new window for full size)

And the beginning of the mod-8 triangular spiral:

sum of digsum(n) mod 8 = 0 (triangular spiral) (open in new window for full size)

The mod-8 spiral is behaving strangely again. So the strangeness is partly an artefact of the way the spirals are constructed.

Post-Performative Post-Scriptum

“Spiral Artefact”, the title of this incendiary intervention, is of course a tip-of-the-hat to core Black-Sabbath track “Spiral Architect”, off core Black-Sabbath album Sabbath Bloody Sabbath, issued in core Black-Sabbath success-period of 1973.

# WhirlpUlam

Stanislaw Ulam (pronounced OO-lam) was an American mathematician who was doodling one day in 1963 and created what is now called the Ulam spiral. It’s a spiral of integers on a square grid with the prime squares filled in and the composite squares left empty. At the beginning it looks like this (the blue square is the integer 1, with 2 to the east, 3 to the north-east, 4 to the north, 5 to the north-west, 6 to the west, and so on):

Ulam spiral

And here’s an Ulam spiral with more integers:

Ulam spiral at higher resolution

The primes aren’t scattered at random over the spiral: they often fall into lines that are related to what are called polynomial functions, such as n2 + n + 1. To understand polynomial functions better, let’s look at how the Ulam spiral is made. Here is a text version with the primes underlined:

Here’s an animated version:

Here’s the true spiral again with 1 marked as a blue square:

Ulam spiral centred on 1

What happens when you try other numbers at the centre? Here’s 2 at the centre as a purple square, because it’s prime:

Ulam spiral centred on 2

And 3 at the centre, also purple because it’s also prime:

Ulam spiral centred on 3

And 4 at the centre, blue again because 4 = 2^2:

Ulam spiral centred on 4

And 5 at the centre, prime and purple:

Ulam spiral centred on 5

Each time the central number changes, the spiral shifts fractionally. Here’s an animation of the central number shifting from 1 to 41. If you watch, you’ll see patterns remaining stable, then breaking up as the numbers shift towards the center and disappear (the central number is purple if prime, blue if composite):

Ulam whirlpool, or WhirlpUlam

I think the animation looks like a whirlpool or whirlpUlam (prounced whirlpool-am), as numbers spiral towards the centre and disappear. You can see the whirlpUlam more clearly here:

WhirlpUlam again

Note that something interesting happens when the central number is 41. The spiral is bisected by a long line of prime squares, like this:

Ulam spiral centred on 41

The line is actually a visual representation of something David Wells wrote about in The Penguin Dictionary of Curious and Interesting Numbers (1986):

Euler discovered the excellent and famous formula x2 + x + 41, which gives prime values for x = 0 to 39.

Here are the primes generated by the formula:

41, 43, 47, 53, 61, 71, 83, 97, 113, 131, 151, 173, 197, 223, 251, 281, 313, 347, 383, 421, 461, 503, 547, 593, 641, 691, 743, 797, 853, 911, 971, 1033, 1097, 1163, 1231, 1301, 1373, 1447, 1523, 1601

You’ll see other lines appear and disappear as the whirlpUlam whirls:

Ulam spiral centred on 17

Primes in line: 17, 19, 23, 29, 37, 47, 59, 73, 89, 107, 127, 149, 173, 199, 227, 257 (n=0..15)

Ulam spiral centred on 59

Primes in line: 59, 67, 83, 107, 139, 179, 227, 283, 347, 419, 499, 587, 683, 787 (n=0..13)

Ulam spiral centred on 163

Primes in line: 163, 167, 179, 199, 227, 263, 307, 359, 419, 487, 563, 647, 739, 839, 947, 1063, 1187, 1319, 1459, 1607 (n=0..19)

Ulam spiral centred on 233

Primes in line: 233, 241, 257, 281, 313, 353, 401, 457, 521, 593, 673, 761, 857 ((n=0..12)

Ulam spiral centred on 653

Primes in line: 653, 661, 677, 701, 733, 773, 821, 877, 941, 1013, 1093, 1181, 1277, 1381, 1493, 1613, 1741, 1877 (n=0..17)

Ulam spiral centred on 409,333

Primes in line: 409,333, 409337, 409349, 409369, 409397, 409433, 409477, 409529, 409589, 409657, 409733, 409817, 409909, 410009, 410117, 410233 (n=0..15)

Some bisect the centre, some don’t, because you could say that the Ulam spiral has six diagonals, two that bisect the centre (top-left-to-bottom-right and bottom-left-to-top-right) and four that don’t. You could also call them spokes:

If you look at the integers in the spokes, you can see that they’re generated by polynomial functions in which c stands for the central number:

North-west spoke: 1, 5, 17, 37, 65, 101, 145, 197, 257, 325, 401, 485, 577, 677, 785, 901, 1025, 1157, 1297, 1445, 1601, 1765, 1937, 2117, 2305, 2501, 2705, 2917... = c + (2n)^2

South-east spoke: 1, 9, 25, 49, 81, 121, 169, 225, 289, 361, 441, 529, 625, 729, 841, 961, 1089, 1225, 1369, 1521, 1681, 1849, 2025, 2209, 2401, 2601, 2809, 3025, 3249, 3481, 3721, 3969, 4225, 4489, 4761, 5041, 5329, 5625... = c+(2n+1)^2-1

NW-SE diagonal: 1, 5, 9, 17, 25, 37, 49, 65, 81, 101, 121, 145, 169, 197, 225, 257, 289, 325, 361, 401, 441, 485, 529, 577, 625, 677, 729, 785, 841, 901, 961, 1025, 1089, 1157, 1225, 1297, 1369, 1445, 1521, 1601, 1681 = c + n^2 + 1 - (n mod 2)

North-east spoke: 1, 3, 13, 31, 57, 91, 133, 183, 241, 307, 381, 463, 553, 651, 757, 871, 993, 1123, 1261, 1407, 1561, 1723, 1893, 2071... = c + (n+1)^2 - n - 1

South-west spoke: 1, 7, 21, 43, 73, 111, 157, 211, 273, 343, 421, 507, 601, 703, 813, 931, 1057, 1191, 1333, 1483, 1641, 1807, 1981, 2163... = c + (2n)^2 + 2n

SW-NE diagonal: 1, 3, 7, 13, 21, 31, 43, 57, 73, 91, 111, 133, 157, 183, 211, 241, 273, 307, 343, 381, 421, 463, 507, 553, 601, 651, 703, 757, 813, 871, 931, 993, 1057, 1123, 1191, 1261, 1333, 1407, 1483, 1561, 1641... = c + n^2 + n

Elsewhere other-engageable:

All posts interrogating issues around the Ulam spiral

# Magistra Rules the Waves

One of my favourite integer sequences has the simple formula n(i) = n(i-1) + digitsum(n(i-1)). If it’s seeded with 1, its first few terms go like this:

n(1) = 1
n(2) = n(1) + digitsum(n(1)) = 1 + digitsum(1) = 2
n(3) = 2 + digitsum(2) = 4
n(4) = 4 + digitsum(4) = 8
n(5) = 8 + digitsum(8) = 16
n(6) = 16 + digitsum(16) = 16 + 1+6 = 16 + 7 = 23
n(7) = 23 + digitsum(23) = 23 + 2+3 = 23 + 5 = 28
n(8) = 28 + digitsum(28) = 28 + 2+8 = 28 + 10 = 38

As a sequence, it looks like this:

1, 2, 4, 8, 16, 23, 28, 38, 49, 62, 70, 77, 91, 101, 103, 107, 115, 122, 127, 137, 148, 161, 169, 185, 199, 218, 229, 242, 250, 257, 271, 281, 292, 305, 313, 320, 325, 335, 346, 359, 376, 392, 406, 416, 427, 440, 448, 464, 478, 497, 517, 530, 538, 554, 568, 587, 607, 620, 628, 644, 658, 677, 697, 719, 736, 752, 766, 785, 805, 818, 835, 851, 865, 884, 904, 917, 934, 950, 964, 983, 1003…

Given a number at random, is there a quick way to say whether it appears in the sequence seeded with 1? Not that I know, with one exception. If the number is divisible by 3, it doesn’t appear, at least in base 10. In base 2, that rule doesn’t apply:

n(1) = 1
n(2) = 1 + digitsum(1) = 10 = 1 + 1 = 2
n(3) = 10 + digitsum(10) = 10 + 1 = 11 = 2 + 1 = 3
n(4) = 11 + digitsum(11) = 11 + 1+1 = 101 = 3 + 2 = 5
n(5) = 101 + digitsum(101) = 101 + 1+0+1 = 111 = 5 + 2 = 7
n(6) = 111 + digitsum(111) = 111 + 11 = 1010 = 7 + 3 = 10
n(7) = 1010 + digitsum(1010) = 1010 + 10 = 1100 = 10 + 2 = 12
n(8) = 1100 + digitsum(1100) = 1100 + 10 = 1110 = 12 + 2 = 14

1, 2, 3, 5, 7, 10, 12, 14, 17, 19, 22, 25, 28, 31, 36, 38, 41, 44, 47, 52, 55, 60, 64, 65, 67, 70, 73, 76, 79, 84, 87, 92, 96, 98, 101, 105, 109, 114, 118, 123, 129, 131, 134, 137, 140, 143, 148, 151, 156, 160, 162, 165, 169, 173, 178, 182, 187, 193, 196, 199, 204, 208, 211, 216, 220, 225, 229, 234, 239, 246, 252, 258, 260, 262, 265, 268, 271, 276, 279, 284, 288, 290, 293, 297, 301, 306, 310, 315, 321, 324, 327, 332, 336, 339, 344, 348, 353, 357, 362, 367, 374…

What patterns are there in these sequences? It’s easier to check when they’re represented graphically, so I converted them into patterns à la the Ulam spiral, where n is represented as a dot on a spiral of integers. This is the spiral for base 10:

Base 10

And these are the spirals for bases 2 and 3:

Base 2

Base 3

These sequences look fairly random to me: there are no obvious patterns in the jumps from n(i) to n(i+1), i.e. in the values for digitsum(n(i)). Now try the spirals for bases 9 and 33:

Base 9

Base 33

Patterns have appeared: there is some regularity in the jumps. You can see these regularities more clearly if you represent digitsum(n(i)) as a graph, with n(i) on the x axis and digitsum(n(i)) on the y axis. If the graph starts with n(i) = 1 on the lower left and proceeds left-right, left-right up the screen, it looks like this in base 10:

Base 10 (click to enlarge)

Here are bases 2 and 3:

Base 2

Base 3

The jumps seem fairly random. Now try bases 9, 13, 16, 17, 25, 33 and 49:

Base 9

Base 13

Base 16

Base 17

Base 25

Base 33

Base 49

In some bases, the formula n(i) = n(i-1) + digitsum(n(i-1)) generates mild randomness. In others, it generates strong regularity, like waves rolling ashore under a steady wind. I don’t understand why, but regularity seems to occur in bases that are one more than a power of 2 and also in some bases that are primes or squares.

Elsewhere other-posted:

# Spiral Archipelago

Incomplete map of Earthsea

Ursula K. Le Guin, creatrix of Earthsea, is a much better writer than J.R.R. Tolkien, creator of Middle-earth: much more subtle, skilful and sophisticated. But for me Middle-earth has one big advantage over Earthsea: I can imagine Middle-earth really existing. I can’t say that for Earthsea, an archipelago-world of fishermen, goatherds and wizards. There’s something dead and disconnected about Earthsea. I’m not sure what it is, but it may have something to do with Le Guin’s dedicated political correctness.

For example, despite the northern European climate and culture on Earthsea, a sea-faring world with lots of rain, mist, snow and mountains, most of the people are supposed to have dark skins. The ones that don’t – the white-skinned, blond-haired Kargs – are the bloodthirsty baddies of A Wizard of Earthsea (1968), the first book in the series. Balls to biology, in other words: there’s propaganda to propagate. So it’s not surprising that Le Guin’s father was a famous and respected figure in the mostly disreputable discipline of anthropology. Earthsea is fantasy for Guardian-readers, in short.

But I still like the idea of an archipelago-world: sea and islands, islands and sea. As Le Guin herself says: “We all have archipelagos in our minds.” That’s one of the reasons I like the Ulam spiral: it reminds me of Earthsea. Unlike Earthsea, however, the sea and islands go on for ever. In the Ulam spiral, the islands are the prime numbers and the sea is the composite numbers. It’s based on a counter-clockwise spiral of integers, like this:

```145←144←143←142←141←140←139←138←137←136←135←134←133
↓                                               ↑
146 101←100←099←098←097←096←095←094←093←092←091 132
↓   ↓                                       ↑   ↑
147 102 065←064←063←062←061←060←059←058←057 090 131
↓   ↓   ↓                               ↑   ↑   ↑
148 103 066 037←036←035←034←033←032←031 056 089 130
↓   ↓   ↓   ↓                       ↑   ↑   ↑   ↑
149 104 067 038 017←016←015←014←013 030 055 088 129
↓   ↓   ↓   ↓   ↓               ↑   ↑   ↑   ↑   ↑
150 105 068 039 018 005←004←003 012 029 054 087 128
↓   ↓   ↓   ↓   ↓   ↓       ↑   ↑   ↑   ↑   ↑   ↑
151 106 069 040 019 006 001→002 011 028 053 086 127
↓   ↓   ↓   ↓   ↓   ↓           ↑   ↑   ↑   ↑   ↑
152 107 070 041 020 007→008→009→010 027 052 085 126
↓   ↓   ↓   ↓   ↓                   ↑   ↑   ↑   ↑
153 108 071 042 021→022→023→024→025→026 051 084 125
↓   ↓   ↓   ↓                           ↑   ↑   ↑
154 109 072 043→044→045→046→047→048→049→050 083 124
↓   ↓   ↓                                   ↑   ↑
155 110 073→074→075→076→077→078→079→080→081→082 123
↓   ↓                                           ↑
156 111→112→113→114→115→116→117→118→119→120→121→122
↓                                                   ↑
157→158→159→160→161→162→163→164→165→166→167→168→169→170
```

The spiral is named after Stanislaw Ulam (1909-84), a Polish mathematician who invented it while doodling during a boring meeting. When numbers are represented as pixels and 1 is green, the spiral looks like this – note the unique “knee” formed by 2, 3 (directly above 2) and 11 (to the right of 2):

(If the image above does not animate, please try opening it in a new window.)

Some prime-pixels are isolated, like eyots or aits (small islands) in the number-sea, but some touch corner-to-corner and form larger units, larger islands. There are also prime-diamonds, like islands with lakes on them. The largest island, with 19 primes, may come very near the centre of the spiral:

Island 1 = (5, 7, 17, 19, 23, 37, 41, 43, 47, 67, 71, 73, 79, 103, 107, 109, 113, 149, 151) (i=19) (x=-3, y=3, n=37) (n=1 at x=0, y=0)

Here are some more prime-islands – prIslands or priminsulas – in the Ulam-sea that I find interesting or attractive for one reason or other:

Island 2 = (281, 283, 353, 431, 433, 521, 523, 617, 619, 719, 827, 829, 947) (i=13) (x=6, y=-12, n=619)

Island 3 = (20347, 20921, 21499, 21503, 22091, 22093, 22691, 23293, 23297, 23909, 23911, 24533, 25163, 25801, 26449, 27103, 27767, 28439) (i=18) (x=-39, y=-81, n=26449)

Island 4 = (537347, 540283, 543227, 546179, 549139, 552107, 555083, 558067, 561059, 561061, 564059, 564061, 567067, 570083, 573107, 573109) (i=16) (x=375, y=-315, n=561061)

Island 5 = (1259047, 1263539, 1263541, 1268039, 1272547, 1277063, 1281587, 1286119, 1290659, 1295207, 1299763) (i=11) (x=-561, y=399, n=1259047)

Island 6 = (1341841, 1346479, 1351123, 1355777, 1360439, 1360441, 1365107, 1365109, 1369783, 1369787, 1369789, 1374473, 1379167) (i=13) (x=-585, y=-297, n=1369783)

Island 7 = (2419799, 2419801, 2426027, 2426033, 2432263, 2432267, 2438507, 2438509, 2444759, 2451017, 2457283, 2463557) (i=12) (x=558, y=780, n=2432263)

Island 8 = (3189833, 3196979, 3196981, 3204137, 3204139, 3211301, 3211303, 3218471, 3218473, 3218477, 3225653) (i=11) (x=-894, y=858, n=3196981)

# La Spirale è Mobile

nn2 + 41 is prime for all values of n for 0 to 40.

Elsewhere other-posted: