This commit is contained in:
David Doblas Jiménez
2021-10-27 11:27:14 +02:00
parent 846a9343ae
commit d9c96f32cf
51 changed files with 315 additions and 366 deletions

1
.ignored_words.txt Normal file
View File

@@ -0,0 +1 @@
ans

View File

@@ -17,7 +17,7 @@ repos:
rev: 21.9b0 rev: 21.9b0
hooks: hooks:
- id: black - id: black
- repo: https://github.com/codespell-project/codespell #- repo: https://github.com/codespell-project/codespell
rev: v2.1.0 # rev: v2.1.0
hooks: # hooks:
- id: codespell # - id: codespell -I .ignored_words.txt

View File

@@ -24,7 +24,7 @@ function Problem1()
end end
println("Time to evaluate Problem 1:") println("Time to evaluate Problem $(lpad(1, 3, "0")):")
@btime Problem1() @btime Problem1()
println("") println("")
println("Result for Problem 1: ", Problem1()) println("Result for Problem $(lpad(1, 3, "0")): ", Problem1())

View File

@@ -5,8 +5,7 @@ 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
@@ -18,8 +17,7 @@ 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
@@ -37,7 +35,7 @@ function Problem2()
end end
println("Time to evaluate Problem 2:") println("Time to evaluate Problem $(lpad(2, 3, "0")):")
@btime Problem2() @btime Problem2()
println("") println("")
println("Result for Problem 2: ", Problem2()) println("Result for Problem $(lpad(2, 3, "0")): ", Problem2())

View File

@@ -5,8 +5,7 @@ Created on 15 Jun 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 3 of Project Euler Solution for Problem 3 of Project Euler
https://projecteuler.net/problem=3 https://projecteuler.net/problem=3 =#
=#
using BenchmarkTools using BenchmarkTools
@@ -14,8 +13,7 @@ function Problem3()
#= #=
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
@@ -30,7 +28,7 @@ function Problem3()
end end
println("Time to evaluate Problem 3:") println("Time to evaluate Problem $(lpad(3, 3, "0")):")
@btime Problem3() @btime Problem3()
println("") println("")
println("Result for Problem 3: ", Problem3()) println("Result for Problem $(lpad(3, 3, "0")): ", Problem3())

View File

@@ -39,7 +39,7 @@ function Problem4()
end end
println("Time to evaluate Problem 4:") println("Time to evaluate Problem $(lpad(4, 3, "0")):")
@btime Problem4() @btime Problem4()
println("") println("")
println("Result for Problem 4: ", Problem4()) println("Result for Problem $(lpad(4, 3, "0")): ", Problem4())

View File

@@ -5,8 +5,7 @@ Created on 20 Jun 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 5 of Project Euler Solution for Problem 5 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:
@@ -14,8 +13,7 @@ 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
@@ -25,8 +23,7 @@ function Problem5()
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 in 1:21 for i in 1:21
ans *= i ÷ gcd(i, ans) ans *= i ÷ gcd(i, ans)
@@ -36,7 +33,7 @@ function Problem5()
end end
println("Time to evaluate Problem 5:") println("Time to evaluate Problem $(lpad(5, 3, "0")):")
@btime Problem5() @btime Problem5()
println("") println("")
println("Result for Problem 5: ", Problem5()) println("Result for Problem $(lpad(5, 3, "0")): ", Problem5())

View File

@@ -5,8 +5,9 @@ Created on 20 Jun 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 6 of Project Euler Solution for Problem 6 of Project Euler
https://projecteuler.net/problem=6 https://projecteuler.net/problem=6 =#
=#
using BenchmarkTools
function Problem6() function Problem6()
#= #=
@@ -20,8 +21,7 @@ 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. Statement =#
=#
n = 100 n = 100
square_of_sum = sum(i for i in (1:n))^2 square_of_sum = sum(i for i in (1:n))^2
sum_squares = sum(i^2 for i in 1:n) sum_squares = sum(i^2 for i in 1:n)
@@ -30,7 +30,7 @@ function Problem6()
end end
println("Time to evaluate Problem 6:") println("Time to evaluate Problem $(lpad(6, 3, "0")):")
@time Problem6() @btime Problem6()
println("") println("")
println("Result for Problem 6: ", Problem6()) println("Result for Problem $(lpad(6, 3, "0")): ", Problem6())

View File

@@ -5,9 +5,9 @@ Created on 24 Jun 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 7 of Project Euler Solution for Problem 7 of Project Euler
https://projecteuler.net/problem=7 https://projecteuler.net/problem=7 =#
=#
using BenchmarkTools
using Primes using Primes
function Problem7() function Problem7()
@@ -15,13 +15,12 @@ function Problem7()
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 =#
=#
number = 2 number = 2
primeList = Int[] primeList = Int[]
while length(primeList) < 10_001 while length(primeList) < 10_001
if isprime(number) if isprime(number)
append!(primeList,number) append!(primeList, number)
end end
number += 1 number += 1
end end
@@ -30,7 +29,7 @@ function Problem7()
end end
println("Time to evaluate Problem 7:") println("Time to evaluate Problem $(lpad(7, 3, "0")):")
@time Problem7() @btime Problem7()
println("") println("")
println("Result for Problem 7: ", Problem7()) println("Result for Problem $(lpad(7, 3, "0")): ", Problem7())

View File

@@ -5,8 +5,9 @@ Created on 29 Jun 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 8 of Project Euler Solution for Problem 8 of Project Euler
https://projecteuler.net/problem=8 https://projecteuler.net/problem=8 =#
=#
using BenchmarkTools
function Problem8() function Problem8()
#= #=
@@ -16,8 +17,7 @@ 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 = """ NUM = """
73167176531330624919225119674426574742355349194934 73167176531330624919225119674426574742355349194934
@@ -62,7 +62,7 @@ function Problem8()
end end
println("Time to evaluate Problem 8:") println("Time to evaluate Problem $(lpad(8, 3, "0")):")
@time Problem8() @btime Problem8()
println("") println("")
println("Result for Problem 8: ", Problem8()) println("Result for Problem $(lpad(8, 3, "0")): ", Problem8())

View File

@@ -5,8 +5,9 @@ Created on 01 Jul 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 9 of Project Euler Solution for Problem 9 of Project Euler
https://projecteuler.net/problem=9 https://projecteuler.net/problem=9 =#
=#
using BenchmarkTools
function Problem9() function Problem9()
#= #=
@@ -16,8 +17,7 @@ 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 in 1:upper_limit + 1 for a in 1:upper_limit + 1
@@ -32,7 +32,7 @@ function Problem9()
end end
println("Time to evaluate Problem 9:") println("Time to evaluate Problem $(lpad(9, 3, "0")):")
@time Problem9() @btime Problem9()
println("") println("")
println("Result for Problem 9: ", Problem9()) println("Result for Problem $(lpad(9, 3, "0")): ", Problem9())

