Created
March 27, 2018 12:13
-
-
Save barsbek/a92416855d93646b4cb016d7a3132cdf to your computer and use it in GitHub Desktop.
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
def list_squared(m, n) | |
divs_hash = {} | |
(m..n).reduce([]) do |result, i| | |
sum = 1 | |
divs = [1] | |
for k in (2..i/2) | |
if (i % k).zero? | |
current = (i/k) | |
if divs_hash.has_key?(current.to_s) | |
divs_hash[current.to_s].each do |c| | |
unless divs.include?(c) | |
divs.push(c) | |
sum += c*c | |
end | |
end | |
end | |
unless divs.include?(k) | |
divs.push(k) | |
sum += k*k | |
end | |
end | |
end | |
unless i == 1 | |
divs.push(i) | |
sum += i*i | |
end | |
divs_hash[i.to_s] = divs | |
number = Math.sqrt(sum) | |
number == number.to_i ? result.push([i, sum]) : result | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment