Refactor output

This commit is contained in:
David Doblas Jiménez 2023-08-28 18:36:30 +02:00
parent 39d820cca2
commit 768a3278dd

View File

@ -4,8 +4,9 @@ Created on 05 Aug 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 21 of Project Euler Solution for Problem 021 of Project Euler
https://projecteuler.net/problem=21 =# https://projecteuler.net/problem=21
=#
using BenchmarkTools using BenchmarkTools
using Primes using Primes
@ -23,10 +24,10 @@ function sum_divisors(n)
end end
counter *= count counter *= count
end end
return counter - n return counter - n
end end
function is_amicable(n) function is_amicable(n)
s = sum_divisors(n) s = sum_divisors(n)
if s == n if s == n
@ -37,12 +38,11 @@ function is_amicable(n)
if sum_s == n if sum_s == n
return true return true
end end
return false return false
end end
function Problem21() function Problem021()
#= #=
Let d(n) be defined as the sum of proper divisors of n (numbers Let d(n) be defined as the sum of proper divisors of n (numbers
less than n which divide evenly into n). less than n which divide evenly into n).
@ -53,20 +53,23 @@ function Problem21()
44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are
1, 2, 4, 71 and 142; so d(284) = 220. 1, 2, 4, 71 and 142; so d(284) = 220.
Evaluate the sum of all the amicable numbers under 10000 =# Evaluate the sum of all the amicable numbers under 10000
=#
counter = 0 # ans = 0
for i = 2:10_000 # for i = 2:10_000
if is_amicable(i) # if is_amicable(i)
counter += i # ans += i
end # end
end # end
return counter # return ans
ans = sum(i for i in 2:10_000 if is_amicable(i))
return ans
end end
println("Time to evaluate Problem $(lpad(21, 3, "0")):") println("Took:")
@btime Problem21() @btime Problem021()
println("") println("")
println("Result for Problem $(lpad(21, 3, "0")): ", Problem21()) println("Result for Problem $(lpad(21, 3, "0")): ", Problem021())