Refactoring

This commit is contained in:
David Doblas Jiménez 2023-04-11 19:09:54 +02:00
parent 28ba539204
commit 5a2ad49902
5 changed files with 51 additions and 44 deletions

View File

@ -6,26 +6,26 @@ Created on 25 Jul 2021
@email: daviddoji@pm.me
Solution for Problem 15 of Project Euler
https://projecteuler.net/problem=15 =#
https://projecteuler.net/problem=15
=#
using BenchmarkTools
function Problem15()
function Problem015()
#=
Starting in the top left corner of a 2×2 grid, and only being able to
move to the right and down, there are exactly 6 routes to the bottom
right corner.
How many such routes are there through a 20×20 grid? =#
n::Int8 = 20
numerator::BigInt = factorial(big(2n))
denominator1::BigInt = big(factorial(n))
denominator2::BigInt = big(factorial(2n - n))
return BigInt(numerator / (denominator1 * denominator2))
How many such routes are there through a 20×20 grid?
=#
n = 20
return BigInt(factorial(big(2n)) / (factorial(n) * big(factorial(2n-n))))
end
println("Time to evaluate Problem $(lpad(15, 3, "0")):")
@btime Problem15()
println("Took:")
@btime Problem015()
println("")
println("Result for Problem $(lpad(15, 3, "0")): ", Problem15())
println("Result for Problem $(lpad(15, 3, "0")): ", Problem015())

View File

@ -5,24 +5,26 @@ Created on 26 Jul 2021
@author: David Doblas Jiménez
@email: daviddoji@pm.me
Solution for Problem 16 of Project Euler
https://projecteuler.net/problem=16 =#
Solution for Problem 016 of Project Euler
https://projecteuler.net/problem=16
=#
using BenchmarkTools
function Problem16()
function Problem016()
#=
2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number 2^1000? =#
What is the sum of the digits of the number 2^1000?
=#
n::Int16 = 1_000
n = 1_000
return sum(digits(2^BigInt(n)))
return sum(digits(2^big(n)))
end
println("Time to evaluate Problem $(lpad(16, 3, "0")):")
@btime Problem16()
println("Took:")
@btime Problem016()
println("")
println("Result for Problem $(lpad(16, 3, "0")): ", Problem16())
println("Result for Problem $(lpad(16, 3, "0")): ", Problem016())

View File

@ -5,7 +5,8 @@ Created on 28 Jul 2021
@email: daviddoji@pm.me
Solution for Problem 17 of Project Euler
https://projecteuler.net/problem=17 =#
https://projecteuler.net/problem=17
=#
using BenchmarkTools
@ -33,7 +34,7 @@ function num2letters(num, nums)
end
end
function Problem17()
function Problem017()
#=
If the numbers 1 to 5 are written out in words: one, two, three, four,
five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
@ -44,7 +45,8 @@ function Problem17()
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and
forty-two) contains 23 letters and 115 (one hundred and fifteen) contains
20 letters. The use of "and" when writing out numbers is in compliance
with British usage. =#
with British usage.
=#
nums = Dict(
0 => 0,
@ -83,12 +85,11 @@ function Problem17()
for num = 1:n
letters += num2letters(num, nums)
end
return letters
end
println("Time to evaluate Problem $(lpad(17, 3, "0")):")
@btime Problem17()
println("Took:")
@btime Problem017()
println("")
println("Result for Problem $(lpad(17, 3, "0")): ", Problem17())
println("Result for Problem $(lpad(17, 3, "0")): ", Problem017())

View File

@ -4,12 +4,13 @@ Created on 01 Aug 2021
@author: David Doblas Jiménez
@email: daviddoji@pm.me
Solution for Problem 18 of Project Euler
https://projecteuler.net/problem=18 =#
Solution for Problem 018 of Project Euler
https://projecteuler.net/problem=18
=#
using BenchmarkTools
function Problem18()
function Problem018()
#=
By starting at the top of the triangle below and moving to adjacent
numbers on the row below, the maximum total from top to bottom is 23.
@ -21,7 +22,8 @@ function Problem18()
That is, 3 + 7 + 4 + 9 = 23.
Find the maximum total from top to bottom of the triangle above =#
Find the maximum total from top to bottom of the triangle above
=#
triangle = [ # Mutable
[75],
@ -52,7 +54,7 @@ function Problem18()
end
println("Time to evaluate Problem $(lpad(18, 3, "0")):")
@btime Problem18()
println("Took:")
@btime Problem018()
println("")
println("Result for Problem $(lpad(18, 3, "0")): ", Problem18())
println("Result for Problem $(lpad(18, 3, "0")): ", Problem018())

View File

@ -4,13 +4,14 @@ Created on 02 Aug 2021
@author: David Doblas Jiménez
@email: daviddoji@pm.me
Solution for Problem 19 of Project Euler
https://projecteuler.net/problem=19 =#
Solution for Problem 019 of Project Euler
https://projecteuler.net/problem=19
=#
using BenchmarkTools
using Dates
function Problem19()
function Problem019()
#=
You are given the following information, but you may prefer to do some
research for yourself.
@ -25,22 +26,23 @@ function Problem19()
unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth
century (1 Jan 1901 to 31 Dec 2000)? =#
century (1 Jan 1901 to 31 Dec 2000)?
=#
sundays = 0
ans = 0
for year = 1901:2000
for month = 1:12
if Dates.dayofweek(Date(year, month, 1)) == Dates.Sunday
sundays += 1
ans += 1
end
end
end
return sundays
return ans
end
println("Time to evaluate Problem $(lpad(19, 3, "0")):")
@btime Problem19()
println("Took: ")
@btime Problem019()
println("")
println("Result for Problem $(lpad(19, 3, "0")): ", Problem19())
println("Result for Problem $(lpad(19, 3, "0")): ", Problem019())