Created
August 15, 2012 17:33
-
-
Save black-structure/3361808 to your computer and use it in GitHub Desktop.
project euler 032
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'set' | |
s = Set.new | |
class Array | |
def pandigital?(r=(1..9)) | |
len = r.last - r.first + 1 | |
if length != len | |
false | |
else | |
ary = clone | |
while ary.last==0 | |
ary.pop | |
end | |
ary.select{|x| r===x}.uniq.length == len | |
end | |
end | |
end | |
class Integer | |
def digits(base=10) | |
x = self | |
a = [] | |
while x!=0 | |
x,r = x.divmod(base) | |
a << r; | |
end | |
return a | |
end | |
end | |
1.upto(99) do |a| | |
a.upto(9999) do |b| | |
c = a * b | |
digits = (c.digits + a.digits + b.digits) | |
break if digits.length > 9 | |
if digits.pandigital? 1..9 | |
s << c | |
end | |
end | |
end | |
puts s.reduce(:+) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment