Refactor output

This commit is contained in:
David Doblas Jiménez 2023-04-01 18:12:04 +02:00
parent 75d29d32e6
commit fdf280d070
10 changed files with 88 additions and 68 deletions

View File

@ -4,13 +4,13 @@ Created on 08 Jun 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 1 of Project Euler Solution for Problem 001 of Project Euler
https://projecteuler.net/problem=1 https://projecteuler.net/problem=1
=# =#
using BenchmarkTools using BenchmarkTools
function Problem1() function Problem001()
#= #=
If we list all the natural numbers below 10 that are multiples of 3 or 5, If we list all the natural numbers below 10 that are multiples of 3 or 5,
we get 3, 5, 6 and 9. The sum of these multiples is 23. we get 3, 5, 6 and 9. The sum of these multiples is 23.
@ -24,7 +24,7 @@ function Problem1()
end end
println("Time to evaluate Problem $(lpad(1, 3, "0")):") println("Took:")
@btime Problem1() @btime Problem001()
println("") println("")
println("Result for Problem $(lpad(1, 3, "0")): ", Problem1()) println("Result for Problem $(lpad(1, 3, "0")): ", Problem001())

View File

@ -4,12 +4,13 @@ Created on 08 Jun 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 2 of Project Euler Solution for Problem 002 of Project Euler
https://projecteuler.net/problem=2 =# https://projecteuler.net/problem=2
=#
using BenchmarkTools using BenchmarkTools
function Problem2() function Problem002()
#= #=
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:
@ -17,7 +18,8 @@ function Problem2()
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
@ -35,7 +37,7 @@ function Problem2()
end end
println("Time to evaluate Problem $(lpad(2, 3, "0")):") println("Took: ")
@btime Problem2() @btime Problem002()
println("") println("")
println("Result for Problem $(lpad(2, 3, "0")): ", Problem2()) println("Result for Problem $(lpad(2, 3, "0")): ", Problem002())

View File

@ -4,16 +4,18 @@ Created on 15 Jun 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 3 of Project Euler Solution for Problem 003 of Project Euler
https://projecteuler.net/problem=3 =# https://projecteuler.net/problem=3
=#
using BenchmarkTools using BenchmarkTools
function Problem3() function Problem003()
#= #=
The prime factors of 13195 are 5, 7, 13 and 29. The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ? =# What is the largest prime factor of the number 600851475143 ?
=#
ans = 600_851_475_143 ans = 600_851_475_143
factor = 2 factor = 2
@ -28,7 +30,7 @@ function Problem3()
end end
println("Time to evaluate Problem $(lpad(3, 3, "0")):") println("Took:")
@btime Problem3() @btime Problem003()
println("") println("")
println("Result for Problem $(lpad(3, 3, "0")): ", Problem3()) println("Result for Problem $(lpad(3, 3, "0")): ", Problem003())

View File

@ -4,8 +4,9 @@ Created on 17 Jun 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 4 of Project Euler Solution for Problem 004 of Project Euler
https://projecteuler.net/problem=4 =# https://projecteuler.net/problem=4
=#
using BenchmarkTools using BenchmarkTools
@ -20,12 +21,13 @@ function _ispalindrome(x::Integer, divider)
end end
function Problem4() function Problem004()
#= #=
A palindromic number reads the same both ways. The largest palindrome made A palindromic number reads the same both ways. The largest palindrome made
from the product of two 2-digit numbers is 9009 = 91 x 99. from the product of two 2-digit numbers is 9009 = 91 x 99.
Find the largest palindrome made from the product of two 3-digit numbers. =# Find the largest palindrome made from the product of two 3-digit numbers.
=#
ans = 10_001 ans = 10_001
for i = 100:999, j = i:999 for i = 100:999, j = i:999
@ -39,7 +41,7 @@ function Problem4()
end end
println("Time to evaluate Problem $(lpad(4, 3, "0")):") println("Took:")
@btime Problem4() @btime Problem004()
println("") println("")
println("Result for Problem $(lpad(4, 3, "0")): ", Problem4()) println("Result for Problem $(lpad(4, 3, "0")): ", Problem004())

View File

@ -4,8 +4,9 @@ Created on 20 Jun 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 5 of Project Euler Solution for Problem 005 of Project Euler
https://projecteuler.net/problem=5 =# https://projecteuler.net/problem=5
=#
#= #=
The LCM of two natural numbers x and y is given by: The LCM of two natural numbers x and y is given by:
@ -13,17 +14,20 @@ def lcm(x, y):
return x * y // math.gcd(x, y) return x * y // math.gcd(x, y)
It is possible to compute the LCM of more than two numbers by iteratively It is possible to compute the LCM of more than two numbers by iteratively
computing the LCM of two numbers, i.e. LCM(a, b, c) = LCM(a, LCM(b, c)) =# computing the LCM of two numbers, i.e. LCM(a, b, c) = LCM(a, LCM(b, c))
=#
using BenchmarkTools using BenchmarkTools
function Problem5() function Problem005()
#= #=
2520 is the smallest number that can be divided by each of the numbers 2520 is the smallest number that can be divided by each of the numbers
from 1 to 10 without any remainder. from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of What is the smallest positive number that is evenly divisible by all of
the numbers from 1 to 20? =# the numbers from 1 to 20?
=#
ans = 1 ans = 1
for i = 1:21 for i = 1:21
ans *= i ÷ gcd(i, ans) ans *= i ÷ gcd(i, ans)
@ -33,7 +37,7 @@ function Problem5()
end end
println("Time to evaluate Problem $(lpad(5, 3, "0")):") println("Took:")
@btime Problem5() @btime Problem005()
println("") println("")
println("Result for Problem $(lpad(5, 3, "0")): ", Problem5()) println("Result for Problem $(lpad(5, 3, "0")): ", Problem005())

View File

@ -4,12 +4,13 @@ Created on 20 Jun 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 6 of Project Euler Solution for Problem 006 of Project Euler
https://projecteuler.net/problem=6 =# https://projecteuler.net/problem=6
=#
using BenchmarkTools using BenchmarkTools
function Problem6() function Problem006()
#= #=
The sum of the squares of the first ten natural numbers is, The sum of the squares of the first ten natural numbers is,
1^2 + 2^2 + ... + 10^2 = 385 1^2 + 2^2 + ... + 10^2 = 385
@ -21,7 +22,8 @@ function Problem6()
natural numbers and the square of the sum is 3025 385 = 2640. natural numbers and the square of the sum is 3025 385 = 2640.
Find the difference between the sum of the squares of the first one Find the difference between the sum of the squares of the first one
hundred natural numbers and the square of the sum. Statement =# hundred natural numbers and the square of the sum.
=#
n = 100 n = 100
square_of_sum = sum(i for i = 1:n)^2 square_of_sum = sum(i for i = 1:n)^2
@ -32,7 +34,7 @@ function Problem6()
end end
println("Time to evaluate Problem $(lpad(6, 3, "0")):") println("Took:")
@btime Problem6() @btime Problem006()
println("") println("")
println("Result for Problem $(lpad(6, 3, "0")): ", Problem6()) println("Result for Problem $(lpad(6, 3, "0")): ", Problem006())

View File