View File

@@ -5,23 +5,22 @@ Created on 03 Jul 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 10 of Project Euler Solution for Problem 10 of Project Euler
https://projecteuler.net/problem=10 https://projecteuler.net/problem=10 =#
=#
using BenchmarkTools
using Primes using Primes
function Problem10() 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 10:") println("Time to evaluate Problem $(lpad(10, 3, "0")):")
@time Problem10() @btime Problem10()
println("") println("")
println("Result for Problem 10: ", Problem10()) println("Result for Problem $(lpad(10, 3, "0")): ", Problem10())

View File

@@ -5,10 +5,9 @@ Created on 21 Jul 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 12 of Project Euler Solution for Problem 12 of Project Euler
https://projecteuler.net/problem=12 https://projecteuler.net/problem=12 =#
=#
using BenchmarkTools
function Problem12() function Problem12()
#= #=
@@ -32,8 +31,7 @@ function Problem12()
We can see that 28 is the first triangle number to have over five divisors. We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred What is the value of the first triangle number to have over five hundred
divisors? divisors? =#
=#
function num_divisors(n) function num_divisors(n)
r = isqrt(n) r = isqrt(n)
@@ -51,7 +49,7 @@ function Problem12()
end end
println("Time to evaluate Problem 12:") println("Time to evaluate Problem $(lpad(12, 3, "0")):")
@time Problem12() @btime Problem12()
println("") println("")
println("Result for Problem 12: ", Problem12()) println("Result for Problem $(lpad(12, 3, "0")): ", Problem12())

View File

@@ -1,4 +1,4 @@
using Base: String using Base:String
#= #=
Created on 22 Jul 2021 Created on 22 Jul 2021
@@ -6,22 +6,20 @@ Created on 22 Jul 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 13 of Project Euler Solution for Problem 13 of Project Euler
https://projecteuler.net/problem=13 https://projecteuler.net/problem=13 =#
=#
using BenchmarkTools
using DoubleFloats using DoubleFloats
# using JSON
function Problem13() function Problem13()
#= #=
Work out the first ten digits of the sum of the following one-hundred Work out the first ten digits of the sum of the following one-hundred
50-digit numbers 50-digit numbers =#
=# return string(sum(parse.(BigInt, readlines("../files/Problem13.txt"))))[1:10]
return string(sum(parse.(BigInt,readlines("../files/Problem13.txt"))))[1:10]
end end
println("Time to evaluate Problem 13:") println("Time to evaluate Problem $(lpad(13, 3, "0")):")
@time Problem13() @btime Problem13()
println("") println("")
println("Result for Problem 13: ", Problem13()) println("Result for Problem $(lpad(13, 3, "0")): ", Problem13())

View File

@@ -5,16 +5,17 @@ Created on 24 Jul 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 14 of Project Euler Solution for Problem 14 of Project Euler
https://projecteuler.net/problem=14 https://projecteuler.net/problem=14 =#
=#
function chain_length(n)#, terms) using BenchmarkTools
function chain_length(n)# , terms)
length = 0 length = 0
while n > 1 while n > 1
n = iseven(n) ? n >> 1 : 3n + 1 n = iseven(n) ? n >> 1 : 3n + 1
length += 1 length += 1
end end
return length return length
end end
function Problem14() function Problem14()
@@ -36,8 +37,7 @@ function Problem14()
Which starting number, under one million, produces the longest chain? Which starting number, under one million, produces the longest chain?
NOTE: Once the chain starts the terms are allowed to go above one million. NOTE: Once the chain starts the terms are allowed to go above one million. =#
=#
ans = 0 ans = 0
limit = 1_000_000 limit = 1_000_000
@@ -48,12 +48,12 @@ function Problem14()
score = longest score = longest
ans = i ans = i
end end
end end
return ans return ans
end end
println("Time to evaluate Problem 14:") println("Time to evaluate Problem $(lpad(14, 3, "0")):")
@time Problem14() @btime Problem14()
println("") println("")
println("Result for Problem 14: ", Problem14()) println("Result for Problem $(lpad(14, 3, "0")): ", Problem14())

View File

@@ -1,4 +1,4 @@
using Base: Integer using Base:Integer
#= #=
Created on 25 Jul 2021 Created on 25 Jul 2021
@@ -6,8 +6,9 @@ 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
function Problem15() function Problem15()
#= #=
@@ -15,14 +16,13 @@ function Problem15()
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 = 20 n = 20
return Integer(factorial(big(2n)) / (factorial(big(n)) * factorial(big(2n - n)))) return Integer(factorial(big(2n)) / (factorial(big(n)) * factorial(big(2n - n))))
end end
println("Time to evaluate Problem 15:") println("Time to evaluate Problem $(lpad(15, 3, "0")):")
@time Problem15() @btime Problem15()
println("") println("")
println("Result for Problem 15: ", Problem15()) println("Result for Problem $(lpad(15, 3, "0")): ", Problem15())

View File

@@ -1,4 +1,4 @@
using Base: Integer using Base:Integer
#= #=
Created on 26 Jul 2021 Created on 26 Jul 2021
@@ -6,21 +6,21 @@ Created on 26 Jul 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 16 of Project Euler Solution for Problem 16 of Project Euler
https://projecteuler.net/problem=16 https://projecteuler.net/problem=16 =#
=#
using BenchmarkTools
function Problem16() function Problem16()
#= #=
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 = 1000 n = 1000
return sum(parse(Int, d) for d in string(2^BigInt(n))) return sum(parse(Int, d) for d in string(2^BigInt(n)))
end end
println("Time to evaluate Problem 16:") println("Time to evaluate Problem $(lpad(16, 3, "0")):")
@time Problem16() @btime Problem16()
println("") println("")
println("Result for Problem 16: ", Problem16()) println("Result for Problem $(lpad(16, 3, "0")): ", Problem16())

View File

@@ -5,8 +5,9 @@ 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
function num2letters(num, dic) function num2letters(num, dic)
if num <= 20 if num <= 20
@@ -39,8 +40,7 @@ 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 => "", 1 => "one", 2 => "two", 3 => "three", 4 => "four", 5 => "five", 0 => "", 1 => "one", 2 => "two", 3 => "three", 4 => "four", 5 => "five",
6 => "six", 7 => "seven", 8 => "eight", 9 => "nine", 10 => "ten", 6 => "six", 7 => "seven", 8 => "eight", 9 => "nine", 10 => "ten",
@@ -60,7 +60,7 @@ function Problem17()
end end
println("Time to evaluate Problem 17:") println("Time to evaluate Problem $(lpad(17, 3, "0")):")
@time Problem17() @btime Problem17()
println("") println("")
println("Result for Problem 17: ", Problem17()) println("Result for Problem $(lpad(17, 3, "0")): ", Problem17())

View File

@@ -7,6 +7,8 @@ Created on 01 Aug 2021
Solution for Problem 18 of Project Euler Solution for Problem 18 of Project Euler
https://projecteuler.net/problem=18 =# https://projecteuler.net/problem=18 =#
using BenchmarkTools
function Problem18() function Problem18()
#= #=
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
@@ -49,7 +51,7 @@ function Problem18()
end end
println("Time to evaluate Problem 18:") println("Time to evaluate Problem $(lpad(18, 3, "0")):")
@time Problem18() @btime Problem18()
println("") println("")
println("Result for Problem 18: ", Problem18()) println("Result for Problem $(lpad(18, 3, "0")): ", Problem18())

View File

@@ -7,6 +7,7 @@ Created on 02 Aug 2021
Solution for Problem 19 of Project Euler Solution for Problem 19 of Project Euler
https://projecteuler.net/problem=19 =# https://projecteuler.net/problem=19 =#
using BenchmarkTools
using Dates using Dates
function Problem19() function Problem19()
@@ -37,7 +38,7 @@ function Problem19()
end end
println("Time to evaluate Problem 19:") println("Time to evaluate Problem $(lpad(19, 3, "0")):")
@time Problem19() @btime Problem19()
println("") println("")
println("Result for Problem 19: ", Problem19()) println("Result for Problem $(lpad(19, 3, "0")): ", Problem19())

View File

@@ -7,6 +7,8 @@ Created on 03 Aug 2021
Solution for Problem 20 of Project Euler Solution for Problem 20 of Project Euler
https://projecteuler.net/problem=20 =# https://projecteuler.net/problem=20 =#
using BenchmarkTools
function Problem20() function Problem20()
#= #=
n! means n × (n 1) × ... × 3 × 2 × 1 n! means n × (n 1) × ... × 3 × 2 × 1
@@ -21,7 +23,7 @@ function Problem20()
end end
println("Time to evaluate Problem 20:") println("Time to evaluate Problem $(lpad(20, 3, "0")):")
@time Problem20() @btime Problem20()
println("") println("")
println("Result for Problem 20: ", Problem20()) println("Result for Problem $(lpad(20, 3, "0")): ", Problem20())

View File

@@ -7,6 +7,7 @@ Created on 05 Aug 2021
Solution for Problem 21 of Project Euler Solution for Problem 21 of Project Euler
https://projecteuler.net/problem=21 =# https://projecteuler.net/problem=21 =#
using BenchmarkTools
function divisors(n) function divisors(n)
divisors = Int64[1] divisors = Int64[1]
@@ -49,7 +50,7 @@ function Problem21()
end end
println("Time to evaluate Problem 21:") println("Time to evaluate Problem $(lpad(21, 3, "0")):")
@time Problem21() @btime Problem21()
println("") println("")
println("Result for Problem 21: ", Problem21()) println("Result for Problem $(lpad(21, 3, "0")): ", Problem21())

View File

@@ -5,9 +5,9 @@ Created on 08 Aug 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 22 of Project Euler Solution for Problem 22 of Project Euler
https://projecteuler.net/problem=22 https://projecteuler.net/problem=22 =#
=#
using BenchmarkTools
using DelimitedFiles using DelimitedFiles
function Problem22() function Problem22()
@@ -21,20 +21,19 @@ function Problem22()
is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So,
COLIN would obtain a score of 938 × 53 = 49714. COLIN would obtain a score of 938 × 53 = 49714.
What is the total of all the name scores in the file? What is the total of all the name scores in the file? =#
=#
file = "/datos/Scripts/Gitea/Project_Euler/src/files/Problem22.txt" file = "/datos/Scripts/Gitea/Project_Euler/src/files/Problem22.txt"
names = sort(readdlm(file, ',', String)[:]) names = sort(readdlm(file, ',', String)[:])
result = 0 result = 0
for (idx,name) in enumerate(names) for (idx, name) in enumerate(names)
result += sum(Int(c) - 64 for c in name) * idx result += sum(Int(c) - 64 for c in name) * idx
end end
return result return result
end end
println("Time to evaluate Problem 22:") println("Time to evaluate Problem $(lpad(22, 3, "0")):")
@time Problem22() @btime Problem22()
println("") println("")
println("Result for Problem 22: ", Problem22()) println("Result for Problem $(lpad(22, 3, "0")): ", Problem22())

View File

@@ -8,6 +8,8 @@ Solution for Problem 23 of Project Euler
https://projecteuler.net/problem=23 https://projecteuler.net/problem=23
=# =#
using BenchmarkTools
function Problem23() function Problem23()
#= #=
A perfect number is a number for which the sum of its proper divisors is A perfect number is a number for which the sum of its proper divisors is
@@ -54,7 +56,7 @@ function Problem23()
end end
println("Time to evaluate Problem 23:") println("Time to evaluate Problem $(lpad(23, 3, "0")):")
@time Problem23() @btime Problem23()
println("") println("")
println("Result for Problem 23: ", Problem23()) println("Result for Problem $(lpad(23, 3, "0")): ", Problem23())

View File

@@ -5,9 +5,9 @@ Created on 13 Aug 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 24 of Project Euler Solution for Problem 24 of Project Euler
https://projecteuler.net/problem=24 https://projecteuler.net/problem=24 =#
=#
using BenchmarkTools
using Combinatorics using Combinatorics
function Problem24() function Problem24()
@@ -20,15 +20,14 @@ function Problem24()
012 021 102 120 201 210 012 021 102 120 201 210
What is the millionth lexicographic permutation of the digits What is the millionth lexicographic permutation of the digits
0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? =#
=#
digits = [0,1,2,3,4,5,6,7,8,9] digits = [0,1,2,3,4,5,6,7,8,9]
_permutations = nthperm(digits, 1_000_000) _permutations = nthperm(digits, 1_000_000)
return join(_permutations) return join(_permutations)
end end
println("Time to evaluate Problem 24:") println("Time to evaluate Problem $(lpad(24, 3, "0")):")
@time Problem24() @btime Problem24()
println("") println("")
println("Result for Problem 24: ", Problem24()) println("Result for Problem $(lpad(24, 3, "0")): ", Problem24())

View File

