Use benchmark macro

This commit is contained in:
David Doblas Jiménez 2021-09-28 10:14:40 +02:00
parent 89caae5244
commit e5f7e47213

View File

@ -5,7 +5,8 @@ Created on 08 Jun 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 2 of Project Euler Solution for Problem 2 of Project Euler
https://projecteuler.net/problem=2 =# https://projecteuler.net/problem=2
=#
using BenchmarkTools using BenchmarkTools
@ -14,15 +15,17 @@ function Problem2()
Each new term in the Fibonacci sequence is generated by adding the Each new term in the Fibonacci sequence is generated by adding the
previous two terms. By starting with 1 and 2, the first 10 terms will be: previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Find the sum of all the even-valued terms in the sequence which do not Find the sum of all the even-valued terms in the sequence which do not
exceed four million. =# exceed four million.
=#
ans = 0 ans = 0
limit = 4_000_000 limit = 4_000_000
x, y = 1, 1 x, y = 1, 1
z = x + y # Because every third Fibonacci number is even # Because every third Fibonacci number is even
z = x + y
while z <= limit while z <= limit
ans += z ans += z
x = y + z x = y + z
@ -37,4 +40,4 @@ end
println("Time to evaluate Problem 2:") println("Time to evaluate Problem 2:")
@btime Problem2() @btime Problem2()
println("") println("")
println("Result for Problem 2: ", Problem2()) println("Result for Problem 2: ", Problem2())