Smallest Multiple to "Palindromize"

10

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)

Julian Lachniet

Posted 2017-01-10T11:55:57.023

Reputation: 3 216

Question was closed 2017-01-10T13:47:17.603

How do we know if the input has a solution or not? Or is figuring that out part of the challenge? – Luis Mendo – 2017-01-10T12:07:20.313

As far as I can tell, only multiples of 10 do not have solutions. – Julian Lachniet – 2017-01-10T12:07:56.353

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 – 2017-01-10T12:29:47.340

Some are harder to find (powers of 3), I needed some time for 243 and 729, but it seems to work for every n given enough time – G B – 2017-01-10T12:30:45.687

2Related Math.SE post. Yes, every number that isn't a multiple of 10 has a palindromic multiple. – Martin Ender – 2017-01-10T12:33:43.830

@JulianLachniet hey, you seems to like a leaderboard on your question, you can just copy the one in Pyramid of broken strings, change the QUESTION_ID and OVERRIDE_USER to this question id (106336) and to your id (well that question is yours too, so you don't need to change this one) – Rod – 2017-01-10T12:37:10.390

Can someone explain how to contact a moderator about reopening this question? – Julian Lachniet – 2017-01-10T13:53:56.490

4@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 – 2017-01-10T14:14:41.410

Answers

5

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. 

PurkkaKoodari

Posted 2017-01-10T11:55:57.023

Reputation: 16 699

2

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

G B

Posted 2017-01-10T11:55:57.023

Reputation: 11 099

2

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!

Emigna

Posted 2017-01-10T11:55:57.023

Reputation: 50 798

2

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!

betseg

Posted 2017-01-10T11:55:57.023

Reputation: 8 493

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 – 2017-01-10T13:29:25.950

@nmjcman101 thanks! I saw your comment before editing, tried ++m in various places, but it didn't work. – betseg – 2017-01-10T13:45:00.763

1

Java, 113 bytes

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

Roman Gräf

Posted 2017-01-10T11:55:57.023

Reputation: 2 915

1

Python 2, 78 66 60 bytes

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

Try it online

Carra

Posted 2017-01-10T11:55:57.023

Reputation: 301

That's a nice answer, you can still golf it down. – Gurupad Mamadapur – 2017-01-10T13:49:38.583

1Improved it a bit by simplifying the b check. – Carra – 2017-01-10T13:50:25.770

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 – 2017-01-10T14:04:49.927

I've written too much c code in my life :) I think I could also simplifiy the str() casts when using python 2. – Carra – 2017-01-10T14:08:43.623

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 – 2017-01-10T14:13:05.207

Good point, changed my editor byte count to use \n instead of \r\n. – Carra – 2017-01-10T15:37:25.733

1

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

steenbergh

Posted 2017-01-10T11:55:57.023

Reputation: 7 772

0

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.

Martin Ender

Posted 2017-01-10T11:55:57.023

Reputation: 184 808

0

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.

Renzeee

Posted 2017-01-10T11:55:57.023

Reputation: 599

0

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.

Gurupad Mamadapur

Posted 2017-01-10T11:55:57.023

Reputation: 1 791