From: John Knight Date: 2002-08-22T07:30:36+09:00 Subject: Palindrome for integers 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