35
\$\begingroup\$

Write a short program for 196-algorithm. The algorithm starts from an integer, then adds its reverse to it until a palindrome is reached.

e.g.

input = 5280
5280 + 0825 = 6105
6105 + 5016 = 11121
11121 + 12111 = 23232
output = 23232

Input

an integer, which is not a lyrchrel number (that is, it does eventually yield a palindrome under this algorithm, rather than continuing infinitely)

Output

the palindrome reached.

\$\endgroup\$
  • 6
    \$\begingroup\$ Because your question is probably the only one involving the 196 algorithm. Making single-use tags is not useful. \$\endgroup\$ – Chris Jester-Young Jan 29 '11 at 8:48
  • 2
    \$\begingroup\$ What I meant was, your question is likely to be the only one ever to involve this topic, even in 2 years' time. :-) \$\endgroup\$ – Chris Jester-Young Jan 29 '11 at 9:12
  • 1
    \$\begingroup\$ @Chris: Well, 196-algorithm is a pretty popular one, going by many different names. Just to be sure, though, I'll post another question about it before the 2-year-time lapses ;) \$\endgroup\$ – Eelvex Jan 29 '11 at 9:49
  • 1
    \$\begingroup\$ @GigaWatt also, I had missread your fist question :) Just don't bother with A023108s' case. \$\endgroup\$ – Eelvex Mar 9 '12 at 16:03
  • 1
    \$\begingroup\$ @Joel, as with A023108, just ignore them (act like you don't know about them); we don't know if any exists anyway. \$\endgroup\$ – Eelvex May 27 '12 at 15:02

46 Answers 46

1
\$\begingroup\$

Javascript ES6, 56 bytes

f=a=>(g=_=>[...""+a].reverse().join``)()==a?a:f(a+ +g())
\$\endgroup\$
1
\$\begingroup\$

Dyalog APL, 17 bytes

{⍵+⍎⌽⍕⍵}⍣{⍺≡⍎⌽⍕⍺}

{⍵+⍎⌽⍕⍵} add argument and its reverse...
... until...
{⍺≡⍎⌽⍕⍺} ... the result is a palindrome.

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

PHP, 57 Bytes

Try it online!

Code, recursive function

function f($n){echo(strrev($n)!=$n)?f(strrev($n)+$n):$n;}

Explanation

function f($n){
    echo(strrev($n)!=$n)? #check if non-palindrome
        f(strrev($n)+$n): #true, call again with $n + reverse $n
        $n;               #false (is a palindrome) echo $n

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

Java, 90 84 bytes

n->{for(long t;(t=new Long(new StringBuffer(n+"").reverse()+""))!=n;)n+=t;return n;}

-6 bytes thanks to @O.O.Balance.

Try it online.

Explanation:

n->{           // Method with long as both parameter and return-type
  for(long t;  //  Temp-long, starting uninitialized
      (t=new Long(new StringBuffer(n+"").reverse()+""))
               //  Before every iteration, reverse the temp-long
      !=n;)    //  And loop as long as it's not equal to `n` yet
    n+=t;      //   Add this temp-long to the input
  return n;}   //  Return the modified input as result
\$\endgroup\$
1
\$\begingroup\$

Perl 6, 36 bytes

{($_,{$_+.flip}...{$_==.flip})[*-1]}

Try it online!

An anonymous code block that returns the last element of a sequence defined by:

  • The first element is the first parameter
  • The i+1th element is the ith element plus the reverse of itself
  • And ends when the element is equal to its reverse
\$\endgroup\$
1
\$\begingroup\$

C (gcc), 132 bytes

t;r(char*s){char*e=s+strlen(s)-1;for(;e>s;*e--=*s,*s++=t)t=*e;}g(o,l,f){for(l=malloc(9);sprintf(l,"%d",o),r(l),f=atoi(l),f-o;)o+=f;}

Call with g(n) for an int n. Try it online here.

Ungolfed:

t; // temporary varaiable fpr swapping characters
r(char *s) { // helper function to reverse a string: takes a string argument and modifies it
    char *e = s + strlen(s) - 1; // find the end of the string
    for(; e > s; // move the end pointer and the start pointer towards each other until they meet
        *e-- = *s, *s++ = t) // swap the characters under the start and end pointers ...
        t = *e; // ... using the temporary variable
}
g(o, // function for the 196 algorithm: takes an int argument and returns an int
  l, f) { // abusing the argument list to declare to extra variables: one is a string for reversing the number, the other is an int
    for(l = malloc(9); // allocate space for the string
        sprintf(l, "%d", o), // convert the number to a string
        r(l), // reverse that string
        f = atoi(l), // convert it back to an int
        f - o; ) // loop until the two numbers are equal
        o += f; // add the two numbers
}
\$\endgroup\$
1
\$\begingroup\$

MATL, 18 bytes

`Vt2&P=?5M.}1MvUsT

Try it on MATL Online

         % (implicit input)
`        % do-while loop
  Vt     % convert the number into a string, duplicate it
  2&P    % flip the copy left-to-right
  =      % are they equal? 
    ?5M. % if yes, push the number back on the stack and exit
         % (implicit output display)
    }1M  % else, push the number and its reverse (as strings) again on to the stack
    vUs  % convert them to numbers and add them
    T    % "True" value to continue loop, this time with the sum as the input number
         % (implicit loop end)
\$\endgroup\$
1
\$\begingroup\$

Husk, 7 bytes

¤Ω`S↔=+

Try it online!

Alternatively we could use ΩS=↔S+↔ for the same amount of bytes, but I like the one above more.

Explanation

¤Ω`S↔=+
¤        -- compose the arguments of
 Ω       -- | iterate second function until first is truthy
         -- with
  `S     -- | flipped S: applying binary function to itself and
    ↔    -- | | itself reversed
         -- first function: check if palindrome (\x-> x == reverse x)
         -- second function: "196ify" (\x-> x + reverse x)
\$\endgroup\$
1
\$\begingroup\$

Ruby, 55 bytes

l=->x{x.reverse==x ? x:l[(x.to_i+x.reverse.to_i).to_s]}

Different approach than the other Ruby one, ended up golfing it down to one byte fewer

Explanation: makes a lambda l that recursively calls itself, each time adding the number's reverse, until the string is a palindrome

Slightly less golfed version:

func = ->x do
  (x.reverse == x) ? x : func[(x.to_i + x.reverse.to_i).to_s]
end
\$\endgroup\$
1
\$\begingroup\$

Excel VBA, 49 bytes

An immediate window function which takes input from range [A1] and outputs to the VBE immediate window.

n=[A1]:Do:n=n+r:r=StrReverse(n):Loop While n-r:?n
\$\endgroup\$
1
\$\begingroup\$

Lua 5.3.3, 57 bytes

Takes i as input, prints output:

r=0while i~=r do i=i+r|0r=0+(i..""):reverse()end print(i)

More readable version:

r=0
while i~=r do
  i=i+r|0
  r=0+(i..""):reverse()
end
print(i)

Super simple. Just repeatedly adds i to its reverse until i is equal to its reverse. Then it just prints the new value of i.

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

Actually, 16 bytes

;WX;$R≈+;$;R=YWX

Try it online!

Explanation:

;WX;$R≈+;$;R=YWX
;                 push a copy of n
 W            W   while top of stack is truthy:
  X                 pop and discard
   ;$R≈+            copy n, cast to string, reverse, cast to int, add
        ;$;R=Y      not palindrome (cast to string, copy, reverse, check inequality)
               X  pop and discard
\$\endgroup\$
1
\$\begingroup\$

J, 27 bytes

(+&.".|.)^:(-.@-:|.)^:_&.":

Try it online!

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

Japt, 11 10 bytes

@¶ìw}a@±ìw

Try it

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

Python 2, 53 bytes

f=lambda x,y=0:x if x==y else f(x+y,int(`x+y`[::-1]))

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Wren, 102 bytes

A super long program.

Fn.new{|x|
while(x!=x[-1..0])x=(Num.fromString(x)+Num.fromString(x[-1..0])).toString
System.write(x)
}

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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