Last active
February 17, 2020 07:43
-
-
Save dkorduban/6aa8888e011a8473fc81dca44ba81827 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
local function divisors_sum(n) | |
local sum = 0 | |
if n > 1 then | |
sum = 1 | |
end | |
local k = 2 | |
while k*k <= n do | |
if n % k == 0 then | |
sum = sum + k + n/k | |
if k*k == n then | |
sum = sum - k | |
end | |
end | |
k = k + 1 | |
end | |
return sum | |
end | |
function is_amicable(n) | |
local d = divisors_sum(n) | |
return d ~= n and divisors_sum(d) == n | |
end | |
local max_n = 1000000 | |
local sum = 0 | |
for n = 2, max_n do | |
if is_amicable(n) then | |
sum = sum + n | |
end | |
end | |
print(sum) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment