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 @email: daviddoji@pm.me
Solution for Problem 15 of Project Euler Solution for Problem 15 of Project Euler
https://projecteuler.net/problem=15 =# https://projecteuler.net/problem=15
=#
using BenchmarkTools using BenchmarkTools
function Problem15() function Problem015()
#= #=
Starting in the top left corner of a 2×2 grid, and only being able to 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 move to the right and down, there are exactly 6 routes to the bottom
right corner. right corner.
How many such routes are there through a 20×20 grid? =# How many such routes are there through a 20×20 grid?
n::Int8 = 20 =#
numerator::BigInt = factorial(big(2n))
denominator1::BigInt = big(factorial(n)) n = 20
denominator2::BigInt = big(factorial(2n - n)) return BigInt(factorial(big(2n)) / (factorial(n) * big(factorial(2n-n))))
return BigInt(numerator / (denominator1 * denominator2))
end end
println("Time to evaluate Problem $(lpad(15, 3, "0")):") println("Took:")
@btime Problem15() @btime Problem015()
println("") 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 @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 16 of Project Euler Solution for Problem 016 of Project Euler
https://projecteuler.net/problem=16 =# https://projecteuler.net/problem=16
=#
using BenchmarkTools using BenchmarkTools
function Problem16() function Problem016()
#= #=
2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. 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 end
println("Time to evaluate Problem $(lpad(16, 3, "0")):") println("Took:")
@btime Problem16() @btime Problem016()
println("") 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 @email: daviddoji@pm.me
Solution for Problem 17 of Project Euler Solution for Problem 17 of Project Euler
https://projecteuler.net/problem=17 =# https://projecteuler.net/problem=17
=#
using BenchmarkTools using BenchmarkTools
@ -33,7 +34,7 @@ function num2letters(num, nums)
end end
end end
function Problem17() function Problem017()
#= #=
If the numbers 1 to 5 are written out in words: one, two, three, four, 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. 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 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 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 20 letters. The use of "and" when writing out numbers is in compliance
with British usage. =# with British usage.
=#
nums = Dict( nums = Dict(
0 => 0, 0 => 0,
@ -83,12 +85,11 @@ function Problem17()
for num = 1:n for num = 1:n
letters += num2letters(num, nums) letters += num2letters(num, nums)
end end
return letters return letters
end end
println("Time to evaluate Problem $(lpad(17, 3, "0")):") println("Took:")
@btime Problem17() @btime Problem017()
println("") 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 @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 18 of Project Euler Solution for Problem 018 of Project Euler
https://projecteuler.net/problem=18 =# https://projecteuler.net/problem=18
=#
using BenchmarkTools using BenchmarkTools
function Problem18() function Problem018()
#= #=
By starting at the top of the triangle below and moving to adjacent 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. 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. 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 triangle = [ # Mutable
[75], [75],
@ -52,7 +54,7 @@ function Problem18()
end end
println("Time to evaluate Problem $(lpad(18, 3, "0")):") println("Took:")
@btime Problem18() @btime Problem018()
println("") 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 @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 19 of Project Euler Solution for Problem 019 of Project Euler
https://projecteuler.net/problem=19 =# https://projecteuler.net/problem=19
=#
using BenchmarkTools using BenchmarkTools
using Dates using Dates
function Problem19() function Problem019()
#= #=
You are given the following information, but you may prefer to do some You are given the following information, but you may prefer to do some
research for yourself. research for yourself.
@ -25,22 +26,23 @@ function Problem19()
unless it is divisible by 400. unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth 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 year = 1901:2000
for month = 1:12 for month = 1:12
if Dates.dayofweek(Date(year, month, 1)) == Dates.Sunday if Dates.dayofweek(Date(year, month, 1)) == Dates.Sunday
sundays += 1 ans += 1
end end
end end
end end
return sundays return ans
end end
println("Time to evaluate Problem $(lpad(19, 3, "0")):") println("Took: ")
@btime Problem19() @btime Problem019()
println("") println("")
println("Result for Problem $(lpad(19, 3, "0")): ", Problem19()) println("Result for Problem $(lpad(19, 3, "0")): ", Problem019())