Special Palindromes

A palindrome is a positive integer that reads the same forwards and backwards; e.g., 101 and 2552 are both palindromes.

What is the smallest x x for which x x and x + 2016 x+2016 are both palindromes?


The answer is 646.

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.

5 solutions

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def ispalindrome(n):
    if n==int(str(n)[::-1]):
        return True
    else:
        return False
a=1
while a>0:
    if ispalindrome(a) and ispalindrome(a+2016):
        print(a)
        break
    else:
        a+=1

Nice! The function definition for ispalindrome(n) is really elegant (+1 for this) , although you can make it even more concise as,

1
2
def ispalindrome(n):
    return n==int(str(n)[::-1])

Prasun Biswas - 5 years, 5 months ago

+1 for [::-1]. What a pythonic way of reversing string!

Hun-Min Park - 5 years, 5 months ago
Sonveer Yadav
Dec 18, 2015

for (2016+x) to be palindrome, no should be of the form 2aa2, where 'a' represent the digits.

2aa2 = 2016 + x

2000+100a+10a+2 = 2016+x

2002+110a = 2016+x

x = 2014 - 110a

now putting a=1,2,3... we get palindrome on a=6 i.e x=646

Ramiel To-ong
Dec 17, 2015

by trial and error.

Bill Bell
Dec 16, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
def palindrome(n):
    result=0
    while n>0:
        result=10*result+n%10
        n=n/10
    return result 

x=1
while True:
    if x==palindrome(x) and x+2016==palindrome(x+2016):
        print x
        break
    x+=1

Faisal Basha
Dec 16, 2015

/(2662 - 2016 = 646 )/

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...