diff --git a/src/Julia/Problems001-050/Problem001.jl b/src/Julia/Problems001-050/Problem001.jl new file mode 100644 index 0000000..19de345 --- /dev/null +++ b/src/Julia/Problems001-050/Problem001.jl @@ -0,0 +1,27 @@ +#= +Created on 08 Jun 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 1 of Project Euler +https://projecteuler.net/problem=1 +=# + +function Problem1() + #= + 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. + + Find the sum of all the multiples of 3 or 5 below 1000. + =# + ans = sum(x for x in 0:999 if x%3==0 || x%5==0) + + return ans +end + + +println("Time to evaluate Problem 1:") +@time Problem1() +println("") +println("Result for Problem 1: ", Problem1()) \ No newline at end of file diff --git a/src/Julia/Problems001-050/Problem002.jl b/src/Julia/Problems001-050/Problem002.jl new file mode 100644 index 0000000..34bb301 --- /dev/null +++ b/src/Julia/Problems001-050/Problem002.jl @@ -0,0 +1,40 @@ +#= +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 +=# + +function Problem2() + #= + 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: + + 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. + =# + + ans = 0 + limit = 4_000_000 + x, y = 1, 1 + z = x + y # Because every third Fibonacci number is even + while z <= limit + ans += z + x = y + z + y = z + x + z = x + y + end + + return ans +end + + +println("Time to evaluate Problem 2:") +@time Problem2() +println("") +println("Result for Problem 2: ", Problem2()) \ No newline at end of file diff --git a/src/Julia/Problems001-050/Problem003.jl b/src/Julia/Problems001-050/Problem003.jl new file mode 100644 index 0000000..af1da0a --- /dev/null +++ b/src/Julia/Problems001-050/Problem003.jl @@ -0,0 +1,34 @@ +#= +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 +=# + +function Problem3() + #= + The prime factors of 13195 are 5, 7, 13 and 29. + + What is the largest prime factor of the number 600851475143 ? + =# + + ans = 600_851_475_143 + factor = 2 + while factor * factor < ans + while ans % factor == 0 + ans = ans ÷ factor + end + factor += 1 + end + + return ans +end + + +println("Time to evaluate Problem 3:") +@time Problem3() +println("") +println("Result for Problem 3: ", Problem3()) diff --git a/src/Julia/Problems001-050/Problem004.jl b/src/Julia/Problems001-050/Problem004.jl new file mode 100644 index 0000000..aeba860 --- /dev/null +++ b/src/Julia/Problems001-050/Problem004.jl @@ -0,0 +1,36 @@ +#= +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 +=# + +function Problem4() + #= + 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. + =# + ans = 0 + for i in 100:1000 + for j in 100:1000 + palindrome = i * j + s = string(palindrome) + if (s==reverse(s)) & (palindrome > ans) + ans = palindrome + end + end + end + + return ans +end + + +println("Time to evaluate Problem 4:") +@time Problem4() +println("") +println("Result for Problem 4: ", Problem4()) diff --git a/src/Julia/Problems001-050/Problem005.jl b/src/Julia/Problems001-050/Problem005.jl new file mode 100644 index 0000000..b61d5fb --- /dev/null +++ b/src/Julia/Problems001-050/Problem005.jl @@ -0,0 +1,41 @@ +#= +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 +=# + +#= +The LCM of two natural numbers x and y is given by: +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)) +=# + + +function Problem5() + #= + 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? + =# + ans = 1 + for i in 1:21 + ans *= i ÷ gcd(i, ans) + end + + return ans +end + + +println("Time to evaluate Problem 5:") +@time Problem5() +println("") +println("Result for Problem 5: ", Problem5()) diff --git a/src/Julia/Problems001-050/Problem006.jl b/src/Julia/Problems001-050/Problem006.jl new file mode 100644 index 0000000..e923edd --- /dev/null +++ b/src/Julia/Problems001-050/Problem006.jl @@ -0,0 +1,36 @@ +#= +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 +=# + +function Problem6() + #= + The sum of the squares of the first ten natural numbers is, + 1^2 + 2^2 + ... + 10^2 = 385 + + The square of the sum of the first ten natural numbers is, + (1 + 2 + ... + 10)^2 = 55^2 = 3025 + + Hence the difference between the sum of the squares of the first ten + 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 + =# + n = 100 + square_of_sum = sum(i for i in (1:n))^2 + sum_squares = sum(i^2 for i in 1:n) + diff = square_of_sum - sum_squares + return diff +end + + +println("Time to evaluate Problem 6:") +@time Problem6() +println("") +println("Result for Problem 6: ", Problem6()) diff --git a/src/Julia/Problems001-050/Problem007.jl b/src/Julia/Problems001-050/Problem007.jl new file mode 100644 index 0000000..30f9539 --- /dev/null +++ b/src/Julia/Problems001-050/Problem007.jl @@ -0,0 +1,36 @@ +#= +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 +=# + +using Primes + +function Problem7() + #= + 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 + =# + number = 2 + primeList = Int[] + while length(primeList) < 10_001 + if isprime(number) + append!(primeList,number) + end + number += 1 + end + + return primeList[length(primeList)] +end + + +println("Time to evaluate Problem 7:") +@time Problem7() +println("") +println("Result for Problem 7: ", Problem7()) diff --git a/src/Julia/Problems001-050/Problem008.jl b/src/Julia/Problems001-050/Problem008.jl new file mode 100644 index 0000000..a2c5397 --- /dev/null +++ b/src/Julia/Problems001-050/Problem008.jl @@ -0,0 +1,68 @@ +#= +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 +=# + +function Problem8() + #= + The four adjacent digits in the 1000-digit number that have the + greatest product are 9 × 9 × 8 × 9 = 5832. + + 731671...963450 + + Find the thirteen adjacent digits in the 1000-digit number that have + the greatest product. What is the value of this product? + =# + + NUM = """ + 73167176531330624919225119674426574742355349194934 + 96983520312774506326239578318016984801869478851843 + 85861560789112949495459501737958331952853208805511 + 12540698747158523863050715693290963295227443043557 + 66896648950445244523161731856403098711121722383113 + 62229893423380308135336276614282806444486645238749 + 30358907296290491560440772390713810515859307960866 + 70172427121883998797908792274921901699720888093776 + 65727333001053367881220235421809751254540594752243 + 52584907711670556013604839586446706324415722155397 + 53697817977846174064955149290862569321978468622482 + 83972241375657056057490261407972968652414535100474 + 82166370484403199890008895243450658541227588666881 + 16427171479924442928230863465674813919123162824586 + 17866458359124566529476545682848912883142607690042 + 24219022671055626321111109370544217506941658960408 + 07198403850962455444362981230987879927244284909188 + 84580156166097919133875499200524063689912560717606 + 05886116467109405077541002256983155200055935729725 + 71636269561882670428252483600823257530420752963450 + """ + + num = replace(NUM, "\n" => "") + adjacent_digits = 13 + target = length(num) - adjacent_digits + ans, i = 0, 1 + while i < target + a, j = 1, i + while j < (i + adjacent_digits) + a = a * parse(Int, (num[j])) + j += 1 + end + if a > ans + ans = a + end + i += 1 + end + + return ans +end + + +println("Time to evaluate Problem 8:") +@time Problem8() +println("") +println("Result for Problem 8: ", Problem8()) diff --git a/src/Julia/Problems001-050/Problem009.jl b/src/Julia/Problems001-050/Problem009.jl new file mode 100644 index 0000000..fc1bc62 --- /dev/null +++ b/src/Julia/Problems001-050/Problem009.jl @@ -0,0 +1,38 @@ +#= +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 +=# + +function Problem9() + #= + A Pythagorean triplet is a set of three natural numbers, a < b < c, + for which a^2 + b^2 = c^2 + + 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. + =# + + upper_limit = 1000 + for a in 1:upper_limit + 1 + for b in a + 1:upper_limit + 1 + c = upper_limit - a - b + if a * a + b * b == c * c + # It is now implied that b < c, because we have a > 0 + return a * b * c + end + end + end +end + + +println("Time to evaluate Problem 9:") +@time Problem9() +println("") +println("Result for Problem 9: ", Problem9()) diff --git a/src/Julia/Problems001-050/Problem010.jl b/src/Julia/Problems001-050/Problem010.jl new file mode 100644 index 0000000..988743d --- /dev/null +++ b/src/Julia/Problems001-050/Problem010.jl @@ -0,0 +1,27 @@ +#= +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 +=# + +using Primes + +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. + =# + + return sum(primes(1_999_999)) +end + + +println("Time to evaluate Problem 10:") +@time Problem10() +println("") +println("Result for Problem 10: ", Problem10()) diff --git a/src/Julia/Problems001-050/Problem012.jl b/src/Julia/Problems001-050/Problem012.jl new file mode 100644 index 0000000..17054df --- /dev/null +++ b/src/Julia/Problems001-050/Problem012.jl @@ -0,0 +1,57 @@ +#= +Created on 21 Jul 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 12 of Project Euler +https://projecteuler.net/problem=12 +=# + + + +function Problem12() + #= + The sequence of triangle numbers is generated by adding the natural + numbers. So the 7th triangle number would be: + 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. + + The first ten terms would be: + 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... + + Let us list the factors of the first seven triangle numbers: + + 1: 1 + 3: 1,3 + 6: 1,2,3,6 + 10: 1,2,5,10 + 15: 1,3,5,15 + 21: 1,3,7,21 + 28: 1,2,4,7,14,28 + + 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 + divisors? + =# + + function num_divisors(n) + r = isqrt(n) + 2 * count(n % i == 0 for i in 1:r) - (r^2 == n) + end + + + triangle = 0 + for i in Iterators.countfrom(1) + triangle += i + if num_divisors(triangle) > 500 + return string(triangle) + end + end +end + + +println("Time to evaluate Problem 12:") +@time Problem12() +println("") +println("Result for Problem 12: ", Problem12()) \ No newline at end of file diff --git a/src/Julia/Problems001-050/Problem013.jl b/src/Julia/Problems001-050/Problem013.jl new file mode 100644 index 0000000..b8a28e8 --- /dev/null +++ b/src/Julia/Problems001-050/Problem013.jl @@ -0,0 +1,27 @@ +using Base: String +#= +Created on 22 Jul 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 13 of Project Euler +https://projecteuler.net/problem=13 +=# + +using DoubleFloats +# using JSON + +function Problem13() + #= + Work out the first ten digits of the sum of the following one-hundred + 50-digit numbers + =# + return string(sum(parse.(BigInt,readlines("../files/Problem13.txt"))))[1:10] +end + + +println("Time to evaluate Problem 13:") +@time Problem13() +println("") +println("Result for Problem 13: ", Problem13()) diff --git a/src/Julia/Problems001-050/Problem014.jl b/src/Julia/Problems001-050/Problem014.jl new file mode 100644 index 0000000..cc9f6a5 --- /dev/null +++ b/src/Julia/Problems001-050/Problem014.jl @@ -0,0 +1,59 @@ +#= +Created on 24 Jul 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 14 of Project Euler +https://projecteuler.net/problem=14 +=# + +function chain_length(n)#, terms) + length = 0 + while n > 1 + n = iseven(n) ? n >> 1 : 3n + 1 + length += 1 + end + return length +end + +function Problem14() + #= + The following iterative sequence is defined for the set of positive + integers: + + n → n/2 (n is even) + n → 3n + 1 (n is odd) + + Using the rule above and starting with 13, we generate the following + sequence: + + 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 + + It can be seen that this sequence (starting at 13 and finishing at 1) + contains 10 terms. Although it has not been proved yet (Collatz Problem), + it is thought that all starting numbers finish at 1. + + Which starting number, under one million, produces the longest chain? + + NOTE: Once the chain starts the terms are allowed to go above one million. + =# + + ans = 0 + limit = 1_000_000 + score = 0 + for i in 1:2:limit # no need to check even numbers + longest = chain_length(i) + if longest > score + score = longest + ans = i + end + end + return ans +end + + +println("Time to evaluate Problem 14:") +@time Problem14() +println("") +println("Result for Problem 14: ", Problem14()) diff --git a/src/Julia/Problems001-050/Problem015.jl b/src/Julia/Problems001-050/Problem015.jl new file mode 100644 index 0000000..d556a9d --- /dev/null +++ b/src/Julia/Problems001-050/Problem015.jl @@ -0,0 +1,28 @@ +using Base: Integer +#= +Created on 25 Jul 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 15 of Project Euler +https://projecteuler.net/problem=15 +=# + +function Problem15() + #= + 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 = 20 + return Integer(factorial(big(2n)) / (factorial(big(n)) * factorial(big(2n - n)))) +end + + +println("Time to evaluate Problem 15:") +@time Problem15() +println("") +println("Result for Problem 15: ", Problem15()) diff --git a/src/Julia/Problems001-050/Problem016.jl b/src/Julia/Problems001-050/Problem016.jl new file mode 100644 index 0000000..670ae1f --- /dev/null +++ b/src/Julia/Problems001-050/Problem016.jl @@ -0,0 +1,26 @@ +using Base: Integer +#= +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 +=# + +function Problem16() + #= + 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? + =# + n = 1000 + return sum(parse(Int, d) for d in string(2^BigInt(n))) +end + + +println("Time to evaluate Problem 16:") +@time Problem16() +println("") +println("Result for Problem 16: ", Problem16()) diff --git a/src/Julia/Problems001-050/Problem017.jl b/src/Julia/Problems001-050/Problem017.jl new file mode 100644 index 0000000..c07a708 --- /dev/null +++ b/src/Julia/Problems001-050/Problem017.jl @@ -0,0 +1,66 @@ +#= +Created on 28 Jul 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 17 of Project Euler +https://projecteuler.net/problem=17 +=# + +function num2letters(num, dic) + if num <= 20 + return length(dic[num]) + elseif num < 100 + tens, units = divrem(num, 10) + return length(dic[tens * 10]) + num2letters(units, dic) + elseif num < 1000 + hundreds, rest = divrem(num, 100) + if rest != 0 + return num2letters(hundreds, dic) + length(dic[100]) + + length("and") + num2letters(rest, dic) + else + return num2letters(hundreds, dic) + length(dic[100]) + end + else + thousands, rest = divrem(num, 1000) + return num2letters(thousands, dic) + length(dic[1000]) + end +end + +function Problem17() + #= + 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. + + If all the numbers from 1 to 1000 (one thousand) inclusive were written + out in words, how many letters would be used? + + 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. + =# + nums = Dict( + 0 => "", 1 => "one", 2 => "two", 3 => "three", 4 => "four", 5 => "five", + 6 => "six", 7 => "seven", 8 => "eight", 9 => "nine", 10 => "ten", + 11 => "eleven", 12 => "twelve", 13 => "thirteen", 14 => "fourteen", + 15 => "fifteen", 16 => "sixteen", 17 => "seventeen", 18 => "eighteen", + 19 => "nineteen", 20 => "twenty", 30 => "thirty", 40 => "forty", + 50 => "fifty", 60 => "sixty", 70 => "seventy", 80 => "eighty", + 90 => "ninety", 100 => "hundred", 1000 => "thousand" + ) + + n = 1000 + letters = 0 + for num in 1:n + letters += num2letters(num, nums) + end + return letters +end + + +println("Time to evaluate Problem 17:") +@time Problem17() +println("") +println("Result for Problem 17: ", Problem17()) diff --git a/src/Julia/Problems001-050/Problem018.jl b/src/Julia/Problems001-050/Problem018.jl new file mode 100644 index 0000000..b70b447 --- /dev/null +++ b/src/Julia/Problems001-050/Problem018.jl @@ -0,0 +1,55 @@ +#= +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 =# + +function Problem18() + #= + 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. + + 3 + 7 4 + 2 4 6 + 8 5 9 3 + + That is, 3 + 7 + 4 + 9 = 23. + + Find the maximum total from top to bottom of the triangle above =# + triangle = [ # Mutable + [75], + [95,64], + [17,47,82], + [18,35,87,10], + [20, 4,82,47,65], + [19, 1,23,75, 3,34], + [88, 2,77,73, 7,63,67], + [99,65, 4,28, 6,16,70,92], + [41,41,26,56,83,40,80,70,33], + [41,48,72,33,47,32,37,16,94,29], + [53,71,44,65,25,43,91,52,97,51,14], + [70,11,33,28,77,73,17,78,39,68,17,57], + [91,71,52,38,17,14,91,43,58,50,27,29,48], + [63,66, 4,68,89,53,67,30,73,16,69,87,40,31], + [4,62,98,27,23, 9,70,98,73,93,38,53,60, 4,23], + ] + len_triangle = length(triangle) + + for i in len_triangle - 1:-1:1 + for j in 1:length(triangle[i]) + triangle[i][j] += max(triangle[i + 1][j], triangle[i + 1][j + 1]) + end + end + + return triangle[1][1] +end + + +println("Time to evaluate Problem 18:") +@time Problem18() +println("") +println("Result for Problem 18: ", Problem18()) diff --git a/src/Julia/Problems001-050/Problem019.jl b/src/Julia/Problems001-050/Problem019.jl new file mode 100644 index 0000000..16771d1 --- /dev/null +++ b/src/Julia/Problems001-050/Problem019.jl @@ -0,0 +1,43 @@ +#= +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 =# + +using Dates + +function Problem19() + #= + You are given the following information, but you may prefer to do some + research for yourself. + + 1 Jan 1900 was a Monday. + Thirty days has September, April, June and November. + All the rest have thirty-one, + Saving February alone, + Which has twenty-eight, rain or shine. + And on leap years, twenty-nine. + A leap year occurs on any year evenly divisible by 4, but not on a century + 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)? =# + sundays = 0 + for y in 1901:2000 + for m in 1:12 + if Dates.dayofweek(Date(y, m, 1)) == Dates.Sunday + sundays += 1 + end + end + end + return sundays +end + + +println("Time to evaluate Problem 19:") +@time Problem19() +println("") +println("Result for Problem 19: ", Problem19()) \ No newline at end of file diff --git a/src/Julia/Problems001-050/Problem020.jl b/src/Julia/Problems001-050/Problem020.jl new file mode 100644 index 0000000..9dbd59e --- /dev/null +++ b/src/Julia/Problems001-050/Problem020.jl @@ -0,0 +1,27 @@ +#= +Created on 03 Aug 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 20 of Project Euler +https://projecteuler.net/problem=20 =# + +function Problem20() + #= + n! means n × (n − 1) × ... × 3 × 2 × 1 + + For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, + and the sum of the digits in the number 10! is: + 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. + + Find the sum of the digits in the number 100! =# + fact = factorial(big(100)) + return sum(parse(Int, d) for d in string(fact)) +end + + +println("Time to evaluate Problem 20:") +@time Problem20() +println("") +println("Result for Problem 20: ", Problem20()) \ No newline at end of file diff --git a/src/Julia/Problems001-050/Problem021.jl b/src/Julia/Problems001-050/Problem021.jl new file mode 100644 index 0000000..ca7f164 --- /dev/null +++ b/src/Julia/Problems001-050/Problem021.jl @@ -0,0 +1,55 @@ +#= +Created on 05 Aug 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 21 of Project Euler +https://projecteuler.net/problem=21 =# + + +function divisors(n) + divisors = Int64[1] + m = round(Int, n / 2) + for i in 2:m + if n % i == 0 + push!(divisors, i) + end + end + return divisors +end + +function Problem21() + #= + Let d(n) be defined as the sum of proper divisors of n (numbers + less than n which divide evenly into n). + If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable + pair and each of a and b are called amicable numbers. + + For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, + 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are + 1, 2, 4, 71 and 142; so d(284) = 220. + + Evaluate the sum of all the amicable numbers under 10000 =# + + n = 9999 + s = zeros(Int, n) + amicable = Int64[] + for i in 2:n + s[i] = sum(divisors(i)) + end + + for i in 2:n + if s[i] <= n && i != s[i] && i == s[s[i]] + push!(amicable, i) + end + end + + return sum(amicable) +end + + +println("Time to evaluate Problem 21:") +@time Problem21() +println("") +println("Result for Problem 21: ", Problem21()) \ No newline at end of file diff --git a/src/Julia/Problems001-050/Problem022.jl b/src/Julia/Problems001-050/Problem022.jl new file mode 100644 index 0000000..11e714d --- /dev/null +++ b/src/Julia/Problems001-050/Problem022.jl @@ -0,0 +1,40 @@ +#= +Created on 08 Aug 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 22 of Project Euler +https://projecteuler.net/problem=22 +=# + +using DelimitedFiles + +function Problem22() + #= + Using names.txt, a 46K text file containing over five-thousand first names, + begin by sorting it into alphabetical order. Then working out the + alphabetical value for each name, multiply this value by its alphabetical + position in the list to obtain a name score. + + For example, when the list is sorted into alphabetical order, COLIN, which + 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. + + What is the total of all the name scores in the file? + =# + file = "/datos/Scripts/Gitea/Project_Euler/src/files/Problem22.txt" + names = sort(readdlm(file, ',', String)[:]) + + result = 0 + for (idx,name) in enumerate(names) + result += sum(Int(c) - 64 for c in name) * idx + end + return result +end + + +println("Time to evaluate Problem 22:") +@time Problem22() +println("") +println("Result for Problem 22: ", Problem22()) diff --git a/src/Julia/Problems001-050/Problem023.jl b/src/Julia/Problems001-050/Problem023.jl new file mode 100644 index 0000000..bd1d940 --- /dev/null +++ b/src/Julia/Problems001-050/Problem023.jl @@ -0,0 +1,60 @@ +#= +Created on 11 Aug 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 23 of Project Euler +https://projecteuler.net/problem=23 +=# + +function Problem23() + #= + A perfect number is a number for which the sum of its proper divisors is + exactly equal to the number. For example, the sum of the proper divisors + of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect + number. + + A number n is called deficient if the sum of its proper divisors is less + than n and it is called abundant if this sum exceeds n. + + As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest + number that can be written as the sum of two abundant numbers is 24. By + mathematical analysis, it can be shown that all integers greater than 28123 + can be written as the sum of two abundant numbers. However, this upper + limit cannot be reduced any further by analysis even though it is known + that the greatest number that cannot be expressed as the sum of two + abundant numbers is less than this limit. + + Find the sum of all the positive integers which cannot be written as the + sum of two abundant numbers. + =# + LIMIT = 28123 + divisorsum = zeros(Int32,LIMIT) + for i in range(1, LIMIT, step=1) + for j in range(2i, LIMIT, step=i) + divisorsum[j] += i + end + end + abundantnums = [i for (i, x) in enumerate(divisorsum) if x > i] + + expressible = falses(LIMIT) + for i in abundantnums + for j in abundantnums + if i + j < LIMIT+1 + expressible[i + j] = true + else + break + end + end + end + + ans = sum(i for (i, x) in enumerate(expressible) if x == false) + return ans +end + + +println("Time to evaluate Problem 23:") +@time Problem23() +println("") +println("Result for Problem 23: ", Problem23()) diff --git a/src/Julia/Problems001-050/Problem024.jl b/src/Julia/Problems001-050/Problem024.jl new file mode 100644 index 0000000..a128b83 --- /dev/null +++ b/src/Julia/Problems001-050/Problem024.jl @@ -0,0 +1,34 @@ +#= +Created on 13 Aug 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 24 of Project Euler +https://projecteuler.net/problem=24 +=# + +using Combinatorics + +function Problem24() + #= + A permutation is an ordered arrangement of objects. For example, 3124 is + one possible permutation of the digits 1, 2, 3 and 4. If all of the + permutations are listed numerically or alphabetically, we call it + lexicographic order. The lexicographic permutations of 0, 1 and 2 are: + + 012 021 102 120 201 210 + + What is the millionth lexicographic permutation of the digits + 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? + =# + digits = [0,1,2,3,4,5,6,7,8,9] + _permutations = nthperm(digits, 1_000_000) + return join(_permutations) +end + + +println("Time to evaluate Problem 24:") +@time Problem24() +println("") +println("Result for Problem 24: ", Problem24()) diff --git a/src/Julia/Problems001-050/Problem025.jl b/src/Julia/Problems001-050/Problem025.jl new file mode 100644 index 0000000..0f9d581 --- /dev/null +++ b/src/Julia/Problems001-050/Problem025.jl @@ -0,0 +1,52 @@ +#= +Created on 15 Aug 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 25 of Project Euler +https://projecteuler.net/problem=25 +=# + +function Problem25() + #= + The Fibonacci sequence is defined by the recurrence relation: + + Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1. + + Hence the first 12 terms will be: + + F1 = 1 + F2 = 1 + F3 = 2 + F4 = 3 + F5 = 5 + F6 = 8 + F7 = 13 + F8 = 21 + F9 = 34 + F10 = 55 + F11 = 89 + F12 = 144 + + 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 + contain 1000 digits? + =# + a, b = 1, 1 + index = 2 + + while length(digits(b)) < 1000 + a, b = big(b), big(b+a) + index += 1 + end + + return index +end + + +println("Time to evaluate Problem 25:") +@time Problem25() +println("") +println("Result for Problem 25: ", Problem25()) diff --git a/src/Julia/Problems001-050/Problem026.jl b/src/Julia/Problems001-050/Problem026.jl new file mode 100644 index 0000000..160ccc4 --- /dev/null +++ b/src/Julia/Problems001-050/Problem026.jl @@ -0,0 +1,53 @@ +#= +Created on 16 Aug 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 26 of Project Euler +https://projecteuler.net/problem=26 +=# + +function Problem26() + #= + A unit fraction contains 1 in the numerator. The decimal representation + of the unit fractions with denominators 2 to 10 are given: + + 1/2 = 0.5 + 1/3 = 0.(3) + 1/4 = 0.25 + 1/5 = 0.2 + 1/6 = 0.1(6) + 1/7 = 0.(142857) + 1/8 = 0.125 + 1/9 = 0.(1) + 1/10 = 0.1 + + Where 0.1(6) means 0.166666..., and has a 1-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 + cycle in its decimal fraction part. + =# + cycle_length = 0 + number_d = 0 + for number in 3:2:999 + if number % 5 == 0 + continue + end + p = 1 + while (big(10)^p % number) != 1 + p += 1 + end + if p > cycle_length + cycle_length, number_d = p, number + end + end + return number_d +end + + +println("Time to evaluate Problem 26:") +@time Problem26() +println("") +println("Result for Problem 26: ", Problem26()) diff --git a/src/Julia/Problems001-050/Problem027.jl b/src/Julia/Problems001-050/Problem027.jl new file mode 100644 index 0000000..35a5aaf --- /dev/null +++ b/src/Julia/Problems001-050/Problem027.jl @@ -0,0 +1,61 @@ +#= +Created on 19 Aug 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 27 of Project Euler +https://projecteuler.net/problem=27 +=# + +using Primes + +function Problem27() + #= + Euler discovered the remarkable quadratic formula: + + n^2 + n + 41 + + It turns out that the formula will produce 40 primes for the consecutive + integer values 0≤n≤39. However, when n=40, 40^2+40+41=40(40+1)+41 is + divisible by 41, and certainly when n=41,41^2+41+41 is clearly divisible + by 41. + + The incredible formula n^2−79n+1601 was discovered, which produces 80 + primes for the consecutive values 0≤n≤79. The product of the coefficients, + −79 and 1601, is −126479. + + Considering quadratics of the form: + + n^2 + an + b + + where |a|<1000, |b|≤1000 and |n| is the modulus/absolute value of n + e.g. |11|=11 and |−4|=4 + + 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, + starting with n=0. + =# + LIMIT = 1000 + consecutive_values = 0 + c = 0 + for a in -999:LIMIT-1 + for b in 0:LIMIT + n = 0 + while isprime((n^2) + (a * n) + b) + n += 1 + if n > consecutive_values + consecutive_values = n + c = a * b + end + end + end + end + return c +end + + +println("Time to evaluate Problem 27:") +@time Problem27() +println("") +println("Result for Problem 27: ", Problem27()) diff --git a/src/Julia/Problems001-050/Problem028.jl b/src/Julia/Problems001-050/Problem028.jl new file mode 100644 index 0000000..17c23a7 --- /dev/null +++ b/src/Julia/Problems001-050/Problem028.jl @@ -0,0 +1,49 @@ +#= +Created on 23 Aug 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 28 of Project Euler +https://projecteuler.net/problem=28 +=# + +using BenchmarkTools + +function Problem28() + #= + Starting with the number 1 and moving to the right in a clockwise + direction a 5 by 5 spiral is formed as follows: + + 21 22 23 24 25 + 20 7 8 9 10 + 19 6 1 2 11 + 18 5 4 3 12 + 17 16 15 14 13 + + 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 + formed in the same way? + =# + size = 1001 # Must be odd + ans = 1 # Special case for size 1 + step = 0 + i, cur = 1, 1 + while step < size-1 + step = i * 2 + for j in 1:4 + cur += step + ans += cur + end + i += 1 + end + + return ans +end + + +println("Time to evaluate Problem 28:") +@btime Problem28() +println("") +println("Result for Problem 28: ", Problem28()) diff --git a/src/Julia/Problems001-050/Problem029.jl b/src/Julia/Problems001-050/Problem029.jl new file mode 100644 index 0000000..94c2881 --- /dev/null +++ b/src/Julia/Problems001-050/Problem029.jl @@ -0,0 +1,38 @@ +#= +Created on 23 Aug 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 29 of Project Euler +https://projecteuler.net/problem=29 +=# + +using BenchmarkTools + +function Problem29() + #= + Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5: + + 2^2=4, 2^3=8, 2^4=16, 2^5=32 + 3^2=9, 3^3=27, 3^4=81, 3^5=243 + 4^2=16, 4^3=64, 4^4=256, 4^5=1024 + 5^2=25, 5^3=125, 5^4=625, 5^5=3125 + + If they are then placed in numerical order, with any repeats removed, we + get the following sequence of 15 distinct terms: + + 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 2≤a≤100 + and 2≤b≤100? + =# + terms = Set(big(a)^b for a in 2:100, b in 2:100) + return length(terms) +end + + +println("Time to evaluate Problem 29:") +@btime Problem29() +println("") +println("Result for Problem 29: ", Problem29()) diff --git a/src/Julia/Problems001-050/Problem030.jl b/src/Julia/Problems001-050/Problem030.jl new file mode 100644 index 0000000..6922e07 --- /dev/null +++ b/src/Julia/Problems001-050/Problem030.jl @@ -0,0 +1,47 @@ +#= +Created on 25 Aug 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 30 of Project Euler +https://projecteuler.net/problem=30 +=# + +using BenchmarkTools + +function power_digit_sum(pow, n) + s = 0 + while n > 0 + (n, r) = divrem(n, 10) + s += r^pow + end + return s +end + +function Problem30() + #= + Surprisingly there are only three numbers that can be written as the sum + of fourth powers of their digits: + + 1634 = 1^4 + 6^4 + 3^4 + 4^4 + 8208 = 8^4 + 2^4 + 0^4 + 8^4 + 9474 = 9^4 + 4^4 + 7^4 + 4^4 + + As 1 = 14 is not a sum it is not included. + + The sum of these numbers is 1634 + 8208 + 9474 = 19316. + + Find the sum of all the numbers that can be written as the sum of fifth + powers of their digits. + =# + ans = sum(i for i in 2:1_000_000 if i == power_digit_sum(5, i)) + + return ans +end + + +println("Time to evaluate Problem 30:") +@btime Problem30() +println("") +println("Result for Problem 30: ", Problem30()) diff --git a/src/Julia/Problems001-050/Problem031.jl b/src/Julia/Problems001-050/Problem031.jl new file mode 100644 index 0000000..e139fc9 --- /dev/null +++ b/src/Julia/Problems001-050/Problem031.jl @@ -0,0 +1,45 @@ +#= +Created on 27 Aug 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 31 of Project Euler +https://projecteuler.net/problem=31 +=# + +using BenchmarkTools +using IterTools + +function Problem31() + #= + In the United Kingdom the currency is made up of pound (£) and pence (p). + There are eight coins in general circulation: + + 1p, 2p, 5p, 10p, 20p, 50p, £1 (100p), and £2 (200p). + + It is possible to make £2 in the following way: + + 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? + =# + no_ways = 0 + + coins = [2, 5, 10, 20, 50, 100] + + bunch_of_coins = product([0:i:200 for i in coins]...) + for money in bunch_of_coins + if sum(money) <= 200 + no_ways += 1 + end + end + # consider also the case for 200 coins of 1p + return no_ways + 1 +end + + +println("Time to evaluate Problem 31:") +@btime Problem31() +println("") +println("Result for Problem 31: ", Problem31()) diff --git a/src/Julia/Problems001-050/Problem032.jl b/src/Julia/Problems001-050/Problem032.jl new file mode 100644 index 0000000..3f66546 --- /dev/null +++ b/src/Julia/Problems001-050/Problem032.jl @@ -0,0 +1,47 @@ +#= +Created on 30 Aug 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 32 of Project Euler +https://projecteuler.net/problem=32 +=# + +using BenchmarkTools + +function Problem32() + #= + We shall say that an n-digit number is pandigital if it makes use of all + the digits 1 to n exactly once; for example, the 5-digit number, 15234, is + 1 through 5 pandigital. + + The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing + multiplicand, multiplier, and product is 1 through 9 pandigital. + + Find the sum of all products whose multiplicand/multiplier/product identity + 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 + include it once in your sum. + =# + + ans = Set() + pandigital = join(['1', '2', '3', '4', '5', '6', '7', '8', '9']) + + for x in 1:100 + for y in 100:10_000 + # product = x * y + if join(sort(collect(string(x) * string(y) * string(x * y)))) == pandigital + push!(ans, x * y) + end + end + end + + return sum(ans) +end + + +println("Time to evaluate Problem 32:") +@btime Problem32() +println("") +println("Result for Problem 32: ", Problem32()) diff --git a/src/Julia/Problems001-050/Problem033.jl b/src/Julia/Problems001-050/Problem033.jl new file mode 100644 index 0000000..dc32286 --- /dev/null +++ b/src/Julia/Problems001-050/Problem033.jl @@ -0,0 +1,51 @@ +#= +Created on 01 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 33 of Project Euler +https://projecteuler.net/problem=33 +=# + +using BenchmarkTools + +function Problem33() + #= + The fraction 49/98 is a curious fraction, as an inexperienced mathematician + in attempting to simplify it may incorrectly believe that 49/98 = 4/8, + which is correct, is obtained by cancelling the 9s. + + We shall consider fractions like, 30/50 = 3/5, to be trivial examples. + + There are exactly four non-trivial examples of this type of fraction, less + 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, + find the value of the denominator. + =# + numerator = 1 + denominator = 1 + for x in 10:99 + for y in 10:99 + if x < y + if reverse(digits(x))[2] == reverse(digits(y))[1] + if reverse(digits(y))[2] != 0 + if reverse(digits(x))[1] / reverse(digits(y))[2] == x / y + numerator *= x + denominator *= y + end + end + end + end + end + end + + return Int(denominator/numerator) +end + + +println("Time to evaluate Problem 33:") +@btime Problem33() +println("") +println("Result for Problem 33: ", Problem33()) diff --git a/src/Julia/Problems001-050/Problem034.jl b/src/Julia/Problems001-050/Problem034.jl new file mode 100644 index 0000000..d5088e9 --- /dev/null +++ b/src/Julia/Problems001-050/Problem034.jl @@ -0,0 +1,42 @@ +#= +Created on 01 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 34 of Project Euler +https://projecteuler.net/problem=34 +=# + +using BenchmarkTools + +function Problem34() + #= + 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. + + Find the sum of all numbers which are equal to the sum of the factorial + of their digits. + + Note: As 1! = 1 and 2! = 2 are not sums they are not included. + =# + + ans = 0 + + for num in 10:2_540_160 + sum_of_factorial = 0 + for digit in reverse(digits(num)) + sum_of_factorial += factorial(digit) + if sum_of_factorial == num + ans += sum_of_factorial + end + end + end + + return ans +end + + +println("Time to evaluate Problem 34:") +@btime Problem34() +println("") +println("Result for Problem 34: ", Problem34()) diff --git a/src/Julia/Problems001-050/Problem035.jl b/src/Julia/Problems001-050/Problem035.jl new file mode 100644 index 0000000..8845cba --- /dev/null +++ b/src/Julia/Problems001-050/Problem035.jl @@ -0,0 +1,72 @@ +#= +Created on 02 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 35 of Project Euler +https://projecteuler.net/problem=35 +=# + +using BenchmarkTools +using Combinatorics +using Primes + +# function circular_number(n) +# result = [] +# perms = collect(permutations(digits(n), length(n))) +# for i in perms +# push!(result, parse(Int, join(i))) +# end +# return result +# end + +function circular_number(n) + if n <10 + return [n] + end + digs=digits(n) + d=length(digs) + cyc=zeros(Int,d) + x=[10^i for i in 0:d-1] + for i in 1:d + cyc[i]=sum(x .* digs[vcat(i:d,1:i-1)]) + end + return cyc +end + +function Problem35() + #= + The number, 197, is called a circular prime because all rotations of the + digits: 197, 971, and 719, are themselves prime. + + There are thirteen such primes below 100: + 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. + + How many circular primes are there below one million? + =# + circular_primes = [] + cnt = 0 + for i in 2:1_000_000 + if isprime(i) + all_primes = true + for j in circular_number(i) + if !isprime(j) + all_primes = false + break + end + end + if all_primes + cnt +=1 #push!(circular_primes, i) + end + end + end + + return cnt #length(circular_primes) +end + + +println("Time to evaluate Problem 35:") +@btime Problem35() +println("") +println("Result for Problem 35: ", Problem35()) diff --git a/src/Julia/Problems001-050/Problem036.jl b/src/Julia/Problems001-050/Problem036.jl new file mode 100644 index 0000000..f61165d --- /dev/null +++ b/src/Julia/Problems001-050/Problem036.jl @@ -0,0 +1,38 @@ +#= +Created on 04 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 36 of Project Euler +https://projecteuler.net/problem=36 +=# + +using BenchmarkTools + +function is_palindrome(num) + return num == reverse(num) +end + +function Problem36() + #= + The decimal number, 585 = 1001001001_2 (binary), is palindromic + in both bases. + + Find the sum of all numbers, less than one million, which are palindromic + in base 10 and base 2. + =# + ans = 0 + for n in 1:2:1_000_000 + if is_palindrome(digits(n,base=10)) && is_palindrome(digits(n,base=2)) + ans += n + end + end + return ans +end + + +println("Time to evaluate Problem 36:") +@btime Problem36() +println("") +println("Result for Problem 36: ", Problem36()) diff --git a/src/Julia/Problems001-050/Problem037.jl b/src/Julia/Problems001-050/Problem037.jl new file mode 100644 index 0000000..da5dae2 --- /dev/null +++ b/src/Julia/Problems001-050/Problem037.jl @@ -0,0 +1,53 @@ +#= +Created on 07 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 37 of Project Euler +https://projecteuler.net/problem=37 +=# + +using BenchmarkTools +using Primes + +function is_truncatable_prime(number) + num_str_rev = reverse(digits(number)) + + for (idx,num) in enumerate(join(num_str_rev)) + if !isprime(parse(Int, join(num_str_rev[idx:end]))) || + !isprime(parse(Int, join(num_str_rev[1:end-idx+1]))) + return false + end + end + return true +end + +function Problem37() + """ + The number 3797 has an interesting property. Being prime itself, it is + possible to continuously remove digits from left to right, and remain + prime at each stage: 3797, 797, 97, and 7. + Similarly we can work from right to left: 3797, 379, 37, and 3. + + Find the sum of the only eleven primes that are both truncatable from left + to right and right to left. + + NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes. + """ + ans = 0 + # Statement of the problem says this + primes_list = primes(11, 1_000_000) + for num in primes_list + if is_truncatable_prime(num) + ans += num + end + end + return ans +end + + +println("Time to evaluate Problem 37:") +@btime Problem37() +println("") +println("Result for Problem 37: ", Problem37()) diff --git a/src/Julia/Problems001-050/Problem038.jl b/src/Julia/Problems001-050/Problem038.jl new file mode 100644 index 0000000..13e2c56 --- /dev/null +++ b/src/Julia/Problems001-050/Problem038.jl @@ -0,0 +1,56 @@ +#= +Created on 08 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 38 of Project Euler +https://projecteuler.net/problem=38 +=# + +using BenchmarkTools + +function Problem38() + #= + Take the number 192 and multiply it by each of 1, 2, and 3: + + 192 × 1 = 192 + 192 × 2 = 384 + 192 × 3 = 576 + + By concatenating each product we get the 1 to 9 pandigital, + 192384576. We will call 192384576 the concatenated product of + 192 and (1,2,3) + + The same can be achieved by starting with 9 and multiplying by + 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is + the concatenated product of 9 and (1,2,3,4,5). + + What is the largest 1 to 9 pandigital 9-digit number that can + be formed as the concatenated product of an integer with + (1,2, ... , n) where n > 1? + =# + + results = [] + pandigital = join(['1', '2', '3', '4', '5', '6', '7', '8', '9']) + # Number must 4 digits (exactly) to be pandigital + # if n > 1 + for i in 1:10_000 + integer = 1 + number = "" + while length(number) < 9 + number *= string(integer * i) + if join(sort(collect(number))) == pandigital + push!(results,number) + end + integer += 1 + end + end + return maximum(results) +end + + +println("Time to evaluate Problem 38:") +@btime Problem38() +println("") +println("Result for Problem 38: ", Problem38()) diff --git a/src/Julia/Problems001-050/Problem039.jl b/src/Julia/Problems001-050/Problem039.jl new file mode 100644 index 0000000..177fcda --- /dev/null +++ b/src/Julia/Problems001-050/Problem039.jl @@ -0,0 +1,49 @@ +#= +Created on 09 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 39 of Project Euler +https://projecteuler.net/problem=39 +=# + +using BenchmarkTools + +function Problem39() + #= + If p is the perimeter of a right angle triangle with integral length sides, + {a,b,c}, there are exactly three solutions for p = 120: + + {20,48,52}, {24,45,51}, {30,40,50} + + For which value of p ≤ 1000, is the number of solutions maximised? + =# + ans, val = 0, 0 + for p in 2:2:1000 + sol = 0 + for a in 1:p + for b in a+1:p-2*a + c = p - (a + b) + if a^2 + b^2 == c^2 + sol += 1 + elseif a^2 + b^2 > c^2 + # As we continue our innermost loop, the left side + # gets bigger, right gets smaller, so we're done here + break + end + end + end + if sol > ans + ans, val = sol, p + end + end + + return val +end + + +println("Time to evaluate Problem 39:") +@btime Problem39() +println("") +println("Result for Problem 39: ", Problem39()) diff --git a/src/Julia/Problems001-050/Problem040.jl b/src/Julia/Problems001-050/Problem040.jl new file mode 100644 index 0000000..b5c0e4a --- /dev/null +++ b/src/Julia/Problems001-050/Problem040.jl @@ -0,0 +1,40 @@ +#= +Created on 10 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 40 of Project Euler +https://projecteuler.net/problem=40 +=# + +using BenchmarkTools + +function Problem40() + #= + An irrational decimal fraction is created by concatenating the positive integers: + + 0.123456789101112131415161718192021... + + It can be seen that the 12th digit of the fractional part is 1. + + 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} + + =# + ans = 1 + fraction = join([i for i in 1:1_000_000]) + + for i in 0:6 + ans *= parse(Int, fraction[10^i]) + end + + return ans +end + + +println("Time to evaluate Problem 40:") +@btime Problem40() +println("") +println("Result for Problem 40: ", Problem40()) diff --git a/src/Julia/Problems001-050/Problem041.jl b/src/Julia/Problems001-050/Problem041.jl new file mode 100644 index 0000000..412242e --- /dev/null +++ b/src/Julia/Problems001-050/Problem041.jl @@ -0,0 +1,43 @@ +#= +Created on 11 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 41 of Project Euler +https://projecteuler.net/problem=41 +=# + +using BenchmarkTools +using Primes + +function is_pandigital(number) + number_ = join(sort(digits(number))) + check = join([i for i in 1:length(digits(number))]) + if number_ == check + return true + end + return false +end + +function Problem41() + #= + We shall say that an n-digit number is pandigital if it makes + use of all the digits 1 to n exactly once. For example, 2143 is + a 4-digit pandigital and is also prime. + + What is the largest n-digit pandigital prime that exists? + =# + + for ans in 7654321:-1:1 + if is_pandigital(ans) & isprime(ans) + return ans + end + end +end + + +println("Time to evaluate Problem 41:") +@btime Problem41() +println("") +println("Result for Problem 41: ", Problem41()) diff --git a/src/Julia/Problems001-050/Problem042.jl b/src/Julia/Problems001-050/Problem042.jl new file mode 100644 index 0000000..b741859 --- /dev/null +++ b/src/Julia/Problems001-050/Problem042.jl @@ -0,0 +1,55 @@ +#= +Created on 12 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 42 of Project Euler +https://projecteuler.net/problem=42 +=# + +using BenchmarkTools +using DelimitedFiles + +function triangle_number(num) + return Int(0.5*num*(num+1)) +end + +function word_to_value(word) + return sum(Int(letter)-64 for letter in word) +end + +function Problem42() + #= + The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); + so the first ten triangle numbers are: + + 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... + + By converting each letter in a word to a number corresponding to its alphabetical + position and adding these values we form a word value. For example, the word value + for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a triangle number then we + shall call the word a triangle word. + + Using words.txt, a 16K text file containing nearly two-thousand common English words, + how many are triangle words? + =# + + triangular_numbers = [triangle_number(n) for n in 1:26] + ans = 0 + file = "/datos/Scripts/Gitea/Project_Euler/src/files/Problem42.txt" + words = sort(readdlm(file, ',', String)[:]) + + for word in words + if word_to_value(word) in triangular_numbers + ans += 1 + end + end + return ans +end + + +println("Time to evaluate Problem 42:") +@btime Problem42() +println("") +println("Result for Problem 42: ", Problem42()) diff --git a/src/Julia/Problems001-050/Problem043.jl b/src/Julia/Problems001-050/Problem043.jl new file mode 100644 index 0000000..110893c --- /dev/null +++ b/src/Julia/Problems001-050/Problem043.jl @@ -0,0 +1,64 @@ +#= +Created on 13 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 43 of Project Euler +https://projecteuler.net/problem=43 +=# + +using BenchmarkTools +using Combinatorics + +function Problem43() + #= + The number, 1406357289, is a 0 to 9 pandigital number because + it is made up of each of the digits 0 to 9 in some order, but + it also has a rather interesting sub-string divisibility property. + + Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this + way, we note the following: + + d2d3d4=406 is divisible by 2 + d3d4d5=063 is divisible by 3 + d4d5d6=635 is divisible by 5 + d5d6d7=357 is divisible by 7 + d6d7d8=572 is divisible by 11 + d7d8d9=728 is divisible by 13 + d8d9d10=289 is divisible by 17 + + Find the sum of all 0 to 9 pandigital numbers with this property. + =# + ans = [] + pandigital = join(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']) + + for n in permutations(pandigital) + n_ =join(n) + if n_[1] != 0 && join(sort(n)) == pandigital + if parse(Int, n_[8:end]) % 17 == 0 + if parse(Int, n_[7:9]) % 13 == 0 + if parse(Int, n_[6:8]) % 11 == 0 + if parse(Int, n_[5:7]) % 7 == 0 + if parse(Int, n_[4:6]) % 5 == 0 + if parse(Int, n_[3:5]) % 3 == 0 + if parse(Int, n_[2:4]) % 2 == 0 + push!(ans, n_) + end + end + end + end + end + end + end + end + end + + return sum(parse(Int, num) for num in ans) +end + + +println("Time to evaluate Problem 43:") +@btime Problem43() +println("") +println("Result for Problem 43: ", Problem43()) diff --git a/src/Julia/Problems001-050/Problem044.jl b/src/Julia/Problems001-050/Problem044.jl new file mode 100644 index 0000000..d7a5e88 --- /dev/null +++ b/src/Julia/Problems001-050/Problem044.jl @@ -0,0 +1,51 @@ +#= +Created on 14 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 44 of Project Euler +https://projecteuler.net/problem=44 +=# + +using BenchmarkTools +using Combinatorics + +function pentagonal(n) + return Int(n*(3*n-1)/2) +end + +function Problem44() + #= + Pentagonal numbers are generated by the formula, Pn=n(3n−1)/2. + The first ten pentagonal numbers are: + + 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ... + + It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their + difference, 70 − 22 = 48, is not pentagonal. + + Find the pair of pentagonal numbers, Pj and Pk, for which their + sum and difference are pentagonal and D = |Pk − Pj| is minimised. + + What is the value of D? + =# + dif = 0 + pentagonal_list = [pentagonal(n) for n in 1:2500] + pairs = combinations(pentagonal_list, 2) + for p in pairs + if reduce(+, p) in pentagonal_list && abs(reduce(-, p)) in pentagonal_list + dif = (abs(reduce(-,p))) + # the first one found would be the smallest + break + end + end + + return dif +end + + +println("Time to evaluate Problem 44:") +@btime Problem44() +println("") +println("Result for Problem 44: ", Problem44()) diff --git a/src/Julia/Problems001-050/Problem045.jl b/src/Julia/Problems001-050/Problem045.jl new file mode 100644 index 0000000..50aea45 --- /dev/null +++ b/src/Julia/Problems001-050/Problem045.jl @@ -0,0 +1,45 @@ +#= +Created on 15 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 45 of Project Euler +https://projecteuler.net/problem=45 +=# + +using BenchmarkTools + +function pentagonal(n) + return Int(n*(3*n-1)/2) +end + +function hexagonal(n) + return Int(n*(2*n-1)) +end + +function Problem45() + #= + Triangle, pentagonal, and hexagonal numbers are generated by the following formulae: + Triangle Tn=n(n+1)/2 1, 3, 6, 10, 15, ... + Pentagonal Pn=n(3n−1)/2 1, 5, 12, 22, 35, ... + Hexagonal Hn=n(2n−1) 1, 6, 15, 28, 45, ... + + It can be verified that T285 = P165 = H143 = 40755. + + Find the next triangle number that is also pentagonal and hexagonal. + =# + pentagonal_list = Set(pentagonal(n) for n in 2:100_000) + # all hexagonal numbers are also triangle numbers! + hexagonal_list = Set(hexagonal(n) for n in 2:100_000) + + ans = sort!(collect(intersect(hexagonal_list, pentagonal_list))) + # First one is already known + return ans[2] +end + + +println("Time to evaluate Problem 45:") +@btime Problem45() +println("") +println("Result for Problem 45: ", Problem45()) \ No newline at end of file diff --git a/src/Julia/Problems001-050/Problem046.jl b/src/Julia/Problems001-050/Problem046.jl new file mode 100644 index 0000000..27df21e --- /dev/null +++ b/src/Julia/Problems001-050/Problem046.jl @@ -0,0 +1,53 @@ +#= +Created on 16 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 46 of Project Euler +https://projecteuler.net/problem=46 +=# + +using BenchmarkTools +using Primes + +function is_goldbach(number) + for i in number - 1:-1:1 + if isprime(i) & ((((number - i) / 2)^0.5)%1==0) + return true + end + end + return false +end + +function Problem46() + #= + It was proposed by Christian Goldbach that every odd composite number + can be written as the sum of a prime and twice a square. + + 9 = 7 + 2×1^2 + 15 = 7 + 2×2^2 + 21 = 3 + 2×3^2 + 25 = 7 + 2×3^2 + 27 = 19 + 2×2^2 + 33 = 31 + 2×1^2 + + It turns out that the conjecture was false. + + What is the smallest odd composite that cannot be written as the sum + of a prime and twice a square? + =# + ans = 9 + while true + ans += 2 + if !isprime(ans) & !is_goldbach(ans) + return ans + end + end +end + + +println("Time to evaluate Problem 46:") +@btime Problem46() +println("") +println("Result for Problem 46: ", Problem46()) \ No newline at end of file diff --git a/src/Julia/Problems001-050/Problem047.jl b/src/Julia/Problems001-050/Problem047.jl new file mode 100644 index 0000000..99765ba --- /dev/null +++ b/src/Julia/Problems001-050/Problem047.jl @@ -0,0 +1,65 @@ +#= +Created on 18 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 47 of Project Euler +https://projecteuler.net/problem=47 +=# + +using BenchmarkTools + +function factor(n) + ans = [] + d = 2 + while d*d <= n + if n % d == 0 + push!(ans,d) + n = n ÷ d + else + d += 1 + end + end + if n > 1 + push!(ans,n) + end + return ans +end + +function Problem47() + #= + The first two consecutive numbers to have two distinct prime factors are: + + 14 = 2 × 7 + 15 = 3 × 5 + + The first three consecutive numbers to have three distinct prime factors are: + + 644 = 2² × 7 × 23 + 645 = 3 × 5 × 43 + 646 = 2 × 17 × 19. + + Find the first four consecutive integers to have four distinct prime factors each. + What is the first of these numbers? + =# + ans = [] + + for number in 1:1_000_000 + if length(ans) == 4 + break + elseif length(Set(factor(number))) == 4 + push!(ans,number) + else + ans = [] + end + end + + return ans[1] +end + + +println("Time to evaluate Problem 47:") +@btime Problem47() +println("") +println("Result for Problem 47: ", Problem47()) diff --git a/src/Julia/Problems001-050/Problem048.jl b/src/Julia/Problems001-050/Problem048.jl new file mode 100644 index 0000000..b23eeb7 --- /dev/null +++ b/src/Julia/Problems001-050/Problem048.jl @@ -0,0 +1,27 @@ +#= +Created on 18 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 48 of Project Euler +https://projecteuler.net/problem=48 +=# + +using BenchmarkTools + +function Problem48() + #= + 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. + =# + series = sum(big(i)^i for i in 1:1000) + return string(series)[end-9:end] +end + + +println("Time to evaluate Problem 48:") +@btime Problem48() +println("") +println("Result for Problem 48: ", Problem48()) diff --git a/src/Julia/Problems001-050/Problem049.jl b/src/Julia/Problems001-050/Problem049.jl new file mode 100644 index 0000000..dc4ca17 --- /dev/null +++ b/src/Julia/Problems001-050/Problem049.jl @@ -0,0 +1,44 @@ +#= +Created on 19 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 49 of Project Euler +https://projecteuler.net/problem=49 +=# + +using BenchmarkTools +using Primes + +function Problem49() + #= + The arithmetic sequence, 1487, 4817, 8147, in which each of the terms + increases by 3330, is unusual in two ways: + (i) each of the three terms are prime, and, + (ii) each of the 4-digit numbers are permutations of one another. + + 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. + + What 12-digit number do you form by concatenating the three terms in this sequence? + =# + ans = [] + primes_list = primes(1_000, 10_000) + + for number in primes_list + 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 + push!(ans, (string(number)*string(number+3300)*string(number+6660))) + end + end + end + # return the second one + return ans[2] +end + + +println("Time to evaluate Problem 49:") +@btime Problem49() +println("") +println("Result for Problem 49: ", Problem49()) diff --git a/src/Julia/Problems001-050/Problem050.jl b/src/Julia/Problems001-050/Problem050.jl new file mode 100644 index 0000000..e890591 --- /dev/null +++ b/src/Julia/Problems001-050/Problem050.jl @@ -0,0 +1,54 @@ +#= +Created on 20 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 50 of Project Euler +https://projecteuler.net/problem=50 +=# + +using BenchmarkTools +using Primes + +function Problem50() + #= + The prime 41, can be written as the sum of six consecutive primes: + + 41 = 2 + 3 + 5 + 7 + 11 + 13 + + This is the longest sum of consecutive primes that adds to a prime below one-hundred. + + The longest sum of consecutive primes below one-thousand that adds to a prime, + contains 21 terms, and is equal to 953. + + Which prime, below one-million, can be written as the sum of the most consecutive primes? + =# + + ans = 0 + result = 0 + prime_list = primes(1_000_000) + + for i in 1:length(prime_list) + sum = 0 + count = 0 + for j in prime_list[i:end] + sum += j + count += 1 + if isprime(sum) && count > result + result = count + ans = sum + end + if sum > 1_000_000 + break + end + end + end + return ans +end + + +println("Time to evaluate Problem 50:") +@btime Problem50() +println("") +println("Result for Problem 50: ", Problem50()) \ No newline at end of file