Powers of 3 in binary
The Endeavour 2021-04-28
I was thumbing through A New Kind of Science [1] and one of the examples that jumped out at me was looking at the bits in the binary representation of the powers of 3. I wanted to reproduce the image myself and here’s the result.
Here a white square represents a 1 and a blue square a 0. There’s a white border on the right edge because all powers of 3 are odd, i.e. end in 1 when written in binary.
As with many of the images in Wolfram’s book, it’s somewhere between regular and chaotic.
I produced the image at the command line with the following Python code.
from numpy import log2 N = 60 k = int(log2(3)*N)+1 for i in range(N): s = format(3**i, str(k)+'b') s = s.replace('1', chr(0x2588)) s = s.replace('0', ' ') print(s)
The code writes powers of three as binary strings of length k where k is calculated so the last number in the sequence will fit. It then replaces 1s with a solid block character and replaces 0s with a blanks space. My terminal has a blue background and a white foreground, so 1s show up as white squares and 0s as blue.
The characters in my terminal are twice as high as they are wide, so I changed the aspect ratio after taking the screen shot so that the blocks come out square.
Related posts
[1] I’ve looked through the book before for fun, but this time I’m looking through it with a practical project in mind. I never thought that would happen.
The post Powers of 3 in binary first appeared on John D. Cook.