23
3
Given a number N, output/return X so that N+X is a palindrome, where |X| has to be as small as possible.
Palindrome: A number is a palindrome, if its sequence of digits is the same when reading them from left to right as when reading from right to left.
95359
and 6548456
are symmetric, 123
and 2424
are not. Numbers with leading zeros such as 020
are not a palindrome.
Input is a positive integer smaller than 1015. Read it from stdin, as a method-parameter, whatever.
Output has to be an integer (positive or negative) and ought to be 0 if the input is already a palindrom. You may write your output to stdout, return it from a function or whatever you like. If there are 2 numbers (e.g. 2
and -2
) that satisfy the requirements, output only one of them.
Examples:
Input Output
3 0
234 -2
1299931 -10
126 5 or -5 (only one of them)
Presumably if a number is halfway between the two nearest palindromes, either is an acceptable output? E.g. for
N=10
the output can beX=-1
orX=1
? – Peter Taylor – 2014-08-27T13:45:52.950@PeterTaylor Yes, it just has to be as small as possible. – CommonGuy – 2014-08-27T13:47:12.440