0

I need to efficiently identify Emirps - primes that stay prime when digits are reversed (like 13 becoming 31).

My current algorithm takes known primes and tests if their reverses are also prime using Miller-Rabin. I've added two filters: skip primes starting with even digits or 5, since their reverses would be composite.

What other mathematical shortcuts can I use to eliminate candidates without running the full primality test? I'm particularly interested in digit patterns or number theory properties that guarantee a reversed number will be composite.

Since I already know the input numbers are prime, are there any special optimizations for testing whether their reverses are prime?

Any suggestions for making this more efficient would be helpful.

  • Well,.... the exact same procedures one would use to determine if a string of digits were prime would work just in the other order. No number ending in an even number can be prime so no number begining with an even number can be Emirp. And not number were the digits add to a multiple of $3$ can be prime so the same about Emirp. Every rule for primes relating to digits can be reversed to emirps and vice versa " but I suspect there are more elegant mathematical shortcuts I'm missing" why would you think that? They should be utterly equivalent. – fleablood Jun 18 '25 at 05:20
  • I was hoping that there would be some sort of trick that applies specifically to Emirps that one could exploit. If one already knows that the original number is prime, is there any information that can be used with that to determine, in a faster way, whether or not the reverse of that number is prime? In other words, does the knowledge that a number is prime help you determine if it is also an Emirp? – Peter El Ghazal Jun 18 '25 at 05:23
  • Okay. I didn't get that the number had to be prime in the first place. Hmm.... I really doubt there is anything one can do. After all digits are pretty arbitrary based on base. – fleablood Jun 18 '25 at 05:28

2 Answers2

3

Most common primality tests applied to large numbers are unrelated to the base the number is written in, so they will not involve any property of the digits of the number itself.

The standard tests for divisibility by 3 and 11 using a number's digits do carry through to the reverse of a number. In the case of divisibility by 3, you're just adding the digits together, and in the case of divisibility by 11 you alternately add and subtract the digits, so it's easy to see that the test gives the same results when the digits are reversed. For digit-based tests of other factors, though, you typically have to group digits together and that breaks down as soon as you reverse the number.

ConMan
  • 27,579
1

Primality testing normally starts out with some degree of testing for divisibility by small primes. There's a fast way to test for divisibility without using costly modulus or division operations, but instead uses some pre-calculated constants. There's a description and a short list of constants here https://lomont.org/posts/2017/divisibility-testing/.

Since you require both the integer and its reverse to be prime, you can test both at the same time for divisibility by small primes to sieve out many composites so you only have to perform a more expensive test like Miller Rabin on a small subset of the range you're exploring.

If you want it to be fast, I strongly recommend using some low level language like c rather than Python. If you get really serious, the sieving part can be done efficiently on a GPU in a similar way to how I found the results to this question here Can I prepend a digit to some number finitely many times and always get primes, for arbitrarily long finite lengths?.