# 196 algorithm code golf

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.

• Because your question is probably the only one involving the 196 algorithm. Making single-use tags is not useful. – Chris Jester-Young Jan 29 '11 at 8:48
• What I meant was, your question is likely to be the only one ever to involve this topic, even in 2 years' time. :-) – Chris Jester-Young Jan 29 '11 at 9:12
• @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 ;) – Eelvex Jan 29 '11 at 9:49
• @GigaWatt also, I had missread your fist question :) Just don't bother with A023108s' case. – Eelvex Mar 9 '12 at 16:03
• @Joel, as with A023108, just ignore them (act like you don't know about them); we don't know if any exists anyway. – Eelvex May 27 '12 at 15:02

# R, 193109 105 bytes

-84 bytes thanks to Giuseppe! -4 byes thanks to JayCe!

function(x){"-"=utf8ToInt
S=strtoi
"!"=rev
while({z=paste(S(x)+S(intToUtf8(!-x),10));any(-z!=!-z)})x=z
z}


Try it online!

• You can (and should) choose a different way of doing this than string manipulation, but here are some golfing tips for the method you've chosen: strsplit(x,"") is shorter than strsplit(x,NULL), and el(L) is shorter than L[]. as.double is shorter than as.numeric and strtoi is shorter than both; instead of setting t just use it directly in your if statement. also this is a recursive function if I'm not mistaken, so you need to put f= as part of your submission. – Giuseppe Aug 2 '18 at 15:51
• @Giuseppe Got it. Thanks for the tips. I'll keep working on this. It's easier for me to just get something that works then go back and optimize. – Robert S. Aug 2 '18 at 16:01
• Hehehe, no worries. If you're hell-bent on using strings (or forced to by the problem), consider utf8ToInt to convert to digits and intToUtf8 to convert back. That'll be a big byte saving! – Giuseppe Aug 2 '18 at 16:07
• Here is a 109 bytes golf using utf8ToInt and a while loop – Giuseppe Aug 2 '18 at 17:58
• Save 4 more bytes by using - in place of U. I also replaced rev with ! but it does not save any byte... – JayCe Aug 6 '18 at 17:02

# Javascript ES6, 56 bytes

f=a=>(g=_=>[...""+a].reverse().join)()==a?a:f(a+ +g())


# Dyalog APL, 17 bytes

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


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

## 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 }  # 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  # 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

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


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


# 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


# 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


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


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.

# 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:
;$R≈+ copy n, cast to string, reverse, cast to int, add ;$;R=Y      not palindrome (cast to string, copy, reverse, check inequality)


# J, 27 bytes

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


Try it online!

# Japt, 11 10 bytes

@¶ìw}a@±ìw


Try it

# Python 2, 53 bytes

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


Try it online!

# Ruby, 47 bytes

->a{v=a.to_s.reverse;v==a.to_s ? a:f[a+v.to_i]}


Try it online!

# 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!