Finding a prime number whose binary representation is a giraffe (or a T-Rex)

Odd Number Theorists

First, here is a prime number whose binary representation is a T-Rex.

Math with bad drawings asked for a prime number whose binary representation is a giraffe. This lead to discussion on Math.reddit and Hacker news which led to finding such a prime number.


You can see the binary representation on reddit.

As Nathaniel Borenstein said It should be noted that no ethically-trained software engineer would ever consent to write a DestroyBaghdad procedure. Basic professional ethics would instead require him to write a DestroyCity procedure, to which Baghdad could be given as a parameter.

So the obvious next step is to generalize this is to program which can take an image and find a primary number whose binary representation is the image.

We run this for an Argentinosaurus image

python -f ~/Downloads/argentinosaurus.jpg -s 40

Which gives us

How does this work?

  • We read the image and convert to desired size
  • The image data is converted to monochrome and pixels darker (lower) than a threshold are converted 1, rest pixels are zero.
  • This data is read in in a numpy array
  • This 2d array is flattened, and treated as a bitarray to get a number
  • We then start incrementing the number until we get a prime.
  • The primality is tested using the Miller Rabin test.
  • When such a number is found the number is converted to its binary representation, and displayed in a square grid.

The full code is:

Thank you for reading the Agiliq blog. This article was written by shabda on Jan 21, 2018 in pythonfun .

You can subscribe ⚛ to our blog.

We love building amazing apps for web and mobile for our clients. If you are looking for development help, contact us today ✉.

Would you like to download 10+ free Django and Python books? Get them here