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.)


  • 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)


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)
  • \$\begingroup\$ How do we know if the input has a solution or not? Or is figuring that out part of the challenge? \$\endgroup\$
    – Luis Mendo
    Jan 10, 2017 at 12:07
  • \$\begingroup\$ As far as I can tell, only multiples of 10 do not have solutions. \$\endgroup\$ Jan 10, 2017 at 12:07
  • \$\begingroup\$ 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? \$\endgroup\$
    – Fatalize
    Jan 10, 2017 at 12:29
  • 2
    \$\begingroup\$ Related Math.SE post. Yes, every number that isn't a multiple of 10 has a palindromic multiple. \$\endgroup\$ Jan 10, 2017 at 12:33
  • 4
    \$\begingroup\$ @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... \$\endgroup\$
    – steenbergh
    Jan 10, 2017 at 14:14

10 Answers 10


Pyth, 10 9 bytes


Try it online.


&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%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!

  • \$\begingroup\$ 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) \$\endgroup\$
    – nmjcman101
    Jan 10, 2017 at 13:29
  • \$\begingroup\$ @nmjcman101 thanks! I saw your comment before editing, tried ++m in various places, but it didn't work. \$\endgroup\$
    – betseg
    Jan 10, 2017 at 13:45

QBIC, 29 bytes



:      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, 78 66 60 bytes

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

Try it online

  • \$\begingroup\$ That's a nice answer, you can still golf it down. \$\endgroup\$ Jan 10, 2017 at 13:49
  • 1
    \$\begingroup\$ Improved it a bit by simplifying the b check. \$\endgroup\$
    – Carra
    Jan 10, 2017 at 13:50
  • \$\begingroup\$ 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. \$\endgroup\$ Jan 10, 2017 at 14:04
  • \$\begingroup\$ I've written too much c code in my life :) I think I could also simplifiy the str() casts when using python 2. \$\endgroup\$
    – Carra
    Jan 10, 2017 at 14:08
  • \$\begingroup\$ 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. \$\endgroup\$ Jan 10, 2017 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 the input is a multiple of 10, return 0 (divided by the input).


Otherwise, repeatedly apply the following substitution to the input.


Match a value which is not a palindrome.


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

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.


Haskell, 67 66 bytes

f a|a`mod`10==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.


Not the answer you're looking for? Browse other questions tagged or ask your own question.