@@ -5,8 +5,9 @@ Created on 15 Aug 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 25 of Project Euler Solution for Problem 25 of Project Euler
https://projecteuler.net/problem=25 https://projecteuler.net/problem=25 =#
=#
using BenchmarkTools
function Problem25() function Problem25()
#= #=
@@ -32,13 +33,12 @@ function Problem25()
The 12th term, F12, is the first term to contain three digits. The 12th term, F12, is the first term to contain three digits.
What is the index of the first term in the Fibonacci sequence to What is the index of the first term in the Fibonacci sequence to
contain 1000 digits? contain 1000 digits? =#
=#
a, b = 1, 1 a, b = 1, 1
index = 2 index = 2
while length(digits(b)) < 1000 while length(digits(b)) < 1000
a, b = big(b), big(b+a) a, b = big(b), big(b + a)
index += 1 index += 1
end end
@@ -46,7 +46,7 @@ function Problem25()
end end
println("Time to evaluate Problem 25:") println("Time to evaluate Problem $(lpad(25, 3, "0")):")
@time Problem25() @btime Problem25()
println("") println("")
println("Result for Problem 25: ", Problem25()) println("Result for Problem $(lpad(25, 3, "0")): ", Problem25())

View File

@@ -5,8 +5,9 @@ Created on 16 Aug 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 26 of Project Euler Solution for Problem 26 of Project Euler
https://projecteuler.net/problem=26 https://projecteuler.net/problem=26 =#
=#
using BenchmarkTools
function Problem26() function Problem26()
#= #=
@@ -27,8 +28,7 @@ function Problem26()
It can be seen that 1/7 has a 6-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle.
Find the value of d < 1000 for which 1/d contains the longest recurring Find the value of d < 1000 for which 1/d contains the longest recurring
cycle in its decimal fraction part. cycle in its decimal fraction part. =#
=#
cycle_length = 0 cycle_length = 0
number_d = 0 number_d = 0
for number in 3:2:999 for number in 3:2:999
@@ -47,7 +47,7 @@ function Problem26()
end end
println("Time to evaluate Problem 26:") println("Time to evaluate Problem $(lpad(26, 3, "0")):")
@time Problem26() @btime Problem26()
println("") println("")
println("Result for Problem 26: ", Problem26()) println("Result for Problem $(lpad(26, 3, "0")): ", Problem26())

View File

@@ -5,9 +5,9 @@ Created on 19 Aug 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 27 of Project Euler Solution for Problem 27 of Project Euler
https://projecteuler.net/problem=27 https://projecteuler.net/problem=27 =#
=#
using BenchmarkTools
using Primes using Primes
function Problem27() function Problem27()
@@ -34,12 +34,11 @@ function Problem27()
Find the product of the coefficients, a and b, for the quadratic expression Find the product of the coefficients, a and b, for the quadratic expression
that produces the maximum number of primes for consecutive values of n, that produces the maximum number of primes for consecutive values of n,
starting with n=0. starting with n=0. =#
=#
LIMIT = 1000 LIMIT = 1000
consecutive_values = 0 consecutive_values = 0
c = 0 c = 0
for a in -999:LIMIT-1 for a in -999:LIMIT - 1
for b in 0:LIMIT for b in 0:LIMIT
n = 0 n = 0
while isprime((n^2) + (a * n) + b) while isprime((n^2) + (a * n) + b)
@@ -55,7 +54,7 @@ function Problem27()
end end
println("Time to evaluate Problem 27:") println("Time to evaluate Problem $(lpad(27, 3, "0")):")
@time Problem27() @btime Problem27()
println("") println("")
println("Result for Problem 27: ", Problem27()) println("Result for Problem $(lpad(27, 3, "0")): ", Problem27())

View File

@@ -5,8 +5,7 @@ Created on 23 Aug 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 28 of Project Euler Solution for Problem 28 of Project Euler
https://projecteuler.net/problem=28 https://projecteuler.net/problem=28 =#
=#
using BenchmarkTools using BenchmarkTools
@@ -24,13 +23,12 @@ function Problem28()
It can be verified that the sum of the numbers on the diagonals is 101. It can be verified that the sum of the numbers on the diagonals is 101.
What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral
formed in the same way? formed in the same way? =#
=#
size = 1001 # Must be odd size = 1001 # Must be odd
ans = 1 # Special case for size 1 ans = 1 # Special case for size 1
step = 0 step = 0
i, cur = 1, 1 i, cur = 1, 1
while step < size-1 while step < size - 1
step = i * 2 step = i * 2
for j in 1:4 for j in 1:4
cur += step cur += step
@@ -43,7 +41,7 @@ function Problem28()
end end
println("Time to evaluate Problem 28:") println("Time to evaluate Problem $(lpad(28, 3, "0")):")
@btime Problem28() @btime Problem28()
println("") println("")
println("Result for Problem 28: ", Problem28()) println("Result for Problem $(lpad(28, 3, "0")): ", Problem28())

View File

@@ -5,8 +5,7 @@ Created on 23 Aug 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 29 of Project Euler Solution for Problem 29 of Project Euler
https://projecteuler.net/problem=29 https://projecteuler.net/problem=29 =#
=#
using BenchmarkTools using BenchmarkTools
@@ -25,14 +24,13 @@ function Problem29()
4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125 4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125
How many distinct terms are in the sequence generated by ab for 2a100 How many distinct terms are in the sequence generated by ab for 2a100
and 2b100? and 2b100? =#
=#
terms = Set(big(a)^b for a in 2:100, b in 2:100) terms = Set(big(a)^b for a in 2:100, b in 2:100)
return length(terms) return length(terms)
end end
println("Time to evaluate Problem 29:") println("Time to evaluate Problem $(lpad(29, 3, "0")):")
@btime Problem29() @btime Problem29()
println("") println("")
println("Result for Problem 29: ", Problem29()) println("Result for Problem $(lpad(29, 3, "0")): ", Problem29())

View File

@@ -41,7 +41,7 @@ function Problem30()
end end
println("Time to evaluate Problem 30:") println("Time to evaluate Problem $(lpad(30, 3, "0")):")
@btime Problem30() @btime Problem30()
println("") println("")
println("Result for Problem 30: ", Problem30()) println("Result for Problem $(lpad(30, 3, "0")): ", Problem30())

View File

@@ -5,8 +5,7 @@ Created on 27 Aug 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 31 of Project Euler Solution for Problem 31 of Project Euler
https://projecteuler.net/problem=31 https://projecteuler.net/problem=31 =#
=#
using BenchmarkTools using BenchmarkTools
using IterTools using IterTools
@@ -22,8 +21,7 @@ function Problem31()
1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p 1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p
How many different ways can £2 be made using any number of coins? How many different ways can £2 be made using any number of coins? =#
=#
no_ways = 0 no_ways = 0
coins = [2, 5, 10, 20, 50, 100] coins = [2, 5, 10, 20, 50, 100]
@@ -39,7 +37,7 @@ function Problem31()
end end
println("Time to evaluate Problem 31:") println("Time to evaluate Problem $(lpad(31, 3, "0")):")
@btime Problem31() @btime Problem31()
println("") println("")
println("Result for Problem 31: ", Problem31()) println("Result for Problem $(lpad(31, 3, "0")): ", Problem31())

View File

@@ -5,8 +5,7 @@ Created on 30 Aug 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 32 of Project Euler Solution for Problem 32 of Project Euler
https://projecteuler.net/problem=32 https://projecteuler.net/problem=32 =#
=#
using BenchmarkTools using BenchmarkTools
@@ -22,8 +21,7 @@ function Problem32()
Find the sum of all products whose multiplicand/multiplier/product identity Find the sum of all products whose multiplicand/multiplier/product identity
can be written as a 1 through 9 pandigital. can be written as a 1 through 9 pandigital.
HINT: Some products can be obtained in more than one way so be sure to only HINT: Some products can be obtained in more than one way so be sure to only
include it once in your sum. include it once in your sum. =#
=#
ans = Set() ans = Set()
pandigital = join(['1', '2', '3', '4', '5', '6', '7', '8', '9']) pandigital = join(['1', '2', '3', '4', '5', '6', '7', '8', '9'])
@@ -41,7 +39,7 @@ function Problem32()
end end
println("Time to evaluate Problem 32:") println("Time to evaluate Problem $(lpad(32, 3, "0")):")
@btime Problem32() @btime Problem32()
println("") println("")
println("Result for Problem 32: ", Problem32()) println("Result for Problem $(lpad(32, 3, "0")): ", Problem32())

View File

@@ -5,8 +5,7 @@ Created on 01 Sep 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 33 of Project Euler Solution for Problem 33 of Project Euler
https://projecteuler.net/problem=33 https://projecteuler.net/problem=33 =#
=#
using BenchmarkTools using BenchmarkTools
@@ -22,8 +21,7 @@ function Problem33()
than one in value, and containing two digits in the numerator and denominator. than one in value, and containing two digits in the numerator and denominator.
If the product of these four fractions is given in its lowest common terms, If the product of these four fractions is given in its lowest common terms,
find the value of the denominator. find the value of the denominator. =#
=#
numerator = 1 numerator = 1
denominator = 1 denominator = 1
for x in 10:99 for x in 10:99
@@ -41,11 +39,11 @@ function Problem33()
end end
end end
return Int(denominator/numerator) return Int(denominator / numerator)
end end
println("Time to evaluate Problem 33:") println("Time to evaluate Problem $(lpad(33, 3, "0")):")
@btime Problem33() @btime Problem33()
println("") println("")
println("Result for Problem 33: ", Problem33()) println("Result for Problem $(lpad(33, 3, "0")): ", Problem33())

View File

@@ -5,8 +5,7 @@ Created on 01 Sep 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 34 of Project Euler Solution for Problem 34 of Project Euler
https://projecteuler.net/problem=34 https://projecteuler.net/problem=34 =#
=#
using BenchmarkTools using BenchmarkTools
@@ -17,8 +16,7 @@ function Problem34()
Find the sum of all numbers which are equal to the sum of the factorial Find the sum of all numbers which are equal to the sum of the factorial
of their digits. of their digits.
Note: As 1! = 1 and 2! = 2 are not sums they are not included. Note: As 1! = 1 and 2! = 2 are not sums they are not included. =#
=#
ans = 0 ans = 0
@@ -36,7 +34,7 @@ function Problem34()
end end
println("Time to evaluate Problem 34:") println("Time to evaluate Problem $(lpad(34, 3, "0")):")
@btime Problem34() @btime Problem34()
println("") println("")
println("Result for Problem 34: ", Problem34()) println("Result for Problem $(lpad(34, 3, "0")): ", Problem34())

View File

@@ -5,8 +5,7 @@ Created on 02 Sep 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 35 of Project Euler Solution for Problem 35 of Project Euler
https://projecteuler.net/problem=35 https://projecteuler.net/problem=35 =#
=#
using BenchmarkTools using BenchmarkTools
using Combinatorics using Combinatorics
@@ -22,15 +21,15 @@ using Primes
# end # end
function circular_number(n) function circular_number(n)
if n <10 if n < 10
return [n] return [n]
end end
digs=digits(n) digs = digits(n)
d=length(digs) d = length(digs)
cyc=zeros(Int,d) cyc = zeros(Int, d)
x=[10^i for i in 0:d-1] x = [10^i for i in 0:d - 1]
for i in 1:d for i in 1:d
cyc[i]=sum(x .* digs[vcat(i:d,1:i-1)]) cyc[i] = sum(x .* digs[vcat(i:d, 1:i - 1)])
end end
return cyc return cyc
end end
@@ -43,8 +42,7 @@ function Problem35()
There are thirteen such primes below 100: There are thirteen such primes below 100:
2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.
How many circular primes are there below one million? How many circular primes are there below one million? =#
=#
circular_primes = [] circular_primes = []
cnt = 0 cnt = 0
for i in 2:1_000_000 for i in 2:1_000_000
@@ -57,16 +55,16 @@ function Problem35()
end end
end end
if all_primes if all_primes
cnt +=1 #push!(circular_primes, i) cnt += 1 # push!(circular_primes, i)
end end
end end
end end
return cnt #length(circular_primes) return cnt # length(circular_primes)
end end
println("Time to evaluate Problem 35:") println("Time to evaluate Problem $(lpad(35, 3, "0")):")
@btime Problem35() @btime Problem35()
println("") println("")
println("Result for Problem 35: ", Problem35()) println("Result for Problem $(lpad(35, 3, "0")): ", Problem35())

View File

@@ -5,8 +5,7 @@ Created on 04 Sep 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 36 of Project Euler Solution for Problem 36 of Project Euler
https://projecteuler.net/problem=36 https://projecteuler.net/problem=36 =#
=#
using BenchmarkTools using BenchmarkTools
@@ -20,11 +19,10 @@ function Problem36()
in both bases. in both bases.
Find the sum of all numbers, less than one million, which are palindromic Find the sum of all numbers, less than one million, which are palindromic
in base 10 and base 2. in base 10 and base 2. =#
=#
ans = 0 ans = 0
for n in 1:2:1_000_000 for n in 1:2:1_000_000
if is_palindrome(digits(n,base=10)) && is_palindrome(digits(n,base=2)) if is_palindrome(digits(n, base=10)) && is_palindrome(digits(n, base=2))
ans += n ans += n
end end
end end
@@ -32,7 +30,7 @@ function Problem36()
end end
println("Time to evaluate Problem 36:") println("Time to evaluate Problem $(lpad(36, 3, "0")):")
@btime Problem36() @btime Problem36()
println("") println("")
println("Result for Problem 36: ", Problem36()) println("Result for Problem $(lpad(36, 3, "0")): ", Problem36())

View File

@@ -5,8 +5,7 @@ Created on 07 Sep 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 37 of Project Euler Solution for Problem 37 of Project Euler
https://projecteuler.net/problem=37 https://projecteuler.net/problem=37 =#
=#
using BenchmarkTools using BenchmarkTools
using Primes using Primes
@@ -14,9 +13,9 @@ using Primes
function is_truncatable_prime(number) function is_truncatable_prime(number)
num_str_rev = reverse(digits(number)) num_str_rev = reverse(digits(number))
for (idx,num) in enumerate(join(num_str_rev)) for (idx, num) in enumerate(join(num_str_rev))
if !isprime(parse(Int, join(num_str_rev[idx:end]))) || if !isprime(parse(Int, join(num_str_rev[idx:end]))) ||
!isprime(parse(Int, join(num_str_rev[1:end-idx+1]))) !isprime(parse(Int, join(num_str_rev[1:end - idx + 1])))
return false return false
end end
end end
@@ -47,7 +46,7 @@ function Problem37()
end end
println("Time to evaluate Problem 37:") println("Time to evaluate Problem $(lpad(37, 3, "0")):")
@btime Problem37() @btime Problem37()
println("") println("")
println("Result for Problem 37: ", Problem37()) println("Result for Problem $(lpad(37, 3, "0")): ", Problem37())

View File

@@ -5,8 +5,7 @@ Created on 08 Sep 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 38 of Project Euler Solution for Problem 38 of Project Euler
https://projecteuler.net/problem=38 https://projecteuler.net/problem=38 =#
=#
using BenchmarkTools using BenchmarkTools
@@ -28,8 +27,7 @@ function Problem38()
What is the largest 1 to 9 pandigital 9-digit number that can What is the largest 1 to 9 pandigital 9-digit number that can
be formed as the concatenated product of an integer with be formed as the concatenated product of an integer with
(1,2, ... , n) where n > 1? (1,2, ... , n) where n > 1? =#
=#
results = [] results = []
pandigital = join(['1', '2', '3', '4', '5', '6', '7', '8', '9']) pandigital = join(['1', '2', '3', '4', '5', '6', '7', '8', '9'])
@@ -41,7 +39,7 @@ function Problem38()
while length(number) < 9 while length(number) < 9
number *= string(integer * i) number *= string(integer * i)
if join(sort(collect(number))) == pandigital if join(sort(collect(number))) == pandigital
push!(results,number) push!(results, number)
end end
integer += 1 integer += 1
end end
@@ -50,7 +48,7 @@ function Problem38()
end end
println("Time to evaluate Problem 38:") println("Time to evaluate Problem $(lpad(38, 3, "0")):")
@btime Problem38() @btime Problem38()
println("") println("")
println("Result for Problem 38: ", Problem38()) println("Result for Problem $(lpad(38, 3, "0")): ", Problem38())

View File

@@ -5,8 +5,7 @@ Created on 09 Sep 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 39 of Project Euler Solution for Problem 39 of Project Euler
https://projecteuler.net/problem=39 https://projecteuler.net/problem=39 =#
=#
using BenchmarkTools using BenchmarkTools
@@ -17,13 +16,12 @@ function Problem39()
{20,48,52}, {24,45,51}, {30,40,50} {20,48,52}, {24,45,51}, {30,40,50}
For which value of p 1000, is the number of solutions maximised? For which value of p 1000, is the number of solutions maximised? =#
=#
ans, val = 0, 0 ans, val = 0, 0
for p in 2:2:1000 for p in 2:2:1000
sol = 0 sol = 0
for a in 1:p for a in 1:p
for b in a+1:p-2*a for b in a + 1:p - 2 * a
c = p - (a + b) c = p - (a + b)
if a^2 + b^2 == c^2 if a^2 + b^2 == c^2
sol += 1 sol += 1
@@ -43,7 +41,7 @@ function Problem39()
end end
println("Time to evaluate Problem 39:") println("Time to evaluate Problem $(lpad(39, 3, "0")):")
@btime Problem39() @btime Problem39()
println("") println("")
println("Result for Problem 39: ", Problem39()) println("Result for Problem $(lpad(39, 3, "0")): ", Problem39())

View File

@@ -5,8 +5,7 @@ Created on 10 Sep 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 40 of Project Euler Solution for Problem 40 of Project Euler
https://projecteuler.net/problem=40 https://projecteuler.net/problem=40 =#
=#
using BenchmarkTools using BenchmarkTools
@@ -20,9 +19,7 @@ function Problem40()
If d_n represents the n^th digit of the fractional part, find the value of the following expression. If d_n represents the n^th digit of the fractional part, find the value of the following expression.
d_1 × d_{10} × d_{100} × d_{1_000} × d_{10_000} × d_{100_000} × d_{1_000_000} d_1 × d_{10} × d_{100} × d_{1_000} × d_{10_000} × d_{100_000} × d_{1_000_000} =#
=#
ans = 1 ans = 1
fraction = join([i for i in 1:1_000_000]) fraction = join([i for i in 1:1_000_000])
@@ -34,7 +31,7 @@ function Problem40()
end end
println("Time to evaluate Problem 40:") println("Time to evaluate Problem $(lpad(40, 3, "0")):")
@btime Problem40() @btime Problem40()
println("") println("")
println("Result for Problem 40: ", Problem40()) println("Result for Problem $(lpad(40, 3, "0")): ", Problem40())

View File

@@ -5,8 +5,7 @@ Created on 11 Sep 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 41 of Project Euler Solution for Problem 41 of Project Euler
https://projecteuler.net/problem=41 https://projecteuler.net/problem=41 =#
=#
using BenchmarkTools using BenchmarkTools
using Primes using Primes
@@ -26,8 +25,7 @@ function Problem41()
use of all the digits 1 to n exactly once. For example, 2143 is use of all the digits 1 to n exactly once. For example, 2143 is
a 4-digit pandigital and is also prime. a 4-digit pandigital and is also prime.
What is the largest n-digit pandigital prime that exists? What is the largest n-digit pandigital prime that exists? =#
=#
for ans in 7654321:-1:1 for ans in 7654321:-1:1
if is_pandigital(ans) & isprime(ans) if is_pandigital(ans) & isprime(ans)
@@ -37,7 +35,7 @@ function Problem41()
end end
println("Time to evaluate Problem 41:") println("Time to evaluate Problem $(lpad(41, 3, "0")):")
@btime Problem41() @btime Problem41()
println("") println("")
println("Result for Problem 41: ", Problem41()) println("Result for Problem $(lpad(41, 3, "0")): ", Problem41())

View File

@@ -5,18 +5,17 @@ Created on 12 Sep 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 42 of Project Euler Solution for Problem 42 of Project Euler
https://projecteuler.net/problem=42 https://projecteuler.net/problem=42 =#
=#
using BenchmarkTools using BenchmarkTools
using DelimitedFiles using DelimitedFiles
function triangle_number(num) function triangle_number(num)
return Int(0.5*num*(num+1)) return Int(0.5 * num * (num + 1))
end end
function word_to_value(word) function word_to_value(word)
return sum(Int(letter)-64 for letter in word) return sum(Int(letter) - 64 for letter in word)
end end
function Problem42() function Problem42()
@@ -32,12 +31,11 @@ function Problem42()
shall call the word a triangle word. shall call the word a triangle word.
Using words.txt, a 16K text file containing nearly two-thousand common English words, Using words.txt, a 16K text file containing nearly two-thousand common English words,
how many are triangle words? how many are triangle words? =#
=#
triangular_numbers = [triangle_number(n) for n in 1:26] triangular_numbers = [triangle_number(n) for n in 1:26]
ans = 0 ans = 0
file = "/datos/Scripts/Gitea/Project_Euler/src/files/Problem42.txt" file = "../files/Problem42.txt"
words = sort(readdlm(file, ',', String)[:]) words = sort(readdlm(file, ',', String)[:])
for word in words for word in words
@@ -49,7 +47,7 @@ function Problem42()
end end
println("Time to evaluate Problem 42:") println("Time to evaluate Problem $(lpad(42, 3, "0")):")
@btime Problem42() @btime Problem42()
println("") println("")
println("Result for Problem 42: ", Problem42()) println("Result for Problem $(lpad(42, 3, "0")): ", Problem42())

View File

@@ -5,8 +5,7 @@ Created on 13 Sep 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 43 of Project Euler Solution for Problem 43 of Project Euler
https://projecteuler.net/problem=43 https://projecteuler.net/problem=43 =#
=#
using BenchmarkTools using BenchmarkTools
using Combinatorics using Combinatorics
@@ -28,13 +27,12 @@ function Problem43()
d7d8d9=728 is divisible by 13 d7d8d9=728 is divisible by 13
d8d9d10=289 is divisible by 17 d8d9d10=289 is divisible by 17
Find the sum of all 0 to 9 pandigital numbers with this property. Find the sum of all 0 to 9 pandigital numbers with this property. =#
=#
ans = [] ans = []
pandigital = join(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']) pandigital = join(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])
for n in permutations(pandigital) for n in permutations(pandigital)
n_ =join(n) n_ = join(n)
if n_[1] != 0 && join(sort(n)) == pandigital if n_[1] != 0 && join(sort(n)) == pandigital
if parse(Int, n_[8:end]) % 17 == 0 if parse(Int, n_[8:end]) % 17 == 0
if parse(Int, n_[7:9]) % 13 == 0 if parse(Int, n_[7:9]) % 13 == 0
@@ -58,7 +56,7 @@ function Problem43()
end end
println("Time to evaluate Problem 43:") println("Time to evaluate Problem $(lpad(43, 3, "0")):")
@btime Problem43() @btime Problem43()
println("") println("")
println("Result for Problem 43: ", Problem43()) println("Result for Problem $(lpad(43, 3, "0")): ", Problem43())

View File

@@ -5,14 +5,13 @@ Created on 14 Sep 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 44 of Project Euler Solution for Problem 44 of Project Euler
https://projecteuler.net/problem=44 https://projecteuler.net/problem=44 =#
=#
using BenchmarkTools using BenchmarkTools
using Combinatorics using Combinatorics
function pentagonal(n) function pentagonal(n)
return Int(n*(3*n-1)/2) return Int(n * (3 * n - 1) / 2)
end end
function Problem44() function Problem44()
@@ -28,14 +27,13 @@ function Problem44()
Find the pair of pentagonal numbers, Pj and Pk, for which their Find the pair of pentagonal numbers, Pj and Pk, for which their
sum and difference are pentagonal and D = |Pk Pj| is minimised. sum and difference are pentagonal and D = |Pk Pj| is minimised.
What is the value of D? What is the value of D? =#
=#
dif = 0 dif = 0
pentagonal_list = [pentagonal(n) for n in 1:2500] pentagonal_list = [pentagonal(n) for n in 1:2500]
pairs = combinations(pentagonal_list, 2) pairs = combinations(pentagonal_list, 2)
for p in pairs for p in pairs
if reduce(+, p) in pentagonal_list && abs(reduce(-, p)) in pentagonal_list if reduce(+, p) in pentagonal_list && abs(reduce(-, p)) in pentagonal_list
dif = (abs(reduce(-,p))) dif = (abs(reduce(-, p)))
# the first one found would be the smallest # the first one found would be the smallest
break break
end end
@@ -45,7 +43,7 @@ function Problem44()
end end
println("Time to evaluate Problem 44:") println("Time to evaluate Problem $(lpad(44, 3, "0")):")
@btime Problem44() @btime Problem44()
println("") println("")
println("Result for Problem 44: ", Problem44()) println("Result for Problem $(lpad(44, 3, "0")): ", Problem44())

View File

@@ -5,17 +5,16 @@ Created on 15 Sep 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 45 of Project Euler Solution for Problem 45 of Project Euler
https://projecteuler.net/problem=45 https://projecteuler.net/problem=45 =#
=#
using BenchmarkTools using BenchmarkTools
function pentagonal(n) function pentagonal(n)
return Int(n*(3*n-1)/2) return Int(n * (3 * n - 1) / 2)
end end
function hexagonal(n) function hexagonal(n)
return Int(n*(2*n-1)) return Int(n * (2 * n - 1))
end end
function Problem45() function Problem45()
@@ -27,8 +26,7 @@ function Problem45()
It can be verified that T285 = P165 = H143 = 40755. It can be verified that T285 = P165 = H143 = 40755.
Find the next triangle number that is also pentagonal and hexagonal. Find the next triangle number that is also pentagonal and hexagonal. =#
=#
pentagonal_list = Set(pentagonal(n) for n in 2:100_000) pentagonal_list = Set(pentagonal(n) for n in 2:100_000)
# all hexagonal numbers are also triangle numbers! # all hexagonal numbers are also triangle numbers!
hexagonal_list = Set(hexagonal(n) for n in 2:100_000) hexagonal_list = Set(hexagonal(n) for n in 2:100_000)
@@ -39,7 +37,7 @@ function Problem45()
end end
println("Time to evaluate Problem 45:") println("Time to evaluate Problem $(lpad(45, 3, "0")):")
@btime Problem45() @btime Problem45()
println("") println("")
println("Result for Problem 45: ", Problem45()) println("Result for Problem $(lpad(45, 3, "0")): ", Problem45())

View File

@@ -5,15 +5,14 @@ Created on 16 Sep 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 46 of Project Euler Solution for Problem 46 of Project Euler
https://projecteuler.net/problem=46 https://projecteuler.net/problem=46 =#
=#
using BenchmarkTools using BenchmarkTools
using Primes using Primes
function is_goldbach(number) function is_goldbach(number)
for i in number - 1:-1:1 for i in number - 1:-1:1
if isprime(i) & ((((number - i) / 2)^0.5)%1==0) if isprime(i) & ((((number - i) / 2)^0.5) % 1 == 0)
return true return true
end end
end end
@@ -35,8 +34,7 @@ function Problem46()
It turns out that the conjecture was false. It turns out that the conjecture was false.
What is the smallest odd composite that cannot be written as the sum What is the smallest odd composite that cannot be written as the sum
of a prime and twice a square? of a prime and twice a square? =#
=#
ans = 9 ans = 9
while true while true
ans += 2 ans += 2
@@ -47,7 +45,7 @@ function Problem46()
end end
println("Time to evaluate Problem 46:") println("Time to evaluate Problem $(lpad(46, 3, "0")):")
@btime Problem46() @btime Problem46()
println("") println("")
println("Result for Problem 46: ", Problem46()) println("Result for Problem $(lpad(46, 3, "0")): ", Problem46())

View File

@@ -5,24 +5,23 @@ Created on 18 Sep 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 47 of Project Euler Solution for Problem 47 of Project Euler
https://projecteuler.net/problem=47 https://projecteuler.net/problem=47 =#
=#
using BenchmarkTools using BenchmarkTools
function factor(n) function factor(n)
ans = [] ans = []
d = 2 d = 2
while d*d <= n while d * d <= n
if n % d == 0 if n % d == 0
push!(ans,d) push!(ans, d)
n = n ÷ d n = n ÷ d
else else
d += 1 d += 1
end end
end end
if n > 1 if n > 1
push!(ans,n) push!(ans, n)
end end
return ans return ans
end end
@@ -41,15 +40,14 @@ function Problem47()
646 = 2 × 17 × 19. 646 = 2 × 17 × 19.
Find the first four consecutive integers to have four distinct prime factors each. Find the first four consecutive integers to have four distinct prime factors each.
What is the first of these numbers? What is the first of these numbers? =#
=#
ans = [] ans = []
for number in 1:1_000_000 for number in 1:1_000_000
if length(ans) == 4 if length(ans) == 4
break break
elseif length(Set(factor(number))) == 4 elseif length(Set(factor(number))) == 4
push!(ans,number) push!(ans, number)
else else
ans = [] ans = []
end end
@@ -59,7 +57,7 @@ function Problem47()
end end
println("Time to evaluate Problem 47:") println("Time to evaluate Problem $(lpad(47, 3, "0")):")
@btime Problem47() @btime Problem47()
println("") println("")
println("Result for Problem 47: ", Problem47()) println("Result for Problem $(lpad(47, 3, "0")): ", Problem47())

View File

@@ -5,8 +5,7 @@ Created on 18 Sep 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 48 of Project Euler Solution for Problem 48 of Project Euler
https://projecteuler.net/problem=48 https://projecteuler.net/problem=48 =#
=#
using BenchmarkTools using BenchmarkTools
@@ -14,14 +13,13 @@ function Problem48()
#= #=
The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317. The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317.
Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000. Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000. =#
=#
series = sum(big(i)^i for i in 1:1000) series = sum(big(i)^i for i in 1:1000)
return string(series)[end-9:end] return string(series)[end - 9:end]
end end
println("Time to evaluate Problem 48:") println("Time to evaluate Problem $(lpad(48, 3, "0")):")
@btime Problem48() @btime Problem48()
println("") println("")
println("Result for Problem 48: ", Problem48()) println("Result for Problem $(lpad(48, 3, "0")): ", Problem48())

View File

@@ -5,8 +5,7 @@ Created on 19 Sep 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 49 of Project Euler Solution for Problem 49 of Project Euler
https://projecteuler.net/problem=49 https://projecteuler.net/problem=49 =#
=#
using BenchmarkTools using BenchmarkTools
using Primes using Primes
@@ -21,15 +20,14 @@ function Problem49()
There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes,
exhibiting this property, but there is one other 4-digit increasing sequence. exhibiting this property, but there is one other 4-digit increasing sequence.
What 12-digit number do you form by concatenating the three terms in this sequence? What 12-digit number do you form by concatenating the three terms in this sequence? =#
=#
ans = [] ans = []
primes_list = primes(1_000, 10_000) primes_list = primes(1_000, 10_000)
for number in primes_list for number in primes_list
if sort(collect(digits(number))) == sort(collect(digits(number+3330))) == sort(collect(digits(number+6660))) if sort(collect(digits(number))) == sort(collect(digits(number + 3330))) == sort(collect(digits(number + 6660)))
if number+3330 in primes_list && number+6660 in primes_list if number + 3330 in primes_list && number + 6660 in primes_list
push!(ans, (string(number)*string(number+3300)*string(number+6660))) push!(ans, (string(number) * string(number + 3300) * string(number + 6660)))
end end
end end
end end
@@ -38,7 +36,7 @@ function Problem49()
end end
println("Time to evaluate Problem 49:") println("Time to evaluate Problem $(lpad(49, 3, "0")):")
@btime Problem49() @btime Problem49()
println("") println("")
println("Result for Problem 49: ", Problem49()) println("Result for Problem $(lpad(49, 3, "0")): ", Problem49())

View File

@@ -5,8 +5,7 @@ Created on 20 Sep 2021
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for Problem 50 of Project Euler Solution for Problem 50 of Project Euler
https://projecteuler.net/problem=50 https://projecteuler.net/problem=50 =#
=#
using BenchmarkTools using BenchmarkTools
using Primes using Primes
@@ -22,8 +21,7 @@ function Problem50()
The longest sum of consecutive primes below one-thousand that adds to a prime, The longest sum of consecutive primes below one-thousand that adds to a prime,
contains 21 terms, and is equal to 953. contains 21 terms, and is equal to 953.
Which prime, below one-million, can be written as the sum of the most consecutive primes? Which prime, below one-million, can be written as the sum of the most consecutive primes? =#
=#
ans = 0 ans = 0
result = 0 result = 0
@@ -48,7 +46,7 @@ function Problem50()
end end
println("Time to evaluate Problem 50:") println("Time to evaluate Problem $(lpad(50, 3, "0")):")
@btime Problem50() @btime Problem50()
println("") println("")
println("Result for Problem 50: ", Problem50()) println("Result for Problem $(lpad(50, 3, "0")): ", Problem50())