@ -4,20 +4,22 @@ Created on 24 Jun 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 7 of Project Euler Solution for Problem 007 of Project Euler
https://projecteuler.net/problem=7 =# https://projecteuler.net/problem=7
=#
using BenchmarkTools using BenchmarkTools
using Primes using Primes
using Transducers using Transducers
function Problem7() function Problem007()
#= #=
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13,
we can see that the 6th prime is 13. we can see that the 6th prime is 13.
What is the 10_001st prime number =# What is the 10_001st prime number
=#
count::Int32 = 10_000 count::Int32 = 10_000
# 2 is prime but not included to speed-up # 2 is prime but not included to speed-up
@ -36,7 +38,7 @@ function Problem7()
end end
println("Time to evaluate Problem $(lpad(7, 3, "0")):") println("Took:")
@btime Problem7() @btime Problem007()
println("") println("")
println("Result for Problem $(lpad(7, 3, "0")): ", Problem7()) println("Result for Problem $(lpad(7, 3, "0")): ", Problem007())

View File

@ -4,12 +4,13 @@ Created on 29 Jun 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 8 of Project Euler Solution for Problem 008 of Project Euler
https://projecteuler.net/problem=8 =# https://projecteuler.net/problem=8
=#
using BenchmarkTools using BenchmarkTools
function Problem8() function Problem008()
#= #=
The four adjacent digits in the 1000-digit number that have the The four adjacent digits in the 1000-digit number that have the
greatest product are 9 × 9 × 8 × 9 = 5832. greatest product are 9 × 9 × 8 × 9 = 5832.
@ -17,7 +18,8 @@ function Problem8()
731671...963450 731671...963450
Find the thirteen adjacent digits in the 1000-digit number that have Find the thirteen adjacent digits in the 1000-digit number that have
the greatest product. What is the value of this product? =# the greatest product. What is the value of this product?
=#
NUM::String = """ NUM::String = """
73167176531330624919225119674426574742355349194934 73167176531330624919225119674426574742355349194934
@ -62,7 +64,7 @@ function Problem8()
end end
println("Time to evaluate Problem $(lpad(8, 3, "0")):") println("Took:")
@btime Problem8() @btime Problem008()
println("") println("")
println("Result for Problem $(lpad(8, 3, "0")): ", Problem8()) println("Result for Problem $(lpad(8, 3, "0")): ", Problem008())

View File

@ -4,12 +4,13 @@ Created on 01 Jul 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 9 of Project Euler Solution for Problem 009 of Project Euler
https://projecteuler.net/problem=9 =# https://projecteuler.net/problem=9
=#
using BenchmarkTools using BenchmarkTools
function Problem9() function Problem009()
#= #=
A Pythagorean triplet is a set of three natural numbers, a < b < c, A Pythagorean triplet is a set of three natural numbers, a < b < c,
for which a^2 + b^2 = c^2 for which a^2 + b^2 = c^2
@ -17,7 +18,8 @@ function Problem9()
For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
There exists exactly one Pythagorean triplet for which a + b + c = 1000. There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc. =# Find the product abc.
=#
upper_limit = 1000 upper_limit = 1000
for a = 1:upper_limit+1 for a = 1:upper_limit+1
@ -32,7 +34,7 @@ function Problem9()
end end
println("Time to evaluate Problem $(lpad(9, 3, "0")):") println("Took:")
@btime Problem9() @btime Problem009()
println("") println("")
println("Result for Problem $(lpad(9, 3, "0")): ", Problem9()) println("Result for Problem $(lpad(9, 3, "0")): ", Problem009())

View File

@ -4,8 +4,9 @@ Created on 03 Jul 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 10 of Project Euler Solution for Problem 010 of Project Euler
https://projecteuler.net/problem=10 =# https://projecteuler.net/problem=10
=#
using BenchmarkTools using BenchmarkTools
using Primes using Primes
@ -14,13 +15,14 @@ function Problem10()
#= #=
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million. =# Find the sum of all the primes below two million.
=#
return sum(primes(1_999_999)) return sum(primes(1_999_999))
end end
println("Time to evaluate Problem $(lpad(10, 3, "0")):") println("Took:")
@btime Problem10() @btime Problem010()
println("") println("")
println("Result for Problem $(lpad(10, 3, "0")): ", Problem10()) println("Result for Problem $(lpad(10, 3, "0")): ", Problem010())