# Goal:

Given any non-zero natural number a, find the smallest non-zero natural number b such that a•b is palindromic, e.g. it reads the same forwards and backwards. Input a through any reasonable means (STDIN, function argument, etc.), And output b through any reasonable means (STDOUT, function return value.)

# Notes:

• Input will not always have a solution (multiples of 10), so you must create an error, or display -1/0

• Input validation is not necessary, assume a is a natural number.

• Standard loopholes apply (Hardcoding, using a language created after this challenge)

# Examples:

a=34, b=8 (272)
a=628, b=78 (48984)
a=15, b=35 (525)
a=33, b=1 (33)
a=20, (Error, -1, or 0)

• How do we know if the input has a solution or not? Or is figuring that out part of the challenge? – Luis Mendo Jan 10 '17 at 12:07
• As far as I can tell, only multiples of 10 do not have solutions. – Julian Lachniet Jan 10 '17 at 12:07
• Do we have to "detect" that there is no solution or can we e.g. go into an infinite loop which will eventually throw an error? – Fatalize Jan 10 '17 at 12:29
• Related Math.SE post. Yes, every number that isn't a multiple of 10 has a palindromic multiple. – Martin Ender Jan 10 '17 at 12:33
• @JulianLachniet Having a*b=c where a is given and c is a palindrome, your question asks to output 'b', the linked question asks to output 'c'. There is really very little difference... – steenbergh Jan 10 '17 at 14:14

# Pyth, 10 9 bytes

&eQf_I*Q


Try it online.

### Explanation

&eQf_I*QT    Implicitly append T, and take input in Q.
eQ           Calculate input mod 10.
&             Logical and, only evaluate the following if result ≠ 0.
f          Starting from 1, find the first integer T for which...
       ...the string representation of...
*QT    ...input multiplied by T...
_I        ...is equal to its reverse.


## Ruby, 51 bytes

->a{(1..a%10/0.0).find{|b|(c="#{a*b}")==c.reverse}}


### Explanation

        a%10/0.0 # -> NaN for multiples of 10, Infinity otherwise


# 05AB1E, 11 bytes

T%            # input mod 10
0›          # is greater than 0
µ         # repeat the following until counter reaches that number (0 or 1)
¹N*      # input * iteration
ÂQ    # is equal to its reverse
½   # if true, increase counter
# implicitly output last iteration (or 0)


Try it online!

# C, 108 101 bytes

Thanks to @nmjcman101 for 7 bytes!

m,o;f(n){for(o=0;n;)o=o*10+n%10,n/=10;return o;}g(n){m=-1;if(n%10)for(m=1;n*m!=f(n*m);m++);return m;}


Is C the only language here with no built-in reversing/checking palindrome function?

Try it online!

• Could you change the for in g to for(m=1;n*m!=f(n*m));, initialize m=-1; earlier in g and then just return m. g(n){m=-1;if(n%10)for(m=1;n*m!=f(n*m);m++);return m;} (I think you could throw ++m in the condition, but check that) – nmjcman101 Jan 10 '17 at 13:29
• @nmjcman101 thanks! I saw your comment before editing, tried ++m in various places, but it didn't work. – betseg Jan 10 '17 at 13:45

## QBIC, 29 bytes

:{c=a*q~!c$=_f!c$||_Xq\q=q+1


Explanation:

:      Get cmd line param as number 'a'
{      DO
c=a*q  multiply 'a' by 'q' and assign to 'c', which is 1 at the start of a QBIC program
~      IF
!c$'c' cast to string = equals _f!c$| 'c' cast to string, the reversed
|      THEN
_Xq    Quit, printing 'q'
\q=q+1 ELSE increment q and rerun
DO Loop is auto-closed by QBIC, as is the IF


Eventually, 'q' will overflow and throw an error on an unsolvable 'a'.

# Python 2, 7866 60 bytes

def f(a):
b=a%10!=0
while a*b!=a*b[::-1]:b+=1
print b


Try it online

• That's a nice answer, you can still golf it down. – Gurupad Mamadapur Jan 10 '17 at 13:49
• Improved it a bit by simplifying the b check. – Carra Jan 10 '17 at 13:50
• You can drop that ; and use a single space instead of 4 spaces. Also, use print b by changing it to Python 2. After all this it'll be 66 bytes. – Gurupad Mamadapur Jan 10 '17 at 14:04
• I've written too much c code in my life :) I think I could also simplifiy the str() casts when using python 2. – Carra Jan 10 '17 at 14:08
• Yea, I forgot that you can use a instead of str(a) in python 2, that's saves a lot too. Also, what encoding you are using, UTF-8 is fine here, so \n is 1 byte. – Gurupad Mamadapur Jan 10 '17 at 14:13

## Java, 113 bytes

i->{int j=1;for(String a;!new StringBuilder(a=String.valueOf(i*j)).reverse().toString().equals(a);)j++;return j;}


## Mathematica, 43 bytes

If[10∣#,0,#//.x_/;!PalindromeQ@x:>x+#]/#&


### Explanation

If[10∣#,0,...]/#&


If the input is a multiple of 10, return 0 (divided by the input).

#//....


Otherwise, repeatedly apply the following substitution to the input.

x_/;!PalindromeQ@x


Match a value which is not a palindrome.

...:>x+#


And replace that value with itself plus the input. This repeated substitution therefore searches through consecutive multiples of the input until it finds a palindrome.

.../#


Finally, to figure out b we simply divide the result by the input again.

## Python 2, 68 bytes

x,y=input(),1
if not x%10:x+''
while x*y!=y*x[::-1]:y+=1
print y


Outputs an error if input is a multiple of 10.

f a|amod10==0=0|1>0=[b|b<-[1..],show(a*b)==reverse(show\$a*b)]!!0

Returns 0 if a is a multiple of 10. Will check from 1 to infinity for b if the textual reverse of a*b is the same as a*b`.