Reduce allocations and refactoring

This commit is contained in:
2023-04-05 22:04:20 +02:00
parent 9983ee23d6
commit 28ba539204
2 changed files with 19 additions and 15 deletions

View File

@@ -4,8 +4,9 @@ Created on 21 Jul 2021
@author: David Doblas Jiménez
@email: daviddoji@pm.me
Solution for Problem 12 of Project Euler
https://projecteuler.net/problem=12 =#
Solution for Problem 012 of Project Euler
https://projecteuler.net/problem=12
=#
using BenchmarkTools
@@ -16,7 +17,7 @@ function num_divisors(number::Int64)
end
function Problem12()
function Problem012()
#=
The sequence of triangle numbers is generated by adding the natural
numbers. So the 7th triangle number would be:
@@ -38,9 +39,10 @@ function Problem12()
We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred
divisors? =#
divisors?
=#
ans::Int64 = 0
ans = 0
for number in Iterators.countfrom(1)
ans += number
if num_divisors(ans) > 500
@@ -50,7 +52,7 @@ function Problem12()
end
println("Time to evaluate Problem $(lpad(12, 3, "0")):")
@btime Problem12()
println("Took:")
@btime Problem012()
println("")
println("Result for Problem $(lpad(12, 3, "0")): ", Problem12())
println("Result for Problem $(lpad(12, 3, "0")): ", Problem012())