A palindromic-prime or PalPrime is a prime number that is also a palindrome.The first few PalPrimes are 2, 3, 5, 7, 11, 101, 131, 151, 181..... Let S be the sum of the digits of the largest PalPrime N such that N<10^9 . What is the value of S ?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Can you explain what you mean by "A much better way would be to create a palindrome generator using a combinatorics approach."?
And if so, how would we implement that?
Can you explain what you mean by "A much better way would be to create a palindrome generator using a combinatorics approach."?
And if so, how would we implement that?
Log in to reply
Let n be equal to 999727999. When I'm using my way, I'm bruteforcing all the way down to n . So I'm checking all numbers between 1 0 9 and n if they are palindromes and if they are, then I'm testing if they are primes. But this all search is not reasonable because 9-digit palindromes follow a certain pattern - a b c d e d c b a where a , b , c , d , e represent not necessarliy distinct digits and a = 0 . Now, with my code, I need to test for 1 0 9 − 9 9 9 7 2 7 9 9 9 = 2 7 2 0 0 1 numbers for prime and palindromes. But if I made a palindrome generator and start generating palindromes in reverse from 1 0 9 , then I just need to check for 27 numbers.
I'm not sure about how am I going to implement a palindrome generator but I think it will be using a similar mechanism to a cryptarithm solver (ie, itertools in python) I'm currently working on the code. I'll post my code when I'm done.
Log in to reply
Well, like you mentioned, all that matters is the first 5 digits. So, you can just do a for loop, counting down from 99999, and given a b c d e to test a b c d e d c b a for primality.
Log in to reply
@Calvin Lin – I posted my code (I posted it long time ago. Sorry for late notice)
Good fun! Counting backwards palindromically.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Can you please explain your code?
Log in to reply
My pleasure.
The idea behind it is this. We want 9-digit palindromes in the sequence 999989999, 999979999, ..., 999222999, ... and so on. We notice that if we start with 99999 and subtract 1 repeatedly we obtain the first five digits of the numbers in this sequence. At any point in this process the digits we can calculate how many digits have been changed by subtraction on the basis of the number of 9 digits that remain. And knowing how many 9 digits remain unchanged tells us how to pack up a palindrome.
For instance, take 99929. Three unchanged 9s remain. This means that we need four digits reversed to make the corresponding palidrome, 999292999.
Line 5 calculates the succession of smaller and smaller 5-digit numbers. Line 6 calculates the strings corresponding to these numbers, and line 7 calculates these strings in reverse. Lines 8 through 10 calculate the number of unchanged 9 digits. Line 11 packs the palindrome as a string. Line 12 calculates the palindrome as an integer. Lines 13 through 15 print the palindrome if it is a prime and break out of the while loop.
Problem Loading...
Note Loading...
Set Loading...
EDIT
Here is a much more efficient way.
This code is created to only search for 9 digit palindromes. To search for smaller length palindromes, slight modifications need to be made.
One more optimization could be made, which is that when first digit of
x
is even, we can skip 1000 counts. Note: If first digit is even, then in my code last digit will also be even. But there are no even primes apart from 2.It only takes 21 iteration to find the answer.
Old Answer
Here is a basic brute force way to do it. For obvious reasons, I start at 1 0 9 and then loop backward. I also only test for odd numbers as all even numbers except 2 are composite.
Here's the code -