```An article on slashdot recently pointed out that of all the numbers under
10000 (could have been heaps more...), only one number had not been
"solved".  It was 196.  Some bright spark has expanded it to numbers
containing millions of digits, and still no palindrome.

I think you made a typo in "169 does not settle on a palindrome after more
than 62k tries", as my program testifies:

\$ ./lycheral.rb 169
169
-> 169 + 961 = 1130
-> 1130 + 311 = 1441

In answer to your questions, I found your code difficult to follow, and
offer my own (written a few days ago) as an alternative.  It does not do
the same thing, rather you saw an example of its usage just above.  I
don't know anything about performance of large number operations, however.

(Code at the bottom.)

Cheers,
Gavin

>  Alan McConnel pointed me to this problem.
>
> Having written the program I have some questions.
>       Is it in good ruby style?
>       How could it be improved?
>       Is there a way to trade RAM for speed?
>       How does it do arithmetic on very long strings w/o taking more
> RAM?
>
> John
>
> Program follows:
>
> =begin
>  Test the hypothesis that for any integer the process of adding it to
> its reverse will eventually yield a palindrome, For example,
>  consider 69:  69+96=165;165+561=726;726+627=1353;1353+3531=4884
>
>  I rubified this and discovered this phenom:
>          The number 169 does not settle on a palindrome after
>          more than 62k tries. But while the program slows to two
>          tials a minute on a 233Mhz Pentiun, it does not increase
>          in RAM used. The length of the number generated
>          for a try is .41 times the try number. So attempt
>          62,000 involves adding 2 numbers 25,420 digits long.
>
>          Actually, the program decreased in size after it had
>          been running for a  short while. I imagine that the decrease
> came at a gc following the switch to bignuum.
> =end
>
> trylimit = 10
> 160.upto(170) do |j|             # check 160 to 170 for
> palindromeability
>
>   tststr = j.to_s                      # Would a reverse method for
> bignum help?
>   palfound = false                 # unnecesary variable?
>
>   trylimit.times do |tries|
> #puts tststr                        ############  trace
>     newstr = (tststr.to_i + tststr.reverse.to_i).to_s
>     if newstr == newstr.reverse
> #print " pal ", newstr, "\n"        ############# trace
>       palfound = true
>       break
>     else
>       tststr = newstr
>     end
> end
>     print j, " No palindrome in ", trylimit, " tries.\n"   unless
> palfound
> end

----------------------------------------------------------

#!/usr/local/bin/ruby -w

# A Lychrel number is one that DOES NOT form a palindrome
# when it is added to its reverse.

class Numeric
# 14541 -> true
# 1242345 -> false
def palindrome?
self == self.reverse
end

# 754 -> 457
def reverse
self.to_s.reverse.to_i
end
end

# 87 ->
# 87 + 78 = 165 ->
# 165 + 561 = 726 ->
# 726 + 627 = 1353 ->
# 1353 + 3531 = 4884, a palindrome
def lychrel_expansion(n)
puts n
until n.palindrome?
r = n.reverse
puts " -> #{n} + #{r} = #{n += r}"
end
end

# Handle arguments, which can be positive integers or
# ranges.

for n in ARGV
case n
when /^\d+\$/
lychrel_expansion(n.to_i)
puts "\n\n"
when /^(\d+)-(\d+)\$/
range = \$1.to_i .. \$2.to_i
for i in range
lychrel_expansion(i)
puts "\n"
end
end
end

```