Oxaric’s Blog

A compendium of amazing things…

Euler Project Problem #55 Solution

Posted by oxaric on November 24, 2008

It uses Ruby.


Click to directly download euler-solution-55.rb

# filename 'euler-solution-55.rb'
# By: Louis Casillas, oxaric@gmail.com

# Euler Problem #55
# How many Lychrel numbers are there below ten-thousand?

def isPalindrome?( num )
   num_s = num.to_s

   if (num_s == num_s.reverse)
      return true
   else
      return false
   end
end

def isALychrelNumber?( num )
   i = 0
   
   temp = num   
   while (< 50)
      temp = temp + temp.to_s.reverse.to_i
      
      if isPalindrome?( temp )
         return false
      end

      i += 1
   end

   true
end

total = 0

for i in (1...10000)

   if isALychrelNumber?( i )
      total += 1
   end
end

puts "There are " + total.to_s + " Lychrel numbers below 10,000"

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>