A palindromic-prime or PalPrime is a prime number that is also a palindrome.The first few PalPrimes are 2 , 3 , 5 , 7 , 1 1 , 1 0 1 , 1 3 1 , 1 5 1 , 1 8 1 , 1 9 1 . . . . Let S be the sum of the digits of the largest PalPrime N such that N < 1 0 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.
@Steven Perkins I wouldn't call this the slickest way of approaching the problem but it is more efficient than other methods I have tried.
I wasn't able to figure out a really slick way to solve this. It helps if you have a program written to factor numbers (or determine if prime). Such programs have been shared before, and are not too hard to write for numbers with less than 12 digits.
After some thought, it was obvious the number should start with as many 9's as possible. Therefore I tried numbers of the form 9999n9999. None of these were prime.
It took a little more thought to convince myself that next I should try 9998n8999. Then 9997n7999. Finally a prime was found.
999727999 is prime. The digit sum is 7 0
I presume that such prime number is a nine-digit one. A matlab built-in function, isprime , determines whether a number is prime or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
OUTPUT
1 2 |
|
Quick and dirty perl solution. Only worked on odd numbers from 999999999 down to save bothering with guaranteed non prime even numbers. Checked palidromic first to avoid wasting time checking that every palindrome is prime.
sub is_prime {
(my $number) = @_;
my $d = 2;
my $sqrt = sqrt $number;
while(1) {
if ($number % $d == 0) {
return 0;
}
if ($d < $sqrt) {
$d++;
}
else {
print "$number\n";
return 1;
}
}
}
my $num = 999999999;
my $found = 0;
while ( $found == 0 ) {
if ( $num == reverse $num ) {
if ( is_prime $num ) {
$found = 1;
}
else {
$num -= 2;
}
}
else {
$num -= 2;
}
}
print "Largest palindromic prime less than 1000000000 is: $num\n";
my $sum = 0;
foreach my $x (split //, $num) {
$sum += $x;
}
print "Sum of digits is $sum\n";
Basically I checked if it was a palindrome by reversing the number and checking if it was the same as the original number. Then I checked if the number was prime. The isPrime() method was checking for factors until the square root, as that is the maximum factor a number can be. The rest is explained in comments in my code.
public class ReverseNumber {
public static void main(String args[]) {
System.out.println(findNumber());
}
static boolean isPrime(long n) {
//check if n is a multiple of 2
if (n%2==0) return false;
//if not, then just check the odds
for(long i=3;i*i<=n;i+=2) {
if(n%i==0)
return false;
}
return true;
}
public static long findNumber() {
String a = "";
for(long i = 1000000000l;i>0;i--){
if(isPalindrome(i)){
if(isPrime(i)){
return i;
}
}
}
return 2;
}
public static boolean isPalindrome(long number) {
long palindrome = number; // copied number into variable
long reverse = 0;
while (palindrome != 0) {
long remainder = palindrome % 10;
reverse = reverse * 10 + remainder;
palindrome = palindrome / 10;
}
// if original and reverse of number is equal means
// number is palindrome in Java
if (number == reverse) {
return true;
}
return false;
}
}
First, I created a function to check whether a number is palindromic. I then tested this on every number descending from 1 0 9 . If these numbers were palindromic, I then tested for primality. Finally, as soon as the code found a number that was both palindromic and prime, it printed this to the console and stopped both loops.
def palindrome(number):
y= str(number)
if y == y[::-1]:
return y
else:
return
for n in range(10**9, 2, -1):
temp = palindrome(n)
if temp != None:
print temp
for x in range(2, n):
if n % x == 0:
break
else:
print(n)
quit = True
break
if quit == True:
break
The digit sum was then found with the short piece of (unnecessary (as I could have done it in my head)) code shown below.
string = "999727999"
total = 0
for x in range(len(string)):
temp = string[x]
total += int(temp)
print(total)
The total was 7 0
First it checks if the number is a palindrome,then checks if it is prime and finally calculates the digit sum.
My highly inefficient c# code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
checks for palindrome then prime then sum.
Problem Loading...
Note Loading...
Set Loading...
My general method was generating the palindromes first and then check them for primality. Generating palindromes is quite easy. For every integer N = a b c (where a , b , c are the base 10 digits of N ) there exists two unique palindromes a b c c b a and a b c b a . So I would only do 1 0 5 checks at most.
After generating all the palindromes I check for primality. The code could have been faster if I used a better primality test like Miller Rabin but the normal method sufficed here. Also after generating the palindromes,I sorted them and started checking primality backwards.
Here's my code