Refactoring
This commit is contained in:
parent
f3b932a04e
commit
4f53fc814d
@ -8,18 +8,40 @@ Solution for Problem 21 of Project Euler
|
||||
https://projecteuler.net/problem=21 =#
|
||||
|
||||
using BenchmarkTools
|
||||
using Primes
|
||||
|
||||
function divisors(n)
|
||||
divisors = Int64[1]
|
||||
m = round(Int, n / 2)
|
||||
for i in 2:m
|
||||
if n % i == 0
|
||||
push!(divisors, i)
|
||||
|
||||
function sum_divisors(n)
|
||||
counter = 1
|
||||
factors = factor(n)
|
||||
factors_keys = keys(factors)
|
||||
|
||||
for i in factors_keys
|
||||
count = 1
|
||||
for j = 1:factors[i]
|
||||
count += i^j
|
||||
end
|
||||
counter *= count
|
||||
end
|
||||
return divisors
|
||||
|
||||
return counter - n
|
||||
end
|
||||
|
||||
function is_amicable(n)
|
||||
s = sum_divisors(n)
|
||||
if s == n
|
||||
return false
|
||||
end
|
||||
|
||||
sum_s = sum_divisors(s)
|
||||
if sum_s == n
|
||||
return true
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
|
||||
function Problem21()
|
||||
#=
|
||||
Let d(n) be defined as the sum of proper divisors of n (numbers
|
||||
@ -33,20 +55,14 @@ function Problem21()
|
||||
|
||||
Evaluate the sum of all the amicable numbers under 10000 =#
|
||||
|
||||
n = 9999
|
||||
s = zeros(Int, n)
|
||||
amicable = Int64[]
|
||||
for i in 2:n
|
||||
s[i] = sum(divisors(i))
|
||||
end
|
||||
|
||||
for i in 2:n
|
||||
if s[i] <= n && i != s[i] && i == s[s[i]]
|
||||
push!(amicable, i)
|
||||
counter = 0
|
||||
for i = 2:10_000
|
||||
if is_amicable(i)
|
||||
counter += i
|
||||
end
|
||||
end
|
||||
|
||||
return sum(amicable)
|
||||
return counter
|
||||
end
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user