24
\$\begingroup\$

Given a positive integer N, output the smallest positive integer such that this number is a palindrome (i.e. is its own reverse) and is divisible by N.

The palindrome (i.e. the output) must not need a leading zero to be a palindrome, e.g. 080 is not the valid answer for 16.

The input will never be a multiple of 10, because of the previous reason.

Your program may take as much time as necessary, even if in practice it would be way too long to output the answer.

Inputs and outputs

  • You may take the input through STDIN, as a function argument, or anything similar.
  • You may print the output to STDOUT, return it from a function, or anything similar.
  • Inputs and outputs must be in the decimal base.

Test cases

N        Output
1        1
2        2
16       272
17       272
42       252
111      111
302      87278
1234     28382

Scoring

This is , so the shortest answer in bytes wins.

\$\endgroup\$
7
  • \$\begingroup\$ Will the input be divisible by 10? \$\endgroup\$
    – Leaky Nun
    Commented Aug 25, 2016 at 7:01
  • \$\begingroup\$ @LeakyNun No, because then there is no solution since the palindrome must not need a leading zero. I will make that explicit. \$\endgroup\$
    – Fatalize
    Commented Aug 25, 2016 at 7:04
  • \$\begingroup\$ Will the input be positive? \$\endgroup\$
    – Wheat Wizard
    Commented Aug 25, 2016 at 14:01
  • 1
    \$\begingroup\$ @WheatWizard Yes: Given a positive integer N \$\endgroup\$
    – Fatalize
    Commented Aug 25, 2016 at 14:29
  • \$\begingroup\$ @Fatalize sorry. I don't know how I missed it. \$\endgroup\$
    – Wheat Wizard
    Commented Aug 25, 2016 at 14:38

41 Answers 41

1
2
1
\$\begingroup\$

dc, 85 78 76 bytes

[pq]sq?ddsI[dddZ0sR[1-dsD10r^rdsN10%*lR+sRlN10/lDd0<@]ds@xlR++=q+lIrlLx]dsLx

Run:

dc -f program.dc <<< "16"

Output:

272

Previously submitted code and explanation:

    # loads register 'q' with an exit function
[pq]sq
    # register '@' contains an iterative function to reverse a number (312 to 213),
#by calculating one coefficient at a time of the new base 10 polynomial
[1-dsD10r^rdsN10%*lR+sRlN10/lDd0<@]s@
    # register 'L' implements the logic. It iteratively compares the current value
#with the generated reversed one, exiting if equal, otherwise increments the value
#by N and repeats.
[dddZ0sRl@xlR++=q+lIrlLx]sL
    # the "main". It reads the input, then calls the solver function from 'L'.
?ddsIlLx
\$\endgroup\$
1
\$\begingroup\$

Racket 174 bytes

(let((p?(λ(m)(equal? m(string->number(list->string(reverse(string->list(number->string m)))))))))
(let loop((N n))(cond[(and(p? N)(= 0(modulo N n)))N][else(loop(add1 N))])))

Ungolfed:

(define (f1 n)
  (let ((p? (λ (m)(equal? m
                          (string->number
                           (list->string
                            (reverse
                             (string->list
                              (number->string m)))))))))
    (let loop ((N n))
      (cond
        [(and (p? N) (= 0 (modulo N n))) N]
        [else (loop (add1 N))]
        ))))

Testing:

(f 1)
(f 2)
(f 16)
(f 17)
(f 42)
(f 111)
(f 302)
(f 1234)

Output:

1
2
272
272
252
111
87278
28382
\$\endgroup\$
1
\$\begingroup\$

Ruby, 50 bytes

->n{a=n;while(s=a.to_s;s!=s.reverse)do a+=n;end;a}

It's horrible, I know.

\$\endgroup\$
1
\$\begingroup\$

C, not 83, 80 bytes

f(n,b,k,z){for(b=1;;){for(z=0,k=b*n;z+=k%10,k/=10;z*=10);if(b++*n==z)return z;}}

For say the true i had seen in one other puzzle one programmer use the same algo number=swapped(number) => number is palindrome. How is possible to edit barred text?

#include <stdio.h>
main()
{unsigned  a[]={0,1,2,3,16,17,42,111,302,1234}, i;
 for(i=0;i<10;++i)
   printf("f(a[%u])=f(%u)=%u\n", i, a[i], f(a[i], 0, 0, 0)); 
 return 0;
}

