Reduce allocations and formatting

This commit is contained in:
David Doblas Jiménez 2022-10-09 19:19:52 +02:00
parent 4d53ec2b82
commit 1eb0fc0dc9

View File

@ -33,17 +33,17 @@ function Problem12()
What is the value of the first triangle number to have over five hundred What is the value of the first triangle number to have over five hundred
divisors? =# divisors? =#
function num_divisors(n) function num_divisors(number::Int64)
r = isqrt(n) res = isqrt(number)
2 * count(n % i == 0 for i in 1:r) - (r^2 == n) return 2 * count(number % i == 0 for i = 1:res) - (res^2 == number)
end end
triangle = 0 ans::Int64 = 0
for i in Iterators.countfrom(1) for number in Iterators.countfrom(1)
triangle += i ans += number
if num_divisors(triangle) > 500 if num_divisors(ans) > 500
return string(triangle) return ans
end end
end end
end end