Reduce allocations and refactoring

This commit is contained in:
David Doblas Jiménez 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 @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 12 of Project Euler Solution for Problem 012 of Project Euler
https://projecteuler.net/problem=12 =# https://projecteuler.net/problem=12
=#
using BenchmarkTools using BenchmarkTools
@ -16,7 +17,7 @@ function num_divisors(number::Int64)
end end
function Problem12() function Problem012()
#= #=
The sequence of triangle numbers is generated by adding the natural The sequence of triangle numbers is generated by adding the natural
numbers. So the 7th triangle number would be: 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. 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 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) for number in Iterators.countfrom(1)
ans += number ans += number
if num_divisors(ans) > 500 if num_divisors(ans) > 500
@ -50,7 +52,7 @@ function Problem12()
end end
println("Time to evaluate Problem $(lpad(12, 3, "0")):") println("Took:")
@btime Problem12() @btime Problem012()
println("") println("")
println("Result for Problem $(lpad(12, 3, "0")): ", Problem12()) println("Result for Problem $(lpad(12, 3, "0")): ", Problem012())

View File

@ -4,8 +4,9 @@ Created on 24 Jul 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 14 of Project Euler Solution for Problem 014 of Project Euler
https://projecteuler.net/problem=14 =# https://projecteuler.net/problem=14
=#
using BenchmarkTools using BenchmarkTools
@ -18,7 +19,7 @@ function chain_length(n)
return length return length
end end
function Problem14() function Problem014()
#= #=
The following iterative sequence is defined for the set of positive The following iterative sequence is defined for the set of positive
integers: integers:
@ -37,7 +38,8 @@ function Problem14()
Which starting number, under one million, produces the longest chain? Which starting number, under one million, produces the longest chain?
NOTE: Once the chain starts the terms are allowed to go above one million. =# NOTE: Once the chain starts the terms are allowed to go above one million.
=#
ans = 0 ans = 0
limit = 1_000_000 limit = 1_000_000
@ -53,7 +55,7 @@ function Problem14()
end end
println("Time to evaluate Problem $(lpad(14, 3, "0")):") println("Took:")
@btime Problem14() @btime Problem014()
println("") println("")
println("Result for Problem $(lpad(14, 3, "0")): ", Problem14()) println("Result for Problem $(lpad(14, 3, "0")): ", Problem014())