Compare commits
10 Commits
75d29d32e6
...
4a960cf1ad
| Author | SHA1 | Date | |
|---|---|---|---|
| 4a960cf1ad | |||
| 5a2ad49902 | |||
| 28ba539204 | |||
| 9983ee23d6 | |||
| 9a953a1529 | |||
| 1d0b05c0d3 | |||
| 48eede9ade | |||
| de814188e2 | |||
| d60dccb6b8 | |||
| fdf280d070 |
136
README.md
136
README.md
@@ -1,67 +1,81 @@
|
||||
Having a bit of fun trying to solve the problems from https://projecteuler.net/archives using different programming languages for learning purposes.
|
||||
|
||||
####
|
||||
Discrepancies in problem 11. Python result is 70600674 and in Julia is 0.
|
||||
Discrepancies in:
|
||||
- Problem 11: Python result is 70600674 and in Julia is --
|
||||
- Problem 24: Python result is 2783915460 and in Julia is --
|
||||
- Problem 31: Python result is 73682 and in Julia is --
|
||||
- Problem 35: Python result is 55 and in Julia is --
|
||||
- Problem 43: Python result is 16695334890 and in Julia is --
|
||||
- Problem 44: Python result is 5482660 and in Julia is --
|
||||
- Problem 53: Python result is 4075 and in Julia is --
|
||||
- Problem 54: Python result is 376 and in Julia is 0
|
||||
- Problem 59: Python result is 129448 and in Julia is --
|
||||
- Problem 60: Python result is 26033 and in Julia is nothing
|
||||
- Problem 61: Python result is 28684 and in Julia is 49
|
||||
- Problem 62: Python result is 127035954683 and in Julia is --
|
||||
|
||||
Discrepancies in problem 54. Python result is 376 and in Julia is 0.
|
||||
| Problem # | Result | T_exec (Python) | T_exec (Julia) |
|
||||
| :-------: | --------------: | --------------: | -------------: |
|
||||
| 1 | 233_168 | 0.154 ms | 3.374 μs |
|
||||
| 2 | 4_613_732 | 0.005 ms | 19.039 ns |
|
||||
| 3 | 6_857 | 0.334 ms | 14.372 μs |
|
||||
| 4 | 906_609 | 320.293 ms | 15.379 ms |
|
||||
| 5 | 232_792_560 | 0.010 ms | 889.854 ns |
|
||||
| 6 | 25_164_150 | 0.064 ms | 1.900 ns |
|
||||
| 7 | 104_743 | 300.693 ms | 12.235 ms |
|
||||
| 8 | 23_514_624_000 | 4.944 ms | 113.070 μs |
|
||||
| 9 | 31_875_000 | 51.913 ms | 225.186 μs |
|
||||
| 10 | 142_913_828_922 | 430.901 ms | 11.855 ms |
|
||||
| 11 | 70_600_674 | 1.965 ms | 0.034 ns |
|
||||
| 12 | 76_576_500 | 4.721 s | 566.421 ms |
|
||||
| 13 | 5_537_376_230 | 0.156 ms | 79.052 μs |
|
||||
| 14 | 837_799 | 2.697 s | 111.106 ms |
|
||||
| 15 | 137_846_528_820 | 0.013 ms | 1.780 μs |
|
||||
| 16 | 1_366 | 0.077 ms | 4.431 μs |
|
||||
| 17 | 21_124 | 9.656 ms | 4.070 ms |
|
||||
| 18 | 1_074 | 0.073 ms | 1.199 μs |
|
||||
| 19 | 171 | 0.365 ms | 23.465 μs |
|
||||
| 20 | 648 | 0.061 ms | 3.037 μs |
|
||||
| 21 | 31_626 | 3.359 s | 246.031 ms |
|
||||
| 22 | 871_198_282 | 10.707 ms | 14.502 ms |
|
||||
| 23 | 4_179_871 | 4.151 s | 85.176 ms |
|
||||
| 24 | 2_783_915_460 | 894.101 ms | 1.804 μs |
|
||||
| 25 | 4_782 | 55.092 ms | 31.359 ms |
|
||||
| 26 | 983 | 92.052 ms | 41.823 ms |
|
||||
| 27 | -59_231 | 3.499 s | 100.734 ms |
|
||||
| 28 | 669_171_001 | 0.513 ms | 1.899 ns |
|
||||
| 29 | 9_183 | 6.951 ms | 3.879 ms |
|
||||
| 30 | 443_839 | 3.486 s | 49.480 ms |
|
||||
| 31 | 73_682 | 3.674 s | 2.902 s |
|
||||
| 32 | 45_228 | 1.461 s | 1.118 s |
|
||||
| 33 | 100 | 3.002 ms | 1.228 ms |
|
||||
| 34 | 40_730 | 5.950 s | 691.940 ms |
|
||||
| 35 | 55 | 12.102 s | 357.072 ms |
|
||||
| 36 | 872_187 | 278.399 ms | 112.236 ms |
|
||||
| 37 | 748_317 | 743.781 ms | 483.269 ms |
|
||||
| 38 | 932_718_654 | 33.149 ms | 19.334 ms |
|
||||
| 39 | 840 | 23.803 s | 18.108 ms |
|
||||
| 40 | 210 | 261.554 ms | 144.162 ms |
|
||||
| 41 | 7_652_413 | 6.867 ms | 4.881 ms |
|
||||
| 42 | 162 | 3.149 ms | 4.194 ms |
|
||||
| 43 | 16_695_334_890 | 6.551 s | 6.276 s |
|
||||
| 44 | 5_482_660 | 331.350 ms | 5.039 s |
|
||||
| 45 | 1_533_776_805 | 95.618 ms | 8.301 ms |
|
||||
| 46 | 5_777 | 211.313 ms | 29.666 ms |
|
||||
| 47 | 134_043 | 1.682 s | 188.603 ms |
|
||||
| 48 | 9_110_846_700 | 11.690 ms | 2.193 ms |
|
||||
| 49 | 296_962_699_629 | 3.702 ms | 656.028 μs |
|
||||
| 50 | 997_651 | 24.830 s | 4.309 s |
|
||||
| 51 | 121_313 | 602.796 ms | 8.053 s |
|
||||
| 52 | 142_857 | 26.457 ms | 10.481 ms |
|
||||
| 53 | 4_075 | 14.253 ms | 4.226 ms |
|
||||
| 54 | 376 | 74.852 ms | 0.026 ns |
|
||||
| 55 | 249 | 59.307 ms | 97.997 ms |
|
||||
| 56 | 972 | 150.477 ms | 18.995 ms |
|
||||
| 57 | 153 | 17.138 ms | 6.794 ms |
|
||||
| 58 | 26_241 | 13.093 s | 7.237 s |
|
||||
| 59 | 129_448 | 1.290 s | 423.036 μs |
|
||||
| 001 | 233_168 | 0.165 ms | 3.614 μs |
|
||||
| 002 | 4_613_732 | 0.004 ms | 19.555 ns |
|
||||
| 003 | 6_857 | 0.314 ms | 14.389 μs |
|
||||
| 004 | 906_609 | 319.644 ms | 1.707 ms |
|
||||
| 005 | 232_792_560 | 0.012 ms | 808.461 ns |
|
||||
| 006 | 25_164_150 | 0.072 ms | 3.158 ns |
|
||||
| 007 | 104_743 | 304.030 ms | 4.133 ms |
|
||||
| 008 | 23_514_624_000 | 4.132 ms | 106.910 μs |
|
||||
| 009 | 31_875_000 | 58.148 ms | 225.199 μs |
|
||||
| 010 | 142_913_828_922 | 409.924 ms | 11.598 ms |
|
||||
| 011 | 70_600_674 | 2.082 ms | -- |
|
||||
| 012 | 76_576_500 | 4.735 s | 539.773 ms |
|
||||
| 013 | 5_537_376_230 | 25.673 ms | 69.562 μs |
|
||||
| 014 | 837_799 | 2.604 s | 106.610 ms |
|
||||
| 015 | 137_846_528_820 | 0.016 ms | 1.472 μs |
|
||||
| 016 | 1_366 | 0.083 ms | 3.453 μs |
|
||||
| 017 | 21_124 | 13.177 ms | 47.304 μs |
|
||||
| 018 | 1_074 | 0.110 ms | 1.231 μs |
|
||||
| 019 | 171 | 0.485 ms | 23.468 μs |
|
||||
| 020 | 648 | 0.069 ms | 2.657 μs |
|
||||
| 021 | 31_626 | 3.499 s | 4.289 ms |
|
||||
| 022 | 871_198_282 | 38.466 ms | 4.763 ms |
|
||||
| 023 | 4_179_871 | 4.207 s | 82.184 ms |
|
||||
| 024 | 2_783_915_460 | 1.015 s | -- |
|
||||
| 025 | 4_782 | 62.040 ms | 29.084 ms |
|
||||
| 026 | 983 | 99.642 ms | 37.576 ms |
|
||||
| 027 | -59_231 | 3.559 s | 22.211 ms |
|
||||
| 028 | 669_171_001 | 0.393 ms | 3.153 ns |
|
||||
| 029 | 9_183 | 9.184 ms | 3.797 ms |
|
||||
| 030 | 443_839 | 3.662 s | 49.453 ms |
|
||||
| 031 | 73_682 | 3.939 s | -- |
|
||||
| 032 | 45_228 | 1.529 s | 1.095 s |
|
||||
| 033 | 100 | 2.598 ms | 1.340 ms |
|
||||
| 034 | 40_730 | 5.812 s | 745.659 ms |
|
||||
| 035 | 55 | 11.863 s | -- |
|
||||
| 036 | 872_187 | 307.045 ms | 121.923 ms |
|
||||
| 037 | 748_317 | 792.588 ms | 451.307 ms |
|
||||
| 038 | 932_718_654 | 41.107 ms | 18.644 ms |
|
||||
| 039 | 840 | 24.633 s | 18.115 ms |
|
||||
| 040 | 210 | 246.492 ms | 127.943 ms |
|
||||
| 041 | 7_652_413 | 7.185 ms | 4.835 ms |
|
||||
| 042 | 162 | 26.712 ms | 951.547 μs |
|
||||
| 043 | 16_695_334_890 | 6.942 s | -- |
|
||||
| 044 | 5_482_660 | 331.891 ms | -- |
|
||||
| 045 | 1_533_776_805 | 93.169 ms | 8.435 ms |
|
||||
| 046 | 5_777 | 220.877 ms | 32.964 ms |
|
||||
| 047 | 134_043 | 1.692 s | 193.265 ms |
|
||||
| 048 | 9_110_846_700 | 11.318 ms | 2.180 ms |
|
||||
| 049 | 296_962_699_629 | 3.641 ms | 711.072 μs |
|
||||
| 050 | 997_651 | 25.066 s | 4.248 s |
|
||||
| 051 | 121_313 | 602.599 ms | 7.686 s |
|
||||
| 052 | 142_857 | 32.730 ms | 11.188 ms |
|
||||
| 053 | 4_075 | 12.028 ms | -- |
|
||||
| 054 | 376 | 104.144 ms | 1.895 ns |
|
||||
| 055 | 249 | 54.781 ms | 70.237 ms |
|
||||
| 056 | 972 | 163.642 ms | 16.350 ms |
|
||||
| 057 | 153 | 15.832 ms | 5.855 ms |
|
||||
| 058 | 26_241 | 13.617 s | 6.752 s |
|
||||
| 059 | 129_448 | 1.319 s | -- |
|
||||
| 060 | 26_033 | 61.160 s | 1.895 ns |
|
||||
| 061 | 28_684 | 42.680 ms | 84.347 μs |
|
||||
| 062 | 127_035_954_683 | 27.491 ms | -- |
|
||||
|
||||
63
src/Julia.json
Normal file
63
src/Julia.json
Normal file
@@ -0,0 +1,63 @@
|
||||
{
|
||||
"1": "Result is 233168 and took 3.614 μs",
|
||||
"2": "Result is 4613732 and took 19.555 ns",
|
||||
"3": "Result is 6857 and took 14.389 μs",
|
||||
"4": "Result is 906609 and took 1.707 ms",
|
||||
"5": "Result is 232792560 and took 808.461 ns",
|
||||
"6": "Result is 25164150 and took 3.158 ns",
|
||||
"7": "Result is 104743 and took 4.133 ms",
|
||||
"8": "Result is 23514624000 and took 106.910 μs",
|
||||
"9": "Result is 31875000 and took 225.199 μs",
|
||||
"10": "Result is 142913828922 and took 11.598 ms",
|
||||
"11": "Result is -- and took --",
|
||||
"12": "Result is 76576500 and took 539.773 ms",
|
||||
"13": "Result is 5537376230 and took 69.562 μs",
|
||||
"14": "Result is 837799 and took 106.610 ms",
|
||||
"15": "Result is 137846528820 and took 1.472 μs",
|
||||
"16": "Result is 1366 and took 3.453 μs",
|
||||
"17": "Result is 21124 and took 47.304 μs",
|
||||
"18": "Result is 1074 and took 1.231 μs",
|
||||
"19": "Result is 171 and took 23.468 μs",
|
||||
"20": "Result is 648 and took 2.657 μs",
|
||||
"21": "Result is 31626 and took 4.289 ms",
|
||||
"22": "Result is 871198282 and took 4.763 ms",
|
||||
"23": "Result is 4179871 and took 82.184 ms",
|
||||
"24": "Result is -- and took --",
|
||||
"25": "Result is 4782 and took 29.084 ms",
|
||||
"26": "Result is 983 and took 37.576 ms",
|
||||
"27": "Result is -59231 and took 22.211 ms",
|
||||
"28": "Result is 669171001 and took 3.153 ns",
|
||||
"29": "Result is 9183 and took 3.797 ms",
|
||||
"30": "Result is 443839 and took 49.453 ms",
|
||||
"31": "Result is -- and took --",
|
||||
"32": "Result is 45228 and took 1.095 s",
|
||||
"33": "Result is 100 and took 1.340 ms",
|
||||
"34": "Result is 40730 and took 745.659 ms",
|
||||
"35": "Result is -- and took --",
|
||||
"36": "Result is 872187 and took 121.923 ms",
|
||||
"37": "Result is 748317 and took 451.307 ms",
|
||||
"38": "Result is 932718654 and took 18.644 ms",
|
||||
"39": "Result is 840 and took 18.115 ms",
|
||||
"40": "Result is 210 and took 127.943 ms",
|
||||
"41": "Result is 7652413 and took 4.835 ms",
|
||||
"42": "Result is 162 and took 951.547 μs",
|
||||
"43": "Result is -- and took --",
|
||||
"44": "Result is -- and took --",
|
||||
"45": "Result is 1533776805 and took 8.435 ms",
|
||||
"46": "Result is 5777 and took 32.964 ms",
|
||||
"47": "Result is 134043 and took 193.265 ms",
|
||||
"48": "Result is 9110846700 and took 2.180 ms",
|
||||
"49": "Result is 296962699629 and took 711.072 μs",
|
||||
"50": "Result is 997651 and took 4.248 s",
|
||||
"51": "Result is 121313 and took 7.686 s",
|
||||
"52": "Result is 142857 and took 11.188 ms",
|
||||
"53": "Result is -- and took --",
|
||||
"54": "Result is 0 and took 1.895 ns",
|
||||
"55": "Result is 249 and took 70.237 ms",
|
||||
"56": "Result is 972 and took 16.350 ms",
|
||||
"57": "Result is 153 and took 5.855 ms",
|
||||
"58": "Result is 26241 and took 6.752 s",
|
||||
"59": "Result is -- and took --",
|
||||
"60": "Result is nothing and took 1.895 ns",
|
||||
"63": "Result is 49 and took 84.347 μs"
|
||||
}
|
||||
@@ -4,13 +4,13 @@ Created on 08 Jun 2021
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 1 of Project Euler
|
||||
Solution for Problem 001 of Project Euler
|
||||
https://projecteuler.net/problem=1
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
function Problem1()
|
||||
function Problem001()
|
||||
#=
|
||||
If we list all the natural numbers below 10 that are multiples of 3 or 5,
|
||||
we get 3, 5, 6 and 9. The sum of these multiples is 23.
|
||||
@@ -24,7 +24,7 @@ function Problem1()
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(1, 3, "0")):")
|
||||
@btime Problem1()
|
||||
println("Took:")
|
||||
@btime Problem001()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(1, 3, "0")): ", Problem1())
|
||||
println("Result for Problem $(lpad(1, 3, "0")): ", Problem001())
|
||||
|
||||
@@ -4,12 +4,13 @@ Created on 08 Jun 2021
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 2 of Project Euler
|
||||
https://projecteuler.net/problem=2 =#
|
||||
Solution for Problem 002 of Project Euler
|
||||
https://projecteuler.net/problem=2
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
function Problem2()
|
||||
function Problem002()
|
||||
#=
|
||||
Each new term in the Fibonacci sequence is generated by adding the
|
||||
previous two terms. By starting with 1 and 2, the first 10 terms will be:
|
||||
@@ -17,7 +18,8 @@ function Problem2()
|
||||
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
|
||||
|
||||
Find the sum of all the even-valued terms in the sequence which do not
|
||||
exceed four million. =#
|
||||
exceed four million.
|
||||
=#
|
||||
|
||||
ans = 0
|
||||
limit = 4_000_000
|
||||
@@ -35,7 +37,7 @@ function Problem2()
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(2, 3, "0")):")
|
||||
@btime Problem2()
|
||||
println("Took: ")
|
||||
@btime Problem002()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(2, 3, "0")): ", Problem2())
|
||||
println("Result for Problem $(lpad(2, 3, "0")): ", Problem002())
|
||||
|
||||
@@ -4,16 +4,18 @@ Created on 15 Jun 2021
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 3 of Project Euler
|
||||
https://projecteuler.net/problem=3 =#
|
||||
Solution for Problem 003 of Project Euler
|
||||
https://projecteuler.net/problem=3
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
function Problem3()
|
||||
function Problem003()
|
||||
#=
|
||||
The prime factors of 13195 are 5, 7, 13 and 29.
|
||||
|
||||
What is the largest prime factor of the number 600851475143 ? =#
|
||||
What is the largest prime factor of the number 600851475143 ?
|
||||
=#
|
||||
|
||||
ans = 600_851_475_143
|
||||
factor = 2
|
||||
@@ -28,7 +30,7 @@ function Problem3()
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(3, 3, "0")):")
|
||||
@btime Problem3()
|
||||
println("Took:")
|
||||
@btime Problem003()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(3, 3, "0")): ", Problem3())
|
||||
println("Result for Problem $(lpad(3, 3, "0")): ", Problem003())
|
||||
|
||||
@@ -4,8 +4,9 @@ Created on 17 Jun 2021
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 4 of Project Euler
|
||||
https://projecteuler.net/problem=4 =#
|
||||
Solution for Problem 004 of Project Euler
|
||||
https://projecteuler.net/problem=4
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
@@ -20,12 +21,13 @@ function _ispalindrome(x::Integer, divider)
|
||||
end
|
||||
|
||||
|
||||
function Problem4()
|
||||
function Problem004()
|
||||
#=
|
||||
A palindromic number reads the same both ways. The largest palindrome made
|
||||
from the product of two 2-digit numbers is 9009 = 91 x 99.
|
||||
|
||||
Find the largest palindrome made from the product of two 3-digit numbers. =#
|
||||
Find the largest palindrome made from the product of two 3-digit numbers.
|
||||
=#
|
||||
|
||||
ans = 10_001
|
||||
for i = 100:999, j = i:999
|
||||
@@ -39,7 +41,7 @@ function Problem4()
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(4, 3, "0")):")
|
||||
@btime Problem4()
|
||||
println("Took:")
|
||||
@btime Problem004()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(4, 3, "0")): ", Problem4())
|
||||
println("Result for Problem $(lpad(4, 3, "0")): ", Problem004())
|
||||
|
||||
@@ -4,8 +4,9 @@ Created on 20 Jun 2021
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 5 of Project Euler
|
||||
https://projecteuler.net/problem=5 =#
|
||||
Solution for Problem 005 of Project Euler
|
||||
https://projecteuler.net/problem=5
|
||||
=#
|
||||
|
||||
#=
|
||||
The LCM of two natural numbers x and y is given by:
|
||||
@@ -13,17 +14,20 @@ def lcm(x, y):
|
||||
return x * y // math.gcd(x, y)
|
||||
|
||||
It is possible to compute the LCM of more than two numbers by iteratively
|
||||
computing the LCM of two numbers, i.e. LCM(a, b, c) = LCM(a, LCM(b, c)) =#
|
||||
computing the LCM of two numbers, i.e. LCM(a, b, c) = LCM(a, LCM(b, c))
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
function Problem5()
|
||||
function Problem005()
|
||||
#=
|
||||
2520 is the smallest number that can be divided by each of the numbers
|
||||
from 1 to 10 without any remainder.
|
||||
|
||||
What is the smallest positive number that is evenly divisible by all of
|
||||
the numbers from 1 to 20? =#
|
||||
the numbers from 1 to 20?
|
||||
=#
|
||||
|
||||
ans = 1
|
||||
for i = 1:21
|
||||
ans *= i ÷ gcd(i, ans)
|
||||
@@ -33,7 +37,7 @@ function Problem5()
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(5, 3, "0")):")
|
||||
@btime Problem5()
|
||||
println("Took:")
|
||||
@btime Problem005()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(5, 3, "0")): ", Problem5())
|
||||
println("Result for Problem $(lpad(5, 3, "0")): ", Problem005())
|
||||
|
||||
@@ -4,12 +4,13 @@ Created on 20 Jun 2021
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 6 of Project Euler
|
||||
https://projecteuler.net/problem=6 =#
|
||||
Solution for Problem 006 of Project Euler
|
||||
https://projecteuler.net/problem=6
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
function Problem6()
|
||||
function Problem006()
|
||||
#=
|
||||
The sum of the squares of the first ten natural numbers is,
|
||||
1^2 + 2^2 + ... + 10^2 = 385
|
||||
@@ -21,7 +22,8 @@ function Problem6()
|
||||
natural numbers and the square of the sum is 3025 − 385 = 2640.
|
||||
|
||||
Find the difference between the sum of the squares of the first one
|
||||
hundred natural numbers and the square of the sum. Statement =#
|
||||
hundred natural numbers and the square of the sum.
|
||||
=#
|
||||
|
||||
n = 100
|
||||
square_of_sum = sum(i for i = 1:n)^2
|
||||
@@ -32,7 +34,7 @@ function Problem6()
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(6, 3, "0")):")
|
||||
@btime Problem6()
|
||||
println("Took:")
|
||||
@btime Problem006()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(6, 3, "0")): ", Problem6())
|
||||
println("Result for Problem $(lpad(6, 3, "0")): ", Problem006())
|
||||
|
||||
@@ -4,20 +4,22 @@ Created on 24 Jun 2021
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 7 of Project Euler
|
||||
https://projecteuler.net/problem=7 =#
|
||||
Solution for Problem 007 of Project Euler
|
||||
https://projecteuler.net/problem=7
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
using Primes
|
||||
using Transducers
|
||||
|
||||
|
||||
function Problem7()
|
||||
function Problem007()
|
||||
#=
|
||||
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13,
|
||||
we can see that the 6th prime is 13.
|
||||
|
||||
What is the 10_001st prime number =#
|
||||
What is the 10_001st prime number
|
||||
=#
|
||||
|
||||
count::Int32 = 10_000
|
||||
# 2 is prime but not included to speed-up
|
||||
@@ -36,7 +38,7 @@ function Problem7()
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(7, 3, "0")):")
|
||||
@btime Problem7()
|
||||
println("Took:")
|
||||
@btime Problem007()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(7, 3, "0")): ", Problem7())
|
||||
println("Result for Problem $(lpad(7, 3, "0")): ", Problem007())
|
||||
|
||||
@@ -4,12 +4,13 @@ Created on 29 Jun 2021
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 8 of Project Euler
|
||||
https://projecteuler.net/problem=8 =#
|
||||
Solution for Problem 008 of Project Euler
|
||||
https://projecteuler.net/problem=8
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
function Problem8()
|
||||
function Problem008()
|
||||
#=
|
||||
The four adjacent digits in the 1000-digit number that have the
|
||||
greatest product are 9 × 9 × 8 × 9 = 5832.
|
||||
@@ -17,7 +18,8 @@ function Problem8()
|
||||
731671...963450
|
||||
|
||||
Find the thirteen adjacent digits in the 1000-digit number that have
|
||||
the greatest product. What is the value of this product? =#
|
||||
the greatest product. What is the value of this product?
|
||||
=#
|
||||
|
||||
NUM::String = """
|
||||
73167176531330624919225119674426574742355349194934
|
||||
@@ -62,7 +64,7 @@ function Problem8()
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(8, 3, "0")):")
|
||||
@btime Problem8()
|
||||
println("Took:")
|
||||
@btime Problem008()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(8, 3, "0")): ", Problem8())
|
||||
println("Result for Problem $(lpad(8, 3, "0")): ", Problem008())
|
||||
|
||||
@@ -4,12 +4,13 @@ Created on 01 Jul 2021
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 9 of Project Euler
|
||||
https://projecteuler.net/problem=9 =#
|
||||
Solution for Problem 009 of Project Euler
|
||||
https://projecteuler.net/problem=9
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
function Problem9()
|
||||
function Problem009()
|
||||
#=
|
||||
A Pythagorean triplet is a set of three natural numbers, a < b < c,
|
||||
for which a^2 + b^2 = c^2
|
||||
@@ -17,7 +18,8 @@ function Problem9()
|
||||
For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
|
||||
|
||||
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
|
||||
Find the product abc. =#
|
||||
Find the product abc.
|
||||
=#
|
||||
|
||||
upper_limit = 1000
|
||||
for a = 1:upper_limit+1
|
||||
@@ -32,7 +34,7 @@ function Problem9()
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(9, 3, "0")):")
|
||||
@btime Problem9()
|
||||
println("Took:")
|
||||
@btime Problem009()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(9, 3, "0")): ", Problem9())
|
||||
println("Result for Problem $(lpad(9, 3, "0")): ", Problem009())
|
||||
|
||||
@@ -4,23 +4,25 @@ Created on 03 Jul 2021
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 10 of Project Euler
|
||||
https://projecteuler.net/problem=10 =#
|
||||
Solution for Problem 010 of Project Euler
|
||||
https://projecteuler.net/problem=10
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
using Primes
|
||||
|
||||
function Problem10()
|
||||
function Problem010()
|
||||
#=
|
||||
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
|
||||
|
||||
Find the sum of all the primes below two million. =#
|
||||
Find the sum of all the primes below two million.
|
||||
=#
|
||||
|
||||
return sum(primes(1_999_999))
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(10, 3, "0")):")
|
||||
@btime Problem10()
|
||||
println("Took:")
|
||||
@btime Problem010()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(10, 3, "0")): ", Problem10())
|
||||
println("Result for Problem $(lpad(10, 3, "0")): ", Problem010())
|
||||
|
||||
@@ -4,8 +4,9 @@ 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 =#
|
||||
Solution for Problem 012 of Project Euler
|
||||
https://projecteuler.net/problem=12
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
@@ -16,7 +17,7 @@ function num_divisors(number::Int64)
|
||||
end
|
||||
|
||||
|
||||
function Problem12()
|
||||
function Problem012()
|
||||
#=
|
||||
The sequence of triangle numbers is generated by adding the natural
|
||||
numbers. So the 7th triangle number would be:
|
||||
@@ -38,9 +39,10 @@ function Problem12()
|
||||
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? =#
|
||||
divisors?
|
||||
=#
|
||||
|
||||
ans::Int64 = 0
|
||||
ans = 0
|
||||
for number in Iterators.countfrom(1)
|
||||
ans += number
|
||||
if num_divisors(ans) > 500
|
||||
@@ -50,7 +52,7 @@ function Problem12()
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(12, 3, "0")):")
|
||||
@btime Problem12()
|
||||
println("Took:")
|
||||
@btime Problem012()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(12, 3, "0")): ", Problem12())
|
||||
println("Result for Problem $(lpad(12, 3, "0")): ", Problem012())
|
||||
|
||||
@@ -4,8 +4,9 @@ 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 =#
|
||||
Solution for Problem 014 of Project Euler
|
||||
https://projecteuler.net/problem=14
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
@@ -18,7 +19,7 @@ function chain_length(n)
|
||||
return length
|
||||
end
|
||||
|
||||
function Problem14()
|
||||
function Problem014()
|
||||
#=
|
||||
The following iterative sequence is defined for the set of positive
|
||||
integers:
|
||||
@@ -37,7 +38,8 @@ function Problem14()
|
||||
|
||||
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
|
||||
limit = 1_000_000
|
||||
@@ -53,7 +55,7 @@ function Problem14()
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(14, 3, "0")):")
|
||||
@btime Problem14()
|
||||
println("Took:")
|
||||
@btime Problem014()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(14, 3, "0")): ", Problem14())
|
||||
println("Result for Problem $(lpad(14, 3, "0")): ", Problem014())
|
||||
|
||||
@@ -6,26 +6,26 @@ Created on 25 Jul 2021
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 15 of Project Euler
|
||||
https://projecteuler.net/problem=15 =#
|
||||
https://projecteuler.net/problem=15
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
function Problem15()
|
||||
function Problem015()
|
||||
#=
|
||||
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::Int8 = 20
|
||||
numerator::BigInt = factorial(big(2n))
|
||||
denominator1::BigInt = big(factorial(n))
|
||||
denominator2::BigInt = big(factorial(2n - n))
|
||||
return BigInt(numerator / (denominator1 * denominator2))
|
||||
How many such routes are there through a 20×20 grid?
|
||||
=#
|
||||
|
||||
n = 20
|
||||
return BigInt(factorial(big(2n)) / (factorial(n) * big(factorial(2n-n))))
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(15, 3, "0")):")
|
||||
@btime Problem15()
|
||||
println("Took:")
|
||||
@btime Problem015()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(15, 3, "0")): ", Problem15())
|
||||
println("Result for Problem $(lpad(15, 3, "0")): ", Problem015())
|
||||
|
||||
@@ -5,24 +5,26 @@ 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 =#
|
||||
Solution for Problem 016 of Project Euler
|
||||
https://projecteuler.net/problem=16
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
function Problem16()
|
||||
function Problem016()
|
||||
#=
|
||||
2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
|
||||
|
||||
What is the sum of the digits of the number 2^1000? =#
|
||||
What is the sum of the digits of the number 2^1000?
|
||||
=#
|
||||
|
||||
n::Int16 = 1_000
|
||||
n = 1_000
|
||||
|
||||
return sum(digits(2^BigInt(n)))
|
||||
return sum(digits(2^big(n)))
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(16, 3, "0")):")
|
||||
@btime Problem16()
|
||||
println("Took:")
|
||||
@btime Problem016()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(16, 3, "0")): ", Problem16())
|
||||
println("Result for Problem $(lpad(16, 3, "0")): ", Problem016())
|
||||
|
||||
@@ -5,7 +5,8 @@ Created on 28 Jul 2021
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 17 of Project Euler
|
||||
https://projecteuler.net/problem=17 =#
|
||||
https://projecteuler.net/problem=17
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
@@ -33,7 +34,7 @@ function num2letters(num, nums)
|
||||
end
|
||||
end
|
||||
|
||||
function Problem17()
|
||||
function Problem017()
|
||||
#=
|
||||
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.
|
||||
@@ -44,7 +45,8 @@ function Problem17()
|
||||
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. =#
|
||||
with British usage.
|
||||
=#
|
||||
|
||||
nums = Dict(
|
||||
0 => 0,
|
||||
@@ -83,12 +85,11 @@ function Problem17()
|
||||
for num = 1:n
|
||||
letters += num2letters(num, nums)
|
||||
end
|
||||
|
||||
return letters
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(17, 3, "0")):")
|
||||
@btime Problem17()
|
||||
println("Took:")
|
||||
@btime Problem017()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(17, 3, "0")): ", Problem17())
|
||||
println("Result for Problem $(lpad(17, 3, "0")): ", Problem017())
|
||||
|
||||
@@ -4,12 +4,13 @@ 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 =#
|
||||
Solution for Problem 018 of Project Euler
|
||||
https://projecteuler.net/problem=18
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
function Problem18()
|
||||
function Problem018()
|
||||
#=
|
||||
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.
|
||||
@@ -21,7 +22,8 @@ function Problem18()
|
||||
|
||||
That is, 3 + 7 + 4 + 9 = 23.
|
||||
|
||||
Find the maximum total from top to bottom of the triangle above =#
|
||||
Find the maximum total from top to bottom of the triangle above
|
||||
=#
|
||||
|
||||
triangle = [ # Mutable
|
||||
[75],
|
||||
@@ -52,7 +54,7 @@ function Problem18()
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(18, 3, "0")):")
|
||||
@btime Problem18()
|
||||
println("Took:")
|
||||
@btime Problem018()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(18, 3, "0")): ", Problem18())
|
||||
println("Result for Problem $(lpad(18, 3, "0")): ", Problem018())
|
||||
|
||||
@@ -4,13 +4,14 @@ 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 =#
|
||||
Solution for Problem 019 of Project Euler
|
||||
https://projecteuler.net/problem=19
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
using Dates
|
||||
|
||||
function Problem19()
|
||||
function Problem019()
|
||||
#=
|
||||
You are given the following information, but you may prefer to do some
|
||||
research for yourself.
|
||||
@@ -25,22 +26,23 @@ function Problem19()
|
||||
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)? =#
|
||||
century (1 Jan 1901 to 31 Dec 2000)?
|
||||
=#
|
||||
|
||||
sundays = 0
|
||||
ans = 0
|
||||
for year = 1901:2000
|
||||
for month = 1:12
|
||||
if Dates.dayofweek(Date(year, month, 1)) == Dates.Sunday
|
||||
sundays += 1
|
||||
ans += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return sundays
|
||||
return ans
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(19, 3, "0")):")
|
||||
@btime Problem19()
|
||||
println("Took:")
|
||||
@btime Problem019()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(19, 3, "0")): ", Problem19())
|
||||
println("Result for Problem $(lpad(19, 3, "0")): ", Problem019())
|
||||
|
||||
@@ -4,12 +4,13 @@ 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 =#
|
||||
Solution for Problem 020 of Project Euler
|
||||
https://projecteuler.net/problem=20
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
function Problem20()
|
||||
function Problem020()
|
||||
#=
|
||||
n! means n × (n − 1) × ... × 3 × 2 × 1
|
||||
|
||||
@@ -17,15 +18,14 @@ function Problem20()
|
||||
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! =#
|
||||
Find the sum of the digits in the number 100!
|
||||
=#
|
||||
|
||||
fact::BigInt = factorial(big(100))
|
||||
|
||||
return sum(parse(Int8, d) for d::Char = string(fact))
|
||||
return sum(digits(factorial(big(100))))
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem $(lpad(20, 3, "0")):")
|
||||
@btime Problem20()
|
||||
println("Took:")
|
||||
@btime Problem020()
|
||||
println("")
|
||||
println("Result for Problem $(lpad(20, 3, "0")): ", Problem20())
|
||||
println("Result for Problem $(lpad(20, 3, "0")): ", Problem020())
|
||||
|
||||
64
src/Python.json
Normal file
64
src/Python.json
Normal file
@@ -0,0 +1,64 @@
|
||||
{
|
||||
"1": "Result is 233168 and took 0.165 ms",
|
||||
"2": "Result is 4613732 and took 0.004 ms",
|
||||
"3": "Result is 6857 and took 0.314 ms",
|
||||
"4": "Result is 906609 and took 319.644 ms",
|
||||
"5": "Result is 232792560 and took 0.012 ms",
|
||||
"6": "Result is 25164150 and took 0.072 ms",
|
||||
"7": "Result is 104743 and took 304.030 ms",
|
||||
"8": "Result is 23514624000 and took 4.132 ms",
|
||||
"9": "Result is 31875000 and took 58.148 ms",
|
||||
"10": "Result is 142913828922 and took 409.924 ms",
|
||||
"11": "Result is 70600674 and took 2.082 ms",
|
||||
"12": "Result is 76576500 and took 4.735 s",
|
||||
"13": "Result is 5537376230 and took 25.673 ms",
|
||||
"14": "Result is 837799 and took 2.604 s",
|
||||
"15": "Result is 137846528820 and took 0.016 ms",
|
||||
"16": "Result is 1366 and took 0.083 ms",
|
||||
"17": "Result is 21124 and took 13.177 ms",
|
||||
"18": "Result is 1074 and took 0.110 ms",
|
||||
"19": "Result is 171 and took 0.485 ms",
|
||||
"20": "Result is 648 and took 0.069 ms",
|
||||
"21": "Result is 31626 and took 3.499 s",
|
||||
"22": "Result is 871198282 and took 38.466 ms",
|
||||
"23": "Result is 4179871 and took 4.207 s",
|
||||
"24": "Result is 2783915460 and took 1.015 s",
|
||||
"25": "Result is 4782 and took 62.040 ms",
|
||||
"26": "Result is 983 and took 99.642 ms",
|
||||
"27": "Result is -59231 and took 3.559 s",
|
||||
"28": "Result is 669171001 and took 0.393 ms",
|
||||
"29": "Result is 9183 and took 9.184 ms",
|
||||
"30": "Result is 443839 and took 3.662 s",
|
||||
"31": "Result is 73682 and took 3.939 s",
|
||||
"32": "Result is 45228 and took 1.529 s",
|
||||
"33": "Result is 100 and took 2.598 ms",
|
||||
"34": "Result is 40730 and took 5.812 s",
|
||||
"35": "Result is 55 and took 11.863 s",
|
||||
"36": "Result is 872187 and took 307.045 ms",
|
||||
"37": "Result is 748317 and took 792.588 ms",
|
||||
"38": "Result is 932718654 and took 41.107 ms",
|
||||
"39": "Result is 840 and took 24.633 s",
|
||||
"40": "Result is 210 and took 246.492 ms",
|
||||
"41": "Result is 7652413 and took 7.185 ms",
|
||||
"42": "Result is 162 and took 26.712 ms",
|
||||
"43": "Result is 16695334890 and took 6.942 s",
|
||||
"44": "Result is 5482660 and took 331.891 ms",
|
||||
"45": "Result is 1533776805 and took 93.169 ms",
|
||||
"46": "Result is 5777 and took 220.877 ms",
|
||||
"47": "Result is 134043 and took 1.692 s",
|
||||
"48": "Result is 9110846700 and took 11.318 ms",
|
||||
"49": "Result is 296962699629 and took 3.641 ms",
|
||||
"50": "Result is 997651 and took 25.066 s",
|
||||
"51": "Result is 121313 and took 602.599 ms",
|
||||
"52": "Result is 142857 and took 32.730 ms",
|
||||
"53": "Result is 4075 and took 12.028 ms",
|
||||
"54": "Result is 376 and took 104.144 ms",
|
||||
"55": "Result is 249 and took 54.781 ms",
|
||||
"56": "Result is 972 and took 163.642 ms",
|
||||
"57": "Result is 153 and took 15.832 ms",
|
||||
"58": "Result is 26241 and took 13.617 s",
|
||||
"59": "Result is 129448 and took 1.319 s",
|
||||
"60": "Result is 26033 and took 61.160 s",
|
||||
"61": "Result is 28684 and took 42.680 ms",
|
||||
"62": "Result is 127035954683 and took 27.491 ms"
|
||||
}
|
||||
@@ -1,28 +1,25 @@
|
||||
import os
|
||||
import functools
|
||||
import json
|
||||
from pathlib import Path
|
||||
from subprocess import run
|
||||
|
||||
import pytablewriter as ptw
|
||||
from pytablereader import MarkdownTableFileLoader
|
||||
from pytablewriter import MarkdownTableWriter
|
||||
from pytablewriter.style import Style
|
||||
|
||||
|
||||
def read_md():
|
||||
"""
|
||||
Read README.md
|
||||
"""
|
||||
with open("../README.md", "r") as f:
|
||||
md = [line.strip() for line in f.readlines() if line.strip() != ""][1:]
|
||||
return md
|
||||
def read_md(f):
|
||||
loader = MarkdownTableFileLoader(f)
|
||||
for table_data in loader.load():
|
||||
return ptw.dumps_tabledata(table_data)
|
||||
|
||||
|
||||
def write_md(results):
|
||||
"""
|
||||
Write to README.md
|
||||
"""
|
||||
initial_message = (
|
||||
f"Having a bit of fun trying to solve the problems from "
|
||||
f"https://projecteuler.net/archives using different programming "
|
||||
f"languages for learning purposes.\n\n"
|
||||
"Having a bit of fun trying to solve the problems from "
|
||||
"https://projecteuler.net/archives using different programming "
|
||||
"languages for learning purposes.\n\n"
|
||||
)
|
||||
with open("../README.md", "w") as f:
|
||||
f.write(initial_message)
|
||||
@@ -30,71 +27,101 @@ def write_md(results):
|
||||
results.write_table()
|
||||
|
||||
|
||||
def _file_stem(f):
|
||||
return f[-5:]
|
||||
def cache_and_save_results(func):
|
||||
@functools.wraps(func)
|
||||
@functools.lru_cache(maxsize=None)
|
||||
def wrapper(script_number):
|
||||
cache_file_name = f"{func.__name__.split('_')[1]}.json"
|
||||
num = int(script_number[-6:-3])
|
||||
|
||||
if Path(cache_file_name).exists():
|
||||
with open(cache_file_name, "r") as f:
|
||||
cache = json.load(f)
|
||||
else:
|
||||
cache = {}
|
||||
|
||||
if str(num) not in cache:
|
||||
try:
|
||||
tim, res = func(script_number)
|
||||
result = f"Result is {res.split(': ')[1]} and took {tim.split(': ')[1]}"
|
||||
except IndexError:
|
||||
tim = "Took: --"
|
||||
res = "Result is: --"
|
||||
result = "Result is -- and took --"
|
||||
cache[str(num)] = result
|
||||
with open(cache_file_name, "w") as f:
|
||||
json.dump(cache, f, indent=2, ensure_ascii=False)
|
||||
else:
|
||||
result = cache[str(num)]
|
||||
tim = f"Took: {' '.join(result.split(' ')[-2:])}"
|
||||
res = f"Result is: {result.split(' ')[2]}"
|
||||
|
||||
return tim, res
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
def get_problems(path=None, ext=None):
|
||||
wd = os.getcwd()
|
||||
os.chdir(Path(path))
|
||||
def get_problems(path, ext=None):
|
||||
scripts = sorted(
|
||||
[script.as_posix() for script in Path(".").rglob(f"*[0-9].{ext}")],
|
||||
key=_file_stem,
|
||||
[script.as_posix() for script in path.rglob(f"*[0-9].{ext}")],
|
||||
key=lambda x: x[-5:],
|
||||
)
|
||||
os.chdir(wd)
|
||||
|
||||
return scripts
|
||||
|
||||
|
||||
def timing_Python(path=None, exec_only=1):
|
||||
wd = os.getcwd()
|
||||
scripts = get_problems(path=path, ext="py")
|
||||
os.chdir(Path(path))
|
||||
python_timings, python_results = [], []
|
||||
for script in scripts[:exec_only]:
|
||||
_res = run(
|
||||
["python3", f"{script}"], capture_output=True, text=True
|
||||
@cache_and_save_results
|
||||
def timing_Python(script_number):
|
||||
_run = run(
|
||||
["python3", f"{script_number}"], capture_output=True, text=True
|
||||
).stdout.split("\n")
|
||||
python_timings.append(_res[0])
|
||||
python_results.append(_res[2])
|
||||
|
||||
os.chdir(wd)
|
||||
return python_timings, python_results
|
||||
return _run[0], _run[2]
|
||||
|
||||
|
||||
def timing_Julia(path=None, exec_only=1):
|
||||
wd = os.getcwd()
|
||||
scripts = get_problems(path="Julia", ext="jl")
|
||||
os.chdir(Path(path))
|
||||
julia_problem_numbers, julia_timings, julia_results = [], [], []
|
||||
for script in scripts[:exec_only]:
|
||||
_res = run(["julia", f"{script}"], capture_output=True, text=True).stdout.split(
|
||||
"\n"
|
||||
)
|
||||
julia_problem_numbers.append(_res[0])
|
||||
julia_timings.append(_res[1].split("(")[0])
|
||||
julia_results.append(_res[3])
|
||||
@cache_and_save_results
|
||||
def timing_Julia(script_number):
|
||||
_run = run(
|
||||
["julia", f"{script_number}"], capture_output=True, text=True
|
||||
).stdout.split("\n")
|
||||
|
||||
os.chdir(wd)
|
||||
return julia_problem_numbers, julia_timings, julia_results
|
||||
tim = " ".join([_run[0], " ".join(_run[1].split(" ")[2:4])])
|
||||
|
||||
return tim, _run[3]
|
||||
|
||||
|
||||
def execute(combine_columns=True, nproblems=-1):
|
||||
def execute(combine_columns=True, nproblems=None):
|
||||
"""
|
||||
Execute scripts and return md formatted table
|
||||
"""
|
||||
python_timings, python_results = timing_Python(path="Python", exec_only=nproblems)
|
||||
julia_n_problem, julia_timings, julia_results = timing_Julia(
|
||||
path="Julia", exec_only=nproblems
|
||||
)
|
||||
python_path = Path.cwd() / "Python"
|
||||
pscripts = get_problems(python_path, ext="py")
|
||||
|
||||
assert len(python_timings) == len(julia_timings)
|
||||
julia_path = Path.cwd() / "Julia"
|
||||
jscripts = get_problems(julia_path, ext="jl")
|
||||
|
||||
ptimings, presults = [], []
|
||||
jtimings, jresults = [], []
|
||||
for problem, _ in enumerate(pscripts[:nproblems]):
|
||||
try:
|
||||
ptim, pres = timing_Python(pscripts[problem])
|
||||
ptimings.append(ptim)
|
||||
presults.append(pres)
|
||||
except KeyError:
|
||||
ptimings.append("Took: --")
|
||||
presults.append(f"Result for problem {problem:0>3}: --")
|
||||
try:
|
||||
jtim, jres = timing_Julia(jscripts[problem])
|
||||
jtimings.append(jtim)
|
||||
jresults.append(jres)
|
||||
except IndexError:
|
||||
jtimings.append("Took: --")
|
||||
jresults.append(f"Result for problem {problem:0>3}: --")
|
||||
|
||||
not_equal = []
|
||||
for nproblem, (python_res, julia_res) in enumerate(
|
||||
zip(python_results, julia_results), start=1
|
||||
):
|
||||
if not int(python_res[22:]) == int(julia_res[23:]):
|
||||
not_equal.append([nproblem, int(python_res[22:]), int(julia_res[23:])])
|
||||
for npb, (pres, jres) in enumerate(zip(presults, jresults), start=1):
|
||||
if not pres.split(" ")[-1] == jres.split(" ")[-1]:
|
||||
not_equal.append([npb, pres.split(" ")[-1], jres.split(" ")[-1]])
|
||||
|
||||
if combine_columns:
|
||||
headers = [
|
||||
@@ -103,19 +130,25 @@ def execute(combine_columns=True, nproblems=-1):
|
||||
"T_exec (Python)",
|
||||
"T_exec (Julia)",
|
||||
]
|
||||
type_hints = ["str", "int", "str", "str"]
|
||||
value_matrix = [
|
||||
[f"{int(t_python[25:28]):03d}", int(r_python[22:]), t_python[29:], t_julia]
|
||||
for t_python, r_python, t_julia in zip(
|
||||
python_timings, python_results, julia_timings
|
||||
[
|
||||
f"{idx:0>3}",
|
||||
int(r_python.split(" ")[-1]),
|
||||
t_python.split(": ")[1],
|
||||
t_julia.split(": ")[1],
|
||||
]
|
||||
for idx, (r_python, t_python, t_julia) in enumerate(
|
||||
zip(presults, ptimings, jtimings), start=1
|
||||
)
|
||||
]
|
||||
discrepancies = [
|
||||
f"\nDiscrepancies in problem {npb}. "
|
||||
f"Python result is {py_r} and in Julia is {jl_r}."
|
||||
f" - Problem {npb}: Python result is {py_r} and in Julia is {jl_r}"
|
||||
for npb, py_r, jl_r in not_equal
|
||||
]
|
||||
writer_comb = MarkdownTableWriter(
|
||||
table_name="\n".join(discrepancies),
|
||||
table_name="\nDiscrepancies in:\n" + "\n".join(discrepancies) + "\n",
|
||||
type_hints=type_hints,
|
||||
headers=headers,
|
||||
value_matrix=value_matrix,
|
||||
column_styles=[
|
||||
@@ -136,22 +169,35 @@ def execute(combine_columns=True, nproblems=-1):
|
||||
for lang in languages:
|
||||
if lang == "Python":
|
||||
headers = ["Problem #", "Result", "T_exec (Python)"]
|
||||
type_hints = ["str", "int", "str"]
|
||||
value_matrix = [
|
||||
[f"{int(t_python[25:28]):03d}", int(r_python[22:]), t_python[29:]]
|
||||
for t_python, r_python in zip(python_timings, python_results)
|
||||
[
|
||||
f"{idx:0>3}",
|
||||
int(r_python.split(" ")[-1]),
|
||||
t_python.split(": ")[1],
|
||||
]
|
||||
for idx, (r_python, t_python) in enumerate(
|
||||
zip(presults, ptimings), start=1
|
||||
) # noqa: E501
|
||||
]
|
||||
elif lang == "Julia":
|
||||
headers = ["Problem #", "Result", "T_exec (Julia)"]
|
||||
type_hints = ["str", "int", "str"]
|
||||
value_matrix = [
|
||||
[f"{int(p_julia[-5:-1]):03d}", int(r_julia[23:]), t_julia]
|
||||
for p_julia, r_julia, t_julia in zip(
|
||||
julia_n_problem, julia_results, julia_timings
|
||||
)
|
||||
[
|
||||
f"{idx:0>3}",
|
||||
int(r_julia.split(" ")[-1]),
|
||||
t_julia.split(": ")[1],
|
||||
]
|
||||
for idx, (r_julia, t_julia) in enumerate(
|
||||
zip(jresults, jtimings), start=1
|
||||
) # noqa: E501
|
||||
]
|
||||
|
||||
writer = MarkdownTableWriter(
|
||||
table_name=f"Results for {lang}",
|
||||
headers=headers,
|
||||
type_hints=type_hints,
|
||||
value_matrix=value_matrix,
|
||||
column_styles=[
|
||||
Style(align="center"),
|
||||
@@ -165,8 +211,7 @@ def execute(combine_columns=True, nproblems=-1):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# md = read_md()
|
||||
# md = read_md("../README.md")
|
||||
# print(md)
|
||||
res = execute(combine_columns=True, nproblems=-1)
|
||||
# print(res)
|
||||
write_md(res)
|
||||
|
||||
Reference in New Issue
Block a user