r/Python • • Jan 05 '20

Use python to find a prime number that looks like any image if glanced upon from far away. [OC]

Post image
1.3k Upvotes

65 comments sorted by

211

u/Gedanke Jan 05 '20

How it's done:

We proceed in 5 steps:

  1. We resize the image to contain at most a certain amount of pixels.

  2. Run various image processing steps like edge enhancement and smoothing before converting the image into grey-scale (using Pillow)

  3. We then quantise the image into just having 5 to 10 greyness levels.

  4. Now we map each greyness level to a digit, et voila, we have embedded the picture into a number.

  5. 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.

45

u/HappyEmanuel Jan 06 '20 edited Jan 06 '20

Wow! I didn't know that theorem.

I love when we use these math theorems to build some things that, although silly, are amazing!!! Especially when it comes from number theory hahah.

Great job! And thanks for the learning :)

9

u/ThunderChaser Jan 06 '20

You mean that math I do in my degree is actually useful

1

u/HappyEmanuel Jan 06 '20

Of course it is!!! :D

1

u/xi27pox Jan 06 '20

I hereby propose Bitcoin change its hashing algorithm to be ascii art based (difficulty == image dimensions).

0

u/[deleted] Jan 06 '20

[deleted]

10

u/suclearnub Jan 06 '20

nlogn3 only means that the problem scales at that rate. You can't substitute "n" for a number.

0

u/[deleted] Jan 06 '20

[deleted]

5

u/dutch_gecko Jan 06 '20

Big O notation describes the worst-case time complexity of an algorithm. It is nearly always notated without including constants. This means that an algorithm that loops through a list once, an algorithm that loops through a list twice, and an algorithm that sometimes loops through a whole list once but can often stop earlier, all share the same complexity O(n).

You can't "work backwards" to get the actual runtime of an algorithm, since the notation describes the worst case.

1

u/arachnivore Jan 06 '20

Big O notation by itself is just a tool with very little inherent context. It's *usually implied* to describe the worst-case complexity of an algorithm, but it's up to whoever uses it to explain if they're analyzing time, memory, or some other resource and whether it's worst case or something else.

You can describe what the a theoretical limit on complexity as OP did: If the density of primes grows with 1/log(N) where N is some number, then the average distance between primes would be 1/density = log(N) which is about the same as the number of digits M in N. That's roughly the number of primality tests you must perform. I'm not sure how they got to O(nlog(n)^3) unless their primality test is O(nlog(n)^2) which would be kind-of strange.

0

u/[deleted] Jan 06 '20

[deleted]

5

u/dutch_gecko Jan 06 '20

The best case for search problems like the above is 1. It occurs when the first number you test turns out to be your answer. That's why the best case scenario isn't a useful metric.

0

u/[deleted] Jan 06 '20

[deleted]

6

u/dutch_gecko Jan 06 '20

The average case is sometimes worth considering. For example, Quicksort has worst case O(n2 ), which is worse than other sorts, but its average case is comparable and it has other advantages which is why it is still often used.

'n' refers to the length of the input to the algorithm. It's not really a number, but it is helpful to consider it as being really frickin' big. If the length of the input were to become bigger, say n+1, how much longer would the algorithm take relative to an input of length n? This is what big O notation tries to encapsulate.

It is useful to know what the big O for an algorithm is, as this lets us reason about how the algorithm will perform as our input grows. As an example, lets imagine an algorithm performs in O(2n ). This is an algorithm which grows in exponential time, which is generally bad news. It could be, however, that in our case we only feed it an input with length 100, and this performs acceptably, so we can deal with having an inefficient algorithm in our code. But, if the boss shows up at my desk and says, "Hey, we're going to have to crunch more numbers, can we do 200?" I'd have to tell him no, as it's almost certain the algorithm won't be able to complete its work in a useful amount of time.

If you want to read more about this, the wikipedia page exists but is utterly terrifying. I also found this three part blog article which I haven't read entirely, but the examples are in Python and the author seems to have his head screwed on straight.

2

u/[deleted] Jan 06 '20

A concrete example might help.

Take insertion sort. The exact metric of its performance in the worst-case scenario (where you get all of the items in order, so you have to compare every new element with every single element of the list) is n(n - 1) = n^2 - n. Given a specific n - let’s say, 10 - we can calculate its absolute worst-case performance as 100 - 10 = 90. But for the sake of simplicity - as the value of n scales to large values - the value of the n^2 term scales much faster than the value of the n term. For very large values of n, the proportionality is determined almost solely by the n^2 term, and the -n term is practically trivial. So, for the big-O notation of the algorithm, we drop all but the highest-order term: O(n) = n^2.

tl;dr - big-O notation is an approximation of performance.

Of course, this means that you can calculate the value of the big-O expression as a useful metric - so long as you understand the result as a rough approximation, indicative of proportionality rather than precision. For insertion sort, the worst-case performance for n = 10 is somewhere around 100. (Its actual value is 90, as noted above.)

0

u/[deleted] Jan 06 '20

[deleted]

→ More replies (0)

41

u/[deleted] 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

u/[deleted] 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.

https://www.reddit.com/r/Python/comments/ekjr42/use_python_to_find_a_prime_number_that_looks_like/fdbtjo5

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

u/[deleted] Jan 06 '20

This is cool, but why does it need to be prime? What is the purpose?

11

u/[deleted] Jan 06 '20

Just for fun!!

3

u/DannyckCZ Jan 06 '20

Because primes are awesome!

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

u/[deleted] 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

u/gibran800 Jan 06 '20

I see. Gonna look them up, then. Thanks!

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

u/[deleted] Jan 06 '20

the whole image is a prime number, it looks like the reddit logo :)

0

u/[deleted] Jan 06 '20

[deleted]

12

u/[deleted] Jan 06 '20

is "8" a "1" ?

0

u/[deleted] 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

u/Mceight_Legs Jan 06 '20

I don't think so 11111111111 isn't a prime

3

u/DatBoi_BP Jan 06 '20

Ah. Thx thx

1

u/[deleted] Jan 06 '20

there's 9s, 3s 7s and 8s there too...

3

u/zacharius_zipfelmann Jan 06 '20

This is the most useless, yet coolest program I've seen in a while. Amazing!

4

u/[deleted] Jan 06 '20

This is pretty damn cool! Good job!

2

u/PatchaScroll Jan 06 '20

Can’t see it if you ain’t on night mode 😌🤏🏼

2

u/Sigolirous Jan 06 '20

Ok this is great

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

u/big_giant_moose Jan 06 '20

Anything can look like anything from far away

2

u/PB_Dendras Jan 06 '20

beautiful! Now this is art

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

u/[deleted] Jan 06 '20

That’s cool

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 number

4

u/YeastBeast33 Jan 06 '20

Damnn. That's long ass number. Thanks

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

u/Steampunkery Jan 07 '20

He's the same guy who did the other one.

-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

u/DatBoi_BP Jan 06 '20

Now if you'll excuse me, I'm going to scuttle

3

u/total_zoidberg Jan 07 '20

Woop woop woop woop woooooop