From 1eb0fc0dc97e4ecc7297cc6c0b3c23f9e27baac5 Mon Sep 17 00:00:00 2001 From: daviddoji Date: Sun, 9 Oct 2022 19:19:52 +0200 Subject: [PATCH] Reduce allocations and formatting --- src/Julia/Problems001-050/Problem012.jl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Julia/Problems001-050/Problem012.jl b/src/Julia/Problems001-050/Problem012.jl index ed46943..fe7ecfc 100644 --- a/src/Julia/Problems001-050/Problem012.jl +++ b/src/Julia/Problems001-050/Problem012.jl @@ -33,17 +33,17 @@ function Problem12() What is the value of the first triangle number to have over five hundred divisors? =# - function num_divisors(n) - r = isqrt(n) - 2 * count(n % i == 0 for i in 1:r) - (r^2 == n) + function num_divisors(number::Int64) + res = isqrt(number) + return 2 * count(number % i == 0 for i = 1:res) - (res^2 == number) end - triangle = 0 - for i in Iterators.countfrom(1) - triangle += i - if num_divisors(triangle) > 500 - return string(triangle) + ans::Int64 = 0 + for number in Iterators.countfrom(1) + ans += number + if num_divisors(ans) > 500 + return ans end end end