19
5
Palindrome Reversal-Addition
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 => 154 + 451 => 605 + 506 => 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 10118. 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 10108.77. As comparison, if that many 1s was inscribed on atoms, we would need 2.26772×10588843575 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)
Related – Sp3000 – 2015-06-18T20:24:17.813
1Is the
*0.6
bonus on top of the others? Or is it just that? – Maltysen – 2015-06-18T20:59:36.193@Maltysen Just the 0.6. – Kade – 2015-06-18T21:00:25.190
When printing out the sums should we print
10 + 01 = 11
or10 + 1 = 11
or is it up to us? – Martin Ender – 2015-06-18T21:02:05.700@Martin Büttner You can do either of the two you specified. – Kade – 2015-06-18T21:05:10.353
3For the lychrel detector, can I print out
262
? – Maltysen – 2015-06-18T21:10:17.037Or like
999
or something else similarly ridculous? – Maltysen – 2015-06-18T21:11:31.150@Maltysen I've updated the thread to make this clearer, you can print any number that can't be a possible value, since we assume anything over 261 is a Lychrel candidate, and it's not possible to go below 0. – Kade – 2015-06-18T21:50:12.343
I read that as _Palindrome Reversal-__Addiction___ (like OCD) and I thought that's the most pointless thing! Palindromes don't need to be reversed! – CJ Dennis – 2015-06-21T11:30:13.597
If the seed number ends in '0', I assume it's reverse drops the leading zeros? E.g. if the seed is 50, the first calculation will be 50 + 5 i.e. 55. If the seed is 300, the first calculation will be 300 + 3 = 303? – Steve Ives – 2015-06-22T14:52:24.533
@SteveIves Yes. – Kade – 2015-06-22T15:09:37.987