Is there any suggestion on solving next higher prime and palindrome number from a given int.
Here is the snippet I am trying but its a kind of slow, please suggest if you ve any good algorithm that i can test.
#!/usr/bin/python
def next_higher(n):
while True:
s = str(n)
if not any([n % i == 0 \
for i in range(2, int(n**0.5))]) and s == s[::-1]:
return n
n = n + 1
print next_higher(2004)
print next_higher(20)
Output:
10201
101
Updated code testing for palindrome before prime. much faster than my previous code. I am implementing the suggestion from user2357112.
#!/usr/bin/python
def next_higher(n):
while True:
s = str(n)
if s == s[::-1]:
if not any([n % i == 0 \
for i in range(2, int(n**0.5))]):
return n
n = n + 1
print next_higher(2004111)
print next_higher(2004)
print next_higher(2004)
print next_higher(20)
I tried optimizing the palindrome check i.e to find odd palindrome's. Since the first digit should be odd number, i focused on that part. Here's the code below with the assumptions its greater than 1 digit.
let me know if anything wrong.
There are quite a few optimizations you could do:
[2] + range(3, int(n**0.5) + 1, 2)
to check only odd numbers after 2. (Also you need to do sqrt + 1 like I mentioned in the comments)()
instead of[]
.[]
generates the entire list of factors first and only then checks forany
. If you use()
, it creates a generator, so it stops as soon as aTrue
value is found without calculating the entire list.xrange
instead ofrange
for the same reason (xrange
gives a generator,range
gives a list)+ 1
each time.Here is a version with most of these optimizations except the last two:
This should be pretty fast for your needs I believe. But you can do the last 2 optimizations to make it much more faster if you want.
Just for the fun of it, I implemented all optimizations by Hari Shankar and Abhishek Bansal.
It first finds the higher odd length palindrome, then increment the palindrome in a way that keeps its palindromity. Then checks each number using prime numbers calculated by Sieve method in the beginning.
This can process up to
n=10^14
(can be higher if you increase the CACHE size) under 1 second in my computer =DWhich in my computer gives this output:
Other than what has already been suggested,
What I suggest is that you first get the first palindrome number that is just higher than the given integer.
You can do this by trying to match the centre digits outwards.
Also, you should only check for numbers with odd number of digits, since if a number has even number of digits and it is a palindrome, then it will always be divisible by 11 and cannot be prime.
Once you get the first palindrome number that has odd number of digits and that is just higher than the current number, test it for primality and find the next palindrome number higher than this one.
You can do this by incrementing the centre digit.
Keep doing this till it rolls over to zero. In that case start incrementing the two neighbouring digits.
Continue, till you reach a prime number.