# prime-palindromic number selected from a list

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[#]]], # ]] &]


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

Here you code

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

• Thanks a lot! That's what I'm looking for. Jul 31 '14 at 6:39
• 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? Jul 31 '14 at 6:41
• @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[#]]]}, {#}]] &] Jul 31 '14 at 6:45
• @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. 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.

Code:

Select[Range@5000, And @@ PrimeQ@{FromDigits@Reverse@IntegerDigits@#, #} &]

• Thanks a lot! I now know where I got wrong. Jul 31 '14 at 6:57