The process of Reversal-Addition is where a number is added to it's reverse until the number created is a palindrome. For example, if we start with 68, the process would be:
$$68 + 86 \to 154 + 451 \to 605 + 506 \to 1111$$
As you can see, this took 3 additions to get to a palindromic number. If we were to start with \$89\$, we would need 24 steps (which you can see the breakdown here).
The world record for the most steps taken before a palindrome is reached is \$261\$, which occurs for the number \$1186060307891929990\$, producing a number larger than \$10^{118}\$. However, there have been quite a few numbers which we have not been able to get a palindrome. These are called Lychrel numbers.
Since we are working in base 10, we can really only call them candidates, because there exists no proof that these numbers never reach a palindrome. For example, the smallest base-10 Lychrel candidate is \$196\$, and has gone through well over a billion iterations. If the palindrome does exist, it is much larger than \$10^{10^{8.77}}\$. As comparison, if that many 1s was inscribed on atoms, we would need \$2.26772×10^{588843575}\$ universes worth of atoms to write it out, assuming it exists.
Your Task
Create a program or function that takes an integer input and returns or prints the number of steps required to reach a palindrome. You will not be required to deal with Lychrel candidates (i.e. Your program, when given a Lychrel candidate, is allowed to either throw an error or run forever).
Test Cases:
f(0) => 0
f(11) => 0
f(89) => 24
f(286) => 23
f(196196871) => 45
f(1005499526) => 109
f(1186060307891929990) => 261
Rules
Bonuses
- If you print out each addition step, formatted
n + rev(n) = m
, you may multiply your score by 0.75. The sums should print out before the number of steps. - If your code can detect if a number is a Lychrel candidate, you may multiply your score by 0.85. In this case it is sufficient to assume anything that takes more than 261 iterations is a Lychrel candidate. Either return nothing, or anything that is not a number that can be mistaken for a correct answer (etc: any string or a number not in the range 0-261). Any error does not count as valid output (ex. maximum recursion depth exceeded) and can not be used in the detection.
- If you complete both bonuses, multiply by 0.6.
This is code-golf, so least number of bytes wins.
This code snippet shows an example solution in Python 3 with both bonuses.
def do(n,c=0,s=''):
m = str(n)
o = m[::-1]
if c > 261:
return "Lychrel candidate"
if m == o:
print(s)
return c
else:
d = int(m)+int(o)
s+="%s + %s = %s"%(m,o,str(d))
return do(d,c+1,s)
*0.6
bonus on top of the others? Or is it just that? \$\endgroup\$10 + 01 = 11
or10 + 1 = 11
or is it up to us? \$\endgroup\$262
? \$\endgroup\$