Refactor output
This commit is contained in:
parent
75d29d32e6
commit
fdf280d070
@ -4,13 +4,13 @@ Created on 08 Jun 2021
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 1 of Project Euler
|
||||
Solution for Problem 001 of Project Euler
|
||||
https://projecteuler.net/problem=1
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
function Problem1()
|
||||
function Problem001()
|
||||
#=
|
||||
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.
|
||||
@ -24,7 +24,7 @@ function Problem1()
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(1, 3, "0")):")
|
||||
@btime Problem1()
|
||||
println("Took:")
|
||||
@btime Problem001()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(1, 3, "0")): ", Problem1())
|
||||
println("Result for Problem $(lpad(1, 3, "0")): ", Problem001())
|
||||
|
@ -4,12 +4,13 @@ Created on 08 Jun 2021
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 2 of Project Euler
|
||||
https://projecteuler.net/problem=2 =#
|
||||
Solution for Problem 002 of Project Euler
|
||||
https://projecteuler.net/problem=2
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
function Problem2()
|
||||
function Problem002()
|
||||
#=
|
||||
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:
|
||||
@ -17,7 +18,8 @@ function Problem2()
|
||||
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
|
||||
exceed four million. =#
|
||||
exceed four million.
|
||||
=#
|
||||
|
||||
ans = 0
|
||||
limit = 4_000_000
|
||||
@ -35,7 +37,7 @@ function Problem2()
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(2, 3, "0")):")
|
||||
@btime Problem2()
|
||||
println("Took: ")
|
||||
@btime Problem002()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(2, 3, "0")): ", Problem2())
|
||||
println("Result for Problem $(lpad(2, 3, "0")): ", Problem002())
|
||||
|
@ -4,16 +4,18 @@ Created on 15 Jun 2021
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 3 of Project Euler
|
||||
https://projecteuler.net/problem=3 =#
|
||||
Solution for Problem 003 of Project Euler
|
||||
https://projecteuler.net/problem=3
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
function Problem3()
|
||||
function Problem003()
|
||||
#=
|
||||
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
|
||||
factor = 2
|
||||
@ -28,7 +30,7 @@ function Problem3()
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(3, 3, "0")):")
|
||||
@btime Problem3()
|
||||
println("Took:")
|
||||
@btime Problem003()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(3, 3, "0")): ", Problem3())
|
||||
println("Result for Problem $(lpad(3, 3, "0")): ", Problem003())
|
||||
|
@ -4,8 +4,9 @@ Created on 17 Jun 2021
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 4 of Project Euler
|
||||
https://projecteuler.net/problem=4 =#
|
||||
Solution for Problem 004 of Project Euler
|
||||
https://projecteuler.net/problem=4
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
@ -20,12 +21,13 @@ function _ispalindrome(x::Integer, divider)
|
||||
end
|
||||
|
||||
|
||||
function Problem4()
|
||||
function Problem004()
|
||||
#=
|
||||
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.
|
||||
|
||||
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
|
||||
for i = 100:999, j = i:999
|
||||
@ -39,7 +41,7 @@ function Problem4()
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(4, 3, "0")):")
|
||||
@btime Problem4()
|
||||
println("Took:")
|
||||
@btime Problem004()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(4, 3, "0")): ", Problem4())
|
||||
println("Result for Problem $(lpad(4, 3, "0")): ", Problem004())
|
||||
|
@ -4,8 +4,9 @@ Created on 20 Jun 2021
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 5 of Project Euler
|
||||
https://projecteuler.net/problem=5 =#
|
||||
Solution for Problem 005 of Project Euler
|
||||
https://projecteuler.net/problem=5
|
||||
=#
|
||||
|
||||
#=
|
||||
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)
|
||||
|
||||
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
|
||||
|
||||
function Problem5()
|
||||
function Problem005()
|
||||
#=
|
||||
2520 is the smallest number that can be divided by each of the numbers
|
||||
from 1 to 10 without any remainder.
|
||||
|
||||
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
|
||||
for i = 1:21
|
||||
ans *= i ÷ gcd(i, ans)
|
||||
@ -33,7 +37,7 @@ function Problem5()
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(5, 3, "0")):")
|
||||
@btime Problem5()
|
||||
println("Took:")
|
||||
@btime Problem005()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(5, 3, "0")): ", Problem5())
|
||||
println("Result for Problem $(lpad(5, 3, "0")): ", Problem005())
|
||||
|
@ -4,12 +4,13 @@ Created on 20 Jun 2021
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 6 of Project Euler
|
||||
https://projecteuler.net/problem=6 =#
|
||||
Solution for Problem 006 of Project Euler
|
||||
https://projecteuler.net/problem=6
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
function Problem6()
|
||||
function Problem006()
|
||||
#=
|
||||
The sum of the squares of the first ten natural numbers is,
|
||||
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.
|
||||
|
||||
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
|
||||
square_of_sum = sum(i for i = 1:n)^2
|
||||
@ -32,7 +34,7 @@ function Problem6()
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(6, 3, "0")):")
|
||||
@btime Problem6()
|
||||
println("Took:")
|
||||
@btime Problem006()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(6, 3, "0")): ", Problem6())
|
||||
println("Result for Problem $(lpad(6, 3, "0")): ", Problem006())
|
||||
|
@ -4,20 +4,22 @@ Created on 24 Jun 2021
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 7 of Project Euler
|
||||
https://projecteuler.net/problem=7 =#
|
||||
Solution for Problem 007 of Project Euler
|
||||
https://projecteuler.net/problem=7
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
using Primes
|
||||
using Transducers
|
||||
|
||||
|
||||
function Problem7()
|
||||
function Problem007()
|
||||
#=
|
||||
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 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
|
||||
# 2 is prime but not included to speed-up
|
||||
@ -36,7 +38,7 @@ function Problem7()
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(7, 3, "0")):")
|
||||
@btime Problem7()
|
||||
println("Took:")
|
||||
@btime Problem007()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(7, 3, "0")): ", Problem7())
|
||||
println("Result for Problem $(lpad(7, 3, "0")): ", Problem007())
|
||||
|
@ -4,12 +4,13 @@ Created on 29 Jun 2021
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 8 of Project Euler
|
||||
https://projecteuler.net/problem=8 =#
|
||||
Solution for Problem 008 of Project Euler
|
||||
https://projecteuler.net/problem=8
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
function Problem8()
|
||||
function Problem008()
|
||||
#=
|
||||
The four adjacent digits in the 1000-digit number that have the
|
||||
greatest product are 9 × 9 × 8 × 9 = 5832.
|
||||
@ -17,7 +18,8 @@ function Problem8()
|
||||
731671...963450
|
||||
|
||||
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 = """
|
||||
73167176531330624919225119674426574742355349194934
|
||||
@ -62,7 +64,7 @@ function Problem8()
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(8, 3, "0")):")
|
||||
@btime Problem8()
|
||||
println("Took:")
|
||||
@btime Problem008()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(8, 3, "0")): ", Problem8())
|
||||
println("Result for Problem $(lpad(8, 3, "0")): ", Problem008())
|
||||
|
@ -4,12 +4,13 @@ Created on 01 Jul 2021
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 9 of Project Euler
|
||||
https://projecteuler.net/problem=9 =#
|
||||
Solution for Problem 009 of Project Euler
|
||||
https://projecteuler.net/problem=9
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
function Problem9()
|
||||
function Problem009()
|
||||
#=
|
||||
A Pythagorean triplet is a set of three natural numbers, a < b < c,
|
||||
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.
|
||||
|
||||
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
|
||||
Find the product abc. =#
|
||||
Find the product abc.
|
||||
=#
|
||||
|
||||
upper_limit = 1000
|
||||
for a = 1:upper_limit+1
|
||||
@ -32,7 +34,7 @@ function Problem9()
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(9, 3, "0")):")
|
||||
@btime Problem9()
|
||||
println("Took:")
|
||||
@btime Problem009()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(9, 3, "0")): ", Problem9())
|
||||
println("Result for Problem $(lpad(9, 3, "0")): ", Problem009())
|
||||
|
@ -4,8 +4,9 @@ Created on 03 Jul 2021
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 10 of Project Euler
|
||||
https://projecteuler.net/problem=10 =#
|
||||
Solution for Problem 010 of Project Euler
|
||||
https://projecteuler.net/problem=10
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
using Primes
|
||||
@ -14,13 +15,14 @@ function Problem10()
|
||||
#=
|
||||
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))
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(10, 3, "0")):")
|
||||
@btime Problem10()
|
||||
println("Took:")
|
||||
@btime Problem010()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(10, 3, "0")): ", Problem10())
|
||||
println("Result for Problem $(lpad(10, 3, "0")): ", Problem010())
|
||||
|
Loading…
x
Reference in New Issue
Block a user