Oxaric’s Blog

A compendium of amazing things…

Euler Project Problem #63 Solution

Posted by oxaric on November 24, 2008

It uses Ruby.


Click to directly download euler-solution-63.rb

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

# Euler Problem #63
# How many n-digit positive integers exist which are also an nth power?
# Example: 7^5 = 16807 and 16807 has 5 digits

def numOfDigits?( num )
   num.to_s().size
end

total = 0

for x in (1..9)
   for n in (1..21)
      temp = x ** n
      if n == numOfDigits?( temp )
         total = total + 1
         puts x.to_s + "^" + n.to_s + " = " + temp.to_s
      end
   end
end

puts "The answer is: " + total.to_s

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>