Created
August 12, 2012 07:41
-
-
Save lune-sta/3330516 to your computer and use it in GitHub Desktop.
Project Euler 21
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 'prime' | |
arr = Array.new(10001){1} | |
sum = 0 | |
2.upto(10000) do |i| | |
i.prime_division.each do |j| | |
a = 0 | |
(j[1]+1).times do |k| | |
a += j[0]**k | |
end | |
arr[i] *= a | |
end | |
arr[i] -= i | |
end | |
1.upto(10000) do |i| | |
(i+1).upto(10000) do |j| | |
sum += i + j if arr[i] == j && arr[j] == i | |
end | |
end | |
puts sum # 31626 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment