r/Python • u/Gedanke • Jan 05 '20
Use python to find a prime number that looks like any image if glanced upon from far away. [OC]
41
Jan 06 '20
Did you post this here before?
I have 100% seen this on here before. The exact image. The exact comment about how it's done, with explanation involving the Prime Number Theorem, and the computational complexity.
39
u/Gedanke Jan 06 '20
Yup, I have posted a prime of the python logo before and linked to this tool :) impressive memory! Different image though
10
Jan 06 '20
Nice!
Glad it was you. I just wanted to make sure somebody wasn't stealing your project and reposting.
17
u/cmcqueen1975 Jan 06 '20
Is this "definitely a prime number" or "probably a prime number"?
23
u/WiggleBooks Jan 06 '20
OP says they use the Baillie-PSW primality test, so... according to Wikipedia only a probable prime. Though I'm not sure if the code further checks if the number is prime after the Baillie-PSW primality test.
The code is on Github if OP doesn't get back to you.
2
u/arachnivore Jan 06 '20
OP says they use Baillie-PSW which is a probabilistic primality test, but it should be trivial to follow up a positive result with a more rigorous test without impacting the average time complexity much. The chance of a false positive is empirically very very low, so you should only run the slower primality test once on average.
This may not be much help, however; because I believe OP messed up their Big-O analysis. I believe it's supposed to be O(log(n)^4) instead of O(nlog(n)^3), so adding a deterministic primality test would be O(log(n)^4 + log(n)^6) = O(log(n)^6) so it might still dominate the overall time-complexity.
5
4
u/gibran800 Jan 06 '20
This is really amazing!! I have just one question: in the file primify_base.py, in the init(), you passed its arguments as:
image_path: Path = Path("./prime.png"), max_digits: int = 5000, conversion_method: int = 1, output_file_path: Path = Path("./prime.txt"), verbose: bool = False,
This is the first time that I've seen this syntax. What's the difference between doing this and just simply going with:
image_path=Path("./prime.png"), max_digits=5000, conversion_method=1, output_file_path=Path("./prime.txt"), verbose=False
Thanks in advance!!
6
u/pmdevita Jan 06 '20
I think you are talking about the type annotations, they were a feature added in 3.6. They're kinda neat but I'm not sure they affect execution at the moment
3
Jan 06 '20
They do not. They are stripped out and are only for annotating the ast. Use a tool like mypy to do type checking.
2
5
u/no_ur_great_bot Jan 06 '20
You're really amazing, u/gibran800!
4
u/gibran800 Jan 06 '20
Marry me
2
u/otterom Jan 07 '20
BEEP BOOP BEEP
WWHHHIIRRRRRRR BING!
No thanks!
I am a bot. Totally. I make funny comments and respond just like a human!
For more on on, click here. To mute me, click here.
3
u/Jinoc Jan 06 '20
No real difference, as there is no actual type checking. It's just good practice (see e.g. this).
8
u/world_is_a_throwAway Jan 06 '20
I don't really understand where the prime number is and how it fits?
20
Jan 06 '20
the whole image is a prime number, it looks like the reddit logo :)
0
Jan 06 '20
[deleted]
12
Jan 06 '20
is "8" a "1" ?
0
Jan 06 '20
[deleted]
5
u/deadwisdom greenlet revolution Jan 06 '20
Yeah dude, you could totally do something even cooler. Go ahead and then post it to the sub.
2
u/Mceight_Legs Jan 06 '20 edited Jan 06 '20
The entire image is the prime number. One prime number in case you're somehow confused
Also it has 1,8,9,3,7,2
Six numbers, so a little more than just ones
2
u/DatBoi_BP Jan 06 '20
Wait a second, is every number whose digits are all 1s prime? (Besides 1 itself I guess)
4
1
3
u/zacharius_zipfelmann Jan 06 '20
This is the most useless, yet coolest program I've seen in a while. Amazing!
4
2
2
2
u/PoemNinja Jan 06 '20
u/Gedanke How long did it take to find reddit logo ?I am running for around 30 min now ( 5000 dig) and still no luck.
Don't you think that this could be optimized with not only using sympy's nextprime but actually changing digits inside ?Congrats on this lib, I like how simple code really is, and could be simplified even further :)
2
2
2
2
u/JesusIsMyZoloft Jan 06 '20
Try squishing the image vertically by a factor of 2 before resizing it. Most fonts display digits as twice as tall as they are wide.
1
u/Steampunkery Jan 07 '20
In the github he linked, the y value is multiplied by 0.45, which is close enough :)
2
3
u/YeastBeast33 Jan 06 '20
Fk i read it all still not understand. Can someone explain like i'm 13 ? I know what is prime number at least xd
3
u/Leeoku Jan 06 '20
The "image" is actually one long prime number. The positioning of the digits of this number looks like the reddit mascot.
Just like how a picture is made of pixels. Each pixel here is a number4
1
u/lillyofthewalley Jan 06 '20
Dude, I was stuck with big primes. How efficient do you think your algorithm for checking is?
2
u/Spellersuntie Jan 06 '20
They're using the Baillie PSW primality check which has a complexity of O((log n)3) bit operations.
Here's a pointer with some additional info. There's no formal proof of the runtime, you'll probably need to look at the resources for the Rabin Miller and Lucas checks.
1
u/Steampunkery Jan 07 '20
Another commenter in this thread said that the time complexity is actually O(log(n)4), not O(nlog(n)3)
2
u/Spellersuntie Jan 07 '20
Possibly? Number theory and algebra is way out of what I know. Wikipedia says one iteration of Miller Rabin is log3 (n) but not sure about the other steps of the algo.
0
u/gandalfx Jan 06 '20
I've seen the exact same thing here before. Plus there's even already at least one tutorial on how to do it.
So it's cool but [OC] is a bit much.
1
-33
u/Rue9X Jan 06 '20
This is an ascii art generator that only uses prime number characters between 0-9.
20
u/raaaaaveNN Jan 06 '20
No, is 8 a prime number? Read the title again
11
u/total_zoidberg Jan 06 '20
The whole number (888...892281) is prime.
4
211
u/Gedanke Jan 05 '20
How it's done:
We proceed in 5 steps:
We resize the image to contain at most a certain amount of pixels.
Run various image processing steps like edge enhancement and smoothing before converting the image into grey-scale (using Pillow)
We then quantise the image into just having 5 to 10 greyness levels.
Now we map each greyness level to a digit, et voila, we have embedded the picture into a number.
It now remains to tweak some of the digits until we find a prime number that still looks like the image.
Note: According to the prime number theorem, the density of prime numbers is asymptotically of order 1/log(n). Hence, if we have a number with m digits, the number of primality tests that we expect to do until we hit a prime number is roughly proportional to m. Since we use the Baillie–PSW primality test, the overall expected computational complexity of our prime searching procedure is O(nlog(n)³).
You can find the code @ Github and experiment yourself.