Oxaric’s Blog

A compendium of amazing things…

Euler Project Problem #30 Solution

Posted by oxaric on November 24, 2008

It uses Ruby.


Click to directly download euler-solution-30.rb

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

# Euler Problem #30
# Find the sum of all the numbers that can be written as the sum of fifth powers of their digits

def isSumOfItsFifths?( num )

   number_s = num.to_s
   sum = 0

   for i in (0...number_s.size)
      sum += (number_s[i, 1].to_i ** 5)
   end

   if (sum == num )
      true
   else
      false
   end
end

= 1600
sum = 0

while (< 443840)
   if isSumOfItsFifths?( i )
      sum += i
      puts "FOUND: " + i.to_s
      puts "Current sum: " + sum.to_s
      puts
   end

   i += 1
end

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>