First, here is a prime number whose binary representation is a T-Rex.
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 prime_dinosaur.py -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:
You can subscribe ⚛ to our blog.
We love building amazing apps for web and mobile for our clients. If you are looking for developmenrt help, contact us today ✉.
Would you like to download 10+ free Django and Python books? Get them here