last day (15 days later) » 

18:09
14
Q: Prime numbers that are also a prime number when reversed

A Honey BustardThis is a challenge on Codewars I took years ago. Task is to find all prime numbers that are also a prime number when reversed, i.e. 13 -> 31 is such a number. This is still the answer with the most upvotes in the categories 'Best practice' as well as in 'Clever'. Any improvements or thoughts abo...

It would be interesting to know with what parameters the function is called. (Depending on the range it might be more efficient to precompute the primes with a sieving method.)
@MartinR I added some examples from the challenge, I do not have access to all the test cases, all I know is that this code passed them all...
I added a link. Hope that's the correct problem. Please check, and in the future include a link yourself.
I'm very sure using a StringBuilder in the parseLong call does nothing to aid.
@Bobby I use StringBuilder because of the .reverse() method which comes in handy if you want to save some lines of code
18:09
Yeah, you're completely right. I could have sworn that String had a reverse method, must have been a util I got too used to.
So this is not about efficiency but just beautiful (and correct) code, right?
@lex82 when I took the challenge this was the aim
although efficiency is also an issue when solving all test cases, for it will time out if it takes too long
rather than converting to and from a string just to reverse a number, why not do something like unsigned long r = 0; while (n) { unsigned long d = n % 10; r = r * 10 + d; n /= 10; } (this is C, sorry, but probably very similar in Java - apart from the unsigned, of course - and should be put into a function). This does the same amount of numerical computation, and avoids all the string builder nonsense.
Using Trial Division which is notoriously slow, especially compared to the Sieve of Eratosthenes. Fast enough for this challenge with small datasets, I guess.
Eliminate Palindrome Prime candidates if they begin with 2 or 5. Reversed, they cannot be prime.
"Concise code" as contrasted with "Understandable code"? In My Experience, I've had to go back and add features/fix bugs to my "concise" code, and it wasn't easy. Write for the poor developer that follows you.
18:09
@waltinator Well maybe concise isnt the right term here, I meant as short code as possible in terms of Code Golf while maintaining minimal readability(i.e. not using only one letter variables).
All primes are reversible to a prime if you pick the right number base to represent them in.
@waltinator primes beginning 4, 6, 8 can also be eliminated. If a number beginning in one of these digits is encountered, then the loop variable could be updated to be the next non-prime digit followed by a string of zeroes. So as soon as you hit 2000000, skip to 3000000.
Checking if start > 12 might seem like a good idea as it eliminates a bunch of calculations, but if you are iterating over a billion values then you are executing this test a billion times. The time taken executing the test will in the long run outweigh the value of the test.
@PhilHibbs thank you, this is also stated in the 2nd answer
I find it odd that the challenge explicitly excludes palindrome primes, but then stipulates that "the second one always being greater than or equal to the first one." But the only time the 2nd number would equal the first is if it was a palindrome.
Why isn't numbers < 12 regarded as valid results?
@DarrelHoffman That's about the range limits, not about the primes.
18:09
@Polygorial All primes <12 are palindromes, which are explicitly excluded.

last day (15 days later) »