I want to find all prime-palindromic numbers up to 5000. Prime-palindromic numbers are numbers that themselves and their reverse digits are both primes. For example, 31 is a prime-palindromic number because both 31 and 13 are prime. Here is my code, but it somehow does not work.

 Select[Range@5000, PrimeQ[Join[FromDigits[Reverse[IntegerDigits[#]]], # ]] &]

enter image description here

  • $\begingroup$ possible duplicate of Functional style using lazy lists? $\endgroup$ – RunnyKine Jul 31 '14 at 6:23
  • $\begingroup$ The link above has great answers for this exact problem. $\endgroup$ – RunnyKine Jul 31 '14 at 6:23
  • $\begingroup$ @RunnyKine But it is way over complicated, isn't it? $\endgroup$ – Lawerance Jul 31 '14 at 6:24
  • $\begingroup$ Not really, look at the third answer with about 4 variations. $\endgroup$ – RunnyKine Jul 31 '14 at 6:26
  • $\begingroup$ @RunnyKine Got it. But I still want my code to work with the help of others. $\endgroup$ – Lawerance Jul 31 '14 at 6:28

Here you code

| improve this answer | |
  • $\begingroup$ Thanks a lot! That's what I'm looking for. $\endgroup$ – Lawerance Jul 31 '14 at 6:39
  • $\begingroup$ I also have a question. In my code, I was trying to make two lists with # into a single list by join. And then, I will apply PrimeQ. Do you know why it won't work, or do you have a better way to make the two into a single list? $\endgroup$ – Lawerance Jul 31 '14 at 6:41
  • 2
    $\begingroup$ @Lawerance, Join needs Lists as inputs. So if you want to do it your way here is how: Select[Range@5000, And @@ PrimeQ[Join[{FromDigits[Reverse[IntegerDigits[#]]]}, {#}]] &] $\endgroup$ – RunnyKine Jul 31 '14 at 6:45
  • $\begingroup$ @RunnyKine Thanks a lot! I think I need to rephrase my title. It is not about finding the prime numbers, but to join the list. $\endgroup$ – Lawerance Jul 31 '14 at 6:46

Let's see where you went wrong:

  • Join[a,b] is not {a,b}, you have to type Join[{a},{b}] if you want to use Join in this way.
  • The second argument of Select is a criterion function, it needs to return either true or false. Your function returns a list of boolean values instead. You need to check if all values in the list are true, so you need to use And.

Improvements of style:

  • Instead of Join[{a},{b}] you can simply write {a,b}.
  • Prefix notation (f@expr) can make your code look simpler.


Select[Range@5000, And @@ PrimeQ@{FromDigits@Reverse@IntegerDigits@#, #} &]
| improve this answer | |
  • $\begingroup$ Thanks a lot! I now know where I got wrong. $\endgroup$ – Lawerance Jul 31 '14 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.