Binary Prime Numbers
![]() |
I was wondering what prime numbers would look like in binary format. So, I wrote this little Python script prime.py:
import math
def int2bin(n):
s = ''
for i in range(0,1 + int(math.log(n, 2))):
if n & (1 << i) > 0: s = '# ' + s
else: s = '. ' + s
return s
def is_prime(n):
if n < 2: return False
if n == 2: return True
if n % 2 == 0: return False
for i in range(3, int(n ** 0.5) + 1, 2):
if n % i == 0: return False
return True
print int2bin(2).center(32)
for i in range(3, 256, 2):
if is_prime(i): print int2bin(i).center(32)
The output is a nicely formatted ASCII interpretation prime.txt:
# .
# #
# . #
# # #
# . # #
# # . #
# . . . #
# . . # #
# . # # #
# # # . #
# # # # #
# . . # . #
# . # . . #
# . # . # #
# . # # # #
# # . # . #
# # # . # #
# # # # . #
# . . . . # #
# . . . # # #
# . . # . . #
# . . # # # #
# . # . . # #
# . # # . . #
# # . . . . #
# # . . # . #
# # . . # # #
# # . # . # #
# # . # # . #
# # # . . . #
# # # # # # #
# . . . . . # #
# . . . # . . #
# . . . # . # #
# . . # . # . #
# . . # . # # #
# . . # # # . #
# . # . . . # #
# . # . . # # #
# . # . # # . #
# . # # . . # #
# . # # . # . #
# . # # # # # #
# # . . . . . #
# # . . . # . #
# # . . . # # #
# # . # . . # #
# # . # # # # #
# # # . . . # #
# # # . . # . #
# # # . # . . #
# # # . # # # #
# # # # . . . #
# # # # # . # #
I just did a screen shot of the output, loaded it in Photoshop, and ran a "Chalk & Charcoal" Filter. |

