Skip to content

Instantly share code, notes, and snippets.

@hyfather
Created November 14, 2011 06:20
Show Gist options
  • Save hyfather/1363373 to your computer and use it in GitHub Desktop.
Save hyfather/1363373 to your computer and use it in GitHub Desktop.
My solution to problem #3 on projecteuler.net
def factorize(list, num, p)
while true
list.each_with_index { |e, i| list[i] = 0 if e!=p && e%p == 0 }
list.each{|e| p = e and break if e > p}
if num % p == 0
# p num
return factorize(list[p..Math.sqrt(num/=p)], num, p)
end
return num if list.empty?
end
end
big = 600851475143
big_list = (2..Math.sqrt(big)).to_a
p factorize(big_list, big, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment