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

43 Answers 43

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\$

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\$
1
\$\begingroup\$

AWK, 74 bytes

func f(n,r){return n?f(int(n/10),r*10+n%10):r}{for(x=$0;$0!=f($0);)$0+=x}1

reverse() - or better yet rev() - is the one function I'm always missing.

To Test:

awk 'func f(n,r){return n?f(int(n/10),r*10+n%10):r}{for(x=$0;$0!=f($0);)$0+=x}1' <<< 16

func f(n,r){return n?f(int(n/10),r*10+n%10):r} # reverse
{for(x=$0;$0!=f($0);)$0+=x}                    # while not palindrome
1                                              # print $0
\$\endgroup\$
1
\$\begingroup\$

Japt, 12 9 bytes

There's an 8 to be had here somehow.

È©vU}aÈsê

Try it

È©vU}aÈsê     :Implicit input of integer U
È             :Left function, taking an integer as argument
 ©            :  Logical AND with
  vU          :    Divisible by U?
    }         :End function
     a        :Pass the non-negative integers through the right function,
              : returning the first result that returns true
              : when passed through the left function
      È       :Right function, taking an integer as argument
       s      :  Convert to string
        ê     :    Palindromise and convert back to integer
\$\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\$
0
\$\begingroup\$

Uiua, 12 bytes

⍢⊸+(¬≍⇌.°⋕).

Try this online!

Explanation:

⍢⊸+(¬≍⇌.°⋕).
           . => duplicate N
⍢            => then while
   (¬≍⇌.°⋕)  => the number is not a palindrome,
 ⊸+          => keep adding N
\$\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.