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
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