/*
 f(a[0])=f(0)=0
 f(a[1])=f(1)=1
 f(a[2])=f(2)=2
 f(a[3])=f(3)=3
 f(a[4])=f(16)=272
 f(a[5])=f(17)=272
 f(a[6])=f(42)=252
 f(a[7])=f(111)=111
 f(a[8])=f(302)=87278
 f(a[9])=f(1234)=28382
 */
\$\endgroup\$
1
\$\begingroup\$

Clojure, 71 bytes

#(first(for[i(rest(range))i[(str(* % i))]:when(=(seq i)(reverse i))]i))

Just one long for with a two-step generation of i.

\$\endgroup\$
1
\$\begingroup\$

Mathematica, 28 bytes

#//.x_/;!PalindromeQ@x:>x+#&

Explanation

#//....

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.

\$\endgroup\$
1
\$\begingroup\$

Mathematica, 34 28 bytes

#//.x_/;!PalindromeQ@x:>x+#&

Unnamed function taking a positive integer input and returning a positive integer. To be read as: "Starting with the function argument #, repeatedly replace any x we see that is not a palindrome with x+#." Yes, Virginia, there is a PalindromeQ! It only works in base 10 though.

Original submission:

(i=1;While@!PalindromeQ[++i#];i#)&
\$\endgroup\$
4
  • \$\begingroup\$ I count 34, but here's 33: (i=0;While@!PalindromeQ[i+=#];i)& or (For[i=0,!PalindromeQ[i+=#],];i)& or (For[i=#,!PalindromeQ@i,i+=#];i)&. \$\endgroup\$ Commented Jan 10, 2017 at 15:07
  • \$\begingroup\$ Revisiting this answer, I see that I've learned some tricks since August! Down to 28 :) \$\endgroup\$ Commented Jan 10, 2017 at 18:25
  • \$\begingroup\$ Good thing that duplicate answers have been allowed since August. ;) \$\endgroup\$ Commented Jan 10, 2017 at 18:27
  • \$\begingroup\$ #$%^&*%#$*&^%!@... ;) \$\endgroup\$ Commented Jan 10, 2017 at 18:29
1
\$\begingroup\$

Japt, 12 bytes

_s ꬩZvU}a1

Try it


Alternative

_¥sw «ZuU}aÄ

Try it

\$\endgroup\$
1
\$\begingroup\$

RProgN, 31 Bytes

►x=01¿1+]x*]''.S.\''.≡!}x*

Explained

►x=01¿1+]x*]''.S.\''.≡!}x*  # ► Defines spaceless segment.
 x=                         # Define the input as x
   01                       # Push 0, 1 to the stack. 1 is used as a truthy for the while, 0 is our incrementing product.
     ¿                 }    # While the value on top of the stack (popped) is truthy. 
      1+                    # Increment the product by one.
        ]x*                 # Duplicate the producter, multiply by the input.
           ]''.S.\''.≡!     # Check to see if the result is a palindrome.
           ]''.             # Push a duplicate of the multiplied number, convert to a string.
               S.           # Convert it to a stack and re-concatinate it. This reverses the string.
                 \''.≡!     # Flip the top two values, exposing the original product. Conver it to a string and test inverse equality.
                        x*  # Once the loop ends, the last number times the input will be a palindrome. Thus, do that.

The equality sugar was only added after the discovery of this challenge, as was the bugfix that allowed strings to be used in spaceless segments.

Try it Online!

\$\endgroup\$
1
\$\begingroup\$

Vyxal, 6 bytes

{:Ḃ≠|+

Try it Online!

Same as my Thunno 2 answer.

Explanation

{:Ḃ≠|+  # Implicit input
{       # While loop
 :      # (condition)  duplicate current number
   ≠    #              not equal to
  Ḃ     #              its reverse
    |+  # (body)  add the input
        # Implicit output
\$\endgroup\$
0
\$\begingroup\$

Thunno 2, 5 bytes

(ḲQ;+

Try it online!

Explanation

(ḲQ;+  # Implicit input
(      # While loop
  Q    # (condition)  not equal to
 Ḳ     #              its reverse
   ;+  # (body)  add the input
       # Implicit output
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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