diff --git a/README.rst b/README.rst index 551389c2..0f75a2b4 100644 --- a/README.rst +++ b/README.rst @@ -73,7 +73,7 @@ Olivia's Project Euler Solutions | | | | |CodeQL| |br| | | | | | |C#-lint| | +------------+--------------------------+--------+-------------------+ -| Java | Java 8+ | 7 | |Javai| |br| | +| Java | Java 8+ | 10 | |Javai| |br| | | | | | |Ja-Cov| |br| | | | | | |CodeQL| |br| | | | | | |Java-lint| | diff --git a/csharp/Euler/p0008.cs b/csharp/Euler/p0008.cs index 70dd3161..86bc13ef 100644 --- a/csharp/Euler/p0008.cs +++ b/csharp/Euler/p0008.cs @@ -39,7 +39,6 @@ namespace Euler { public class p0008 : IEuler { - private const int MAX = 500; public object Answer() { String str = String.Concat( diff --git a/csharp/Euler/p0011.cs b/csharp/Euler/p0011.cs index 34ad462d..aa56e276 100644 --- a/csharp/Euler/p0011.cs +++ b/csharp/Euler/p0011.cs @@ -1,21 +1,37 @@ /* -Project Euler Problem 17 +Project Euler Problem 11 -I feel like there is a better way to recurse this problem, but I could not -think of one at the time +This one was tedious but pretty easy. Problem: -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. +In the 20×20 grid below, four numbers along a diagonal line have been marked in red. -If all the numbers from 1 to 1000 (one thousand) inclusive were written out in -words, how many letters would be used? +08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 +49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 +81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 +52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 +22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 +24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 +32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 +67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 +24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 +21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 +78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 +16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 +86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 +19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 +04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 +88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 +04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 +20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 +20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 +01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 -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. +The product of these numbers is 26 × 63 × 78 × 14 = 1788696. + +What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in +the 20×20 grid? */ using System; @@ -54,26 +70,26 @@ public object Answer() } byte[][] grid = new byte[20][]{ - new byte[20]{ 8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8}, - new byte[20]{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0}, - new byte[20] { 81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65 }, - new byte[20] { 52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91 }, + new byte[20] { 8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8 }, + new byte[20] { 49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0 }, + new byte[20] { 81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65 }, + new byte[20] { 52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91 }, new byte[20] { 22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80 }, - new byte[20] { 24, 47, 32, 60, 99, 03, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50 }, + new byte[20] { 24, 47, 32, 60, 99, 03, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50 }, new byte[20] { 32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70 }, - new byte[20] { 67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21 }, - new byte[20] { 24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72 }, - new byte[20] { 21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95 }, - new byte[20] { 78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92 }, - new byte[20] { 16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57 }, - new byte[20] { 86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58 }, - new byte[20] { 19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40 }, - new byte[20] { 4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66 }, - new byte[20] { 88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69 }, - new byte[20] { 4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36 }, + new byte[20] { 67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21 }, + new byte[20] { 24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72 }, + new byte[20] { 21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95 }, + new byte[20] { 78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92 }, + new byte[20] { 16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57 }, + new byte[20] { 86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58 }, + new byte[20] { 19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40 }, + new byte[20] { 4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66 }, + new byte[20] { 88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69 }, + new byte[20] { 4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36 }, new byte[20] { 20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 04, 36, 16 }, - new byte[20] { 20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54 }, - new byte[20] { 1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48 } + new byte[20] { 20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54 }, + new byte[20] { 1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48 } }; } } diff --git a/docs/index.rst b/docs/index.rst index 29c0f743..94f7a6b8 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -83,17 +83,17 @@ Problems Solved +-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`7` |:c-d:`0007` | | | |:js-d:`0007`|:py-d:`0007`|:rs-d:`0007`| +-----------+------------+------------+------------+------------+------------+------------+------------+ -|:prob:`8` |:c-d:`0008` |:cp-d:`0008`|:cs-d:`0008`| |:js-d:`0008`|:py-d:`0008`|:rs-d:`0008`| +|:prob:`8` |:c-d:`0008` |:cp-d:`0008`|:cs-d:`0008`|:ja-d:`0008`|:js-d:`0008`|:py-d:`0008`|:rs-d:`0008`| +-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`9` |:c-d:`0009` |:cp-d:`0009`|:cs-d:`0009`|:ja-d:`0009`|:js-d:`0009`|:py-d:`0009`|:rs-d:`0009`| +-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`10` |:c-d:`0010` | | | |:js-d:`0010`|:py-d:`0010`|:rs-d:`0010`| +-----------+------------+------------+------------+------------+------------+------------+------------+ -|:prob:`11` |:c-d:`0011` |:cp-d:`0011`|:cs-d:`0011`| |:js-d:`0011`|:py-d:`0011`|:rs-d:`0011`| +|:prob:`11` |:c-d:`0011` |:cp-d:`0011`|:cs-d:`0011`|:ja-d:`0011`|:js-d:`0011`|:py-d:`0011`|:rs-d:`0011`| +-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`12` |:c-s:`0012` | | | | |:py-d:`0012`|:rs-d:`0012`| +-----------+------------+------------+------------+------------+------------+------------+------------+ -|:prob:`13` |:c-d:`0013` |:cp-d:`0013`|:cs-d:`0013`| |:js-d:`0013`|:py-d:`0013`|:rs-d:`0013`| +|:prob:`13` |:c-d:`0013` |:cp-d:`0013`|:cs-d:`0013`|:ja-d:`0013`|:js-d:`0013`|:py-d:`0013`|:rs-d:`0013`| +-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`14` |:c-d:`0014` |:cp-d:`0014`|:cs-d:`0014`| |:js-d:`0014`|:py-d:`0014`|:rs-d:`0014`| +-----------+------------+------------+------------+------------+------------+------------+------------+ diff --git a/docs/java/p0008.rst b/docs/java/p0008.rst new file mode 100644 index 00000000..796ca153 --- /dev/null +++ b/docs/java/p0008.rst @@ -0,0 +1,14 @@ +Java Implementation of Problem 8 +================================ + +View source code :source:`java/src/main/java/euler/p0008.java` + +.. java:type:: public class p0008 implements IEuler + + .. java:method:: Object answer() + + :return: The answer to Project Euler problem 8 + +.. literalinclude:: ../../java/src/main/java/euler/p0008.java + :language: java + :linenos: diff --git a/docs/java/p0011.rst b/docs/java/p0011.rst new file mode 100644 index 00000000..1f9900f6 --- /dev/null +++ b/docs/java/p0011.rst @@ -0,0 +1,14 @@ +Java Implementation of Problem 11 +================================= + +View source code :source:`java/src/main/java/euler/p0011.java` + +.. java:type:: public class p0011 implements IEuler + + .. java:method:: Object answer() + + :return: The answer to Project Euler problem 11 + +.. literalinclude:: ../../java/src/main/java/euler/p0011.java + :language: java + :linenos: diff --git a/docs/java/p0013.rst b/docs/java/p0013.rst new file mode 100644 index 00000000..d8892266 --- /dev/null +++ b/docs/java/p0013.rst @@ -0,0 +1,14 @@ +Java Implementation of Problem 13 +================================= + +View source code :source:`java/src/main/java/euler/p0013.java` + +.. java:type:: public class p0013 implements IEuler + + .. java:method:: Object answer() + + :return: The answer to Project Euler problem 13 + +.. literalinclude:: ../../java/src/main/java/euler/p0013.java + :language: java + :linenos: diff --git a/java/README.rst b/java/README.rst index a3771108..f7c50a9a 100644 --- a/java/README.rst +++ b/java/README.rst @@ -79,6 +79,9 @@ Problems Solved - ☒ `2 <./src/main/java/p0002.java>`__ - ☒ `4 <./src/main/java/p0004.java>`__ - ☒ `6 <./src/main/java/p0006.java>`__ +- ☒ `8 <./src/main/java/p0008.java>`__ - ☒ `9 <./src/main/java/p0009.java>`__ +- ☒ `11 <./src/main/java/p0011.java>`__ +- ☒ `13 <./src/main/java/p0013.java>`__ - ☒ `22 <./src/main/java/p0022.java>`__ - ☒ `836 <./src/main/java/p0836.java>`__ diff --git a/java/src/main/java/euler/lib/Utilities.java b/java/src/main/java/euler/lib/Utilities.java index 3402ebce..bd44fc7d 100644 --- a/java/src/main/java/euler/lib/Utilities.java +++ b/java/src/main/java/euler/lib/Utilities.java @@ -12,22 +12,18 @@ public class Utilities { private static Path getDataPath(String name) throws IOException { -try { - URI classUri = Utilities.class.getProtectionDomain().getCodeSource().getLocation().toURI(); - Path classPath = Paths.get(classUri); -// Path classPath = Paths.get(Utilities.class.getProtectionDomain().getCodeSource().getLocation().getPath()); - Path classDir = classPath.getParent(); - Path filePath = classDir.getParent().getParent().resolve("_data").resolve(name); - return filePath; -} -catch (URISyntaxException e) { -throw new IOException("Invalid syntax in class path"); -} + try { + URI classUri = Utilities.class.getProtectionDomain().getCodeSource().getLocation().toURI(); + Path classPath = Paths.get(classUri); + return classPath.getParent().getParent().getParent().resolve("_data").resolve(name); + } + catch (URISyntaxException e) { + throw new IOException("Invalid syntax in class path"); + } } public static byte[] getDataFileBytes(String name) throws IOException { - Path filePath = getDataPath(name); - return Files.readAllBytes(filePath); + return Files.readAllBytes(getDataPath(name)); } public static String getDataFileText(String name) throws IOException { @@ -41,7 +37,6 @@ public static Object getAnswer(long n) throws IOException { reader.readLine(); while ((line = reader.readLine()) != null) { String[] arr = line.split("\t"); - if (arr.length < 4) continue; long key = Long.parseLong(arr[0]); if (key != n) continue; diff --git a/java/src/main/java/euler/p0008.java b/java/src/main/java/euler/p0008.java new file mode 100644 index 00000000..18b677e2 --- /dev/null +++ b/java/src/main/java/euler/p0008.java @@ -0,0 +1,75 @@ +/* +Project Euler Problem 8 + +In Python I did this with an iterator, but that is more cumbersome here, +so I just did it with a loop that slices. + +Problem: + +The four adjacent digits in the 1000-digit number that have the greatest +product are 9 × 9 × 8 × 9 = 5832. + +73167176531330624919225119674426574742355349194934 +96983520312774506326239578318016984801869478851843 +85861560789112949495459501737958331952853208805511 +12540698747158523863050715693290963295227443043557 +66896648950445244523161731856403098711121722383113 +62229893423380308135336276614282806444486645238749 +30358907296290491560440772390713810515859307960866 +70172427121883998797908792274921901699720888093776 +65727333001053367881220235421809751254540594752243 +52584907711670556013604839586446706324415722155397 +53697817977846174064955149290862569321978468622482 +83972241375657056057490261407972968652414535100474 +82166370484403199890008895243450658541227588666881 +16427171479924442928230863465674813919123162824586 +17866458359124566529476545682848912883142607690042 +24219022671055626321111109370544217506941658960408 +07198403850962455444362981230987879927244284909188 +84580156166097919133875499200524063689912560717606 +05886116467109405077541002256983155200055935729725 +71636269561882670428252483600823257530420752963450 + +Find the thirteen adjacent digits in the 1000-digit number that have the +greatest product. What is the value of this product? +*/ +package euler; + +public class p0008 implements IEuler { + @Override + public Object answer() { + String str = ( + "73167176531330624919225119674426574742355349194934" + + "96983520312774506326239578318016984801869478851843" + + "85861560789112949495459501737958331952853208805511" + + "12540698747158523863050715693290963295227443043557" + + "66896648950445244523161731856403098711121722383113" + + "62229893423380308135336276614282806444486645238749" + + "30358907296290491560440772390713810515859307960866" + + "70172427121883998797908792274921901699720888093776" + + "65727333001053367881220235421809751254540594752243" + + "52584907711670556013604839586446706324415722155397" + + "53697817977846174064955149290862569321978468622482" + + "83972241375657056057490261407972968652414535100474" + + "82166370484403199890008895243450658541227588666881" + + "16427171479924442928230863465674813919123162824586" + + "17866458359124566529476545682848912883142607690042" + + "24219022671055626321111109370544217506941658960408" + + "07198403850962455444362981230987879927244284909188" + + "84580156166097919133875499200524063689912560717606" + + "05886116467109405077541002256983155200055935729725" + + "71636269561882670428252483600823257530420752963450" + ); + long answer = 0; + for (int i = 0; i < str.length() - 13; i++) { + String slice = str.substring(i, i + 13); + long prod = 1; + for (int j = 0; j < slice.length(); j++) + prod *= slice.charAt(j) - '0'; + + if (prod > answer) + answer = prod; + } + return answer; + } +} \ No newline at end of file diff --git a/java/src/main/java/euler/p0011.java b/java/src/main/java/euler/p0011.java new file mode 100644 index 00000000..306bf28b --- /dev/null +++ b/java/src/main/java/euler/p0011.java @@ -0,0 +1,87 @@ +/* +Project Euler Problem 11 + +This one was tedious but pretty easy. + +Problem: + +In the 20×20 grid below, four numbers along a diagonal line have been marked in red. + +08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 +49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 +81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 +52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 +22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 +24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 +32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 +67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 +24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 +21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 +78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 +16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 +86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 +19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 +04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 +88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 +04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 +20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 +20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 +01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 + +The product of these numbers is 26 × 63 × 78 × 14 = 1788696. + +What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in +the 20×20 grid? +*/ +package euler; + +public class p0011 implements IEuler { + @Override + public Object answer() { + int answer = 0, tmp; + byte i, j; + for (i = 0; i < 20; i++) { + for (j = 0; j < 17; j++) { + // horizontal section + tmp = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3]; + answer = Math.max(answer, tmp); + // vertical section + tmp = grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i]; + answer = Math.max(answer, tmp); + } + } + for (i = 0; i < 17; i++) { + for (j = 0; j < 17; j++) { + // right diagonal section + tmp = grid[i][j] * grid[i + 1][j + 1] * grid[i + 2][j + 2] * grid[i + 3][j + 3]; + answer = Math.max(answer, tmp); + // left diagonal section + tmp = grid[i][j + 3] * grid[i + 1][j + 2] * grid[i + 2][j + 1] * grid[i + 3][j]; + answer = Math.max(answer, tmp); + } + } + return answer; + } + byte[][] grid = new byte[][]{ + new byte[] { 8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8 }, + new byte[] { 49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0 }, + new byte[] { 81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65 }, + new byte[] { 52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91 }, + new byte[] { 22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80 }, + new byte[] { 24, 47, 32, 60, 99, 03, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50 }, + new byte[] { 32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70 }, + new byte[] { 67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21 }, + new byte[] { 24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72 }, + new byte[] { 21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95 }, + new byte[] { 78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92 }, + new byte[] { 16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57 }, + new byte[] { 86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58 }, + new byte[] { 19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40 }, + new byte[] { 4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66 }, + new byte[] { 88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69 }, + new byte[] { 4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36 }, + new byte[] { 20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 04, 36, 16 }, + new byte[] { 20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54 }, + new byte[] { 1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48 } + }; +} \ No newline at end of file diff --git a/java/src/main/java/euler/p0013.java b/java/src/main/java/euler/p0013.java new file mode 100644 index 00000000..2219ad8e --- /dev/null +++ b/java/src/main/java/euler/p0013.java @@ -0,0 +1,235 @@ +/* +Project Euler Problem 13 + +Problem: + +Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. +37107287533902102798797998220837590246510135740250 +46376937677490009712648124896970078050417018260538 +74324986199524741059474233309513058123726617309629 +91942213363574161572522430563301811072406154908250 +23067588207539346171171980310421047513778063246676 +89261670696623633820136378418383684178734361726757 +28112879812849979408065481931592621691275889832738 +44274228917432520321923589422876796487670272189318 +47451445736001306439091167216856844588711603153276 +70386486105843025439939619828917593665686757934951 +62176457141856560629502157223196586755079324193331 +64906352462741904929101432445813822663347944758178 +92575867718337217661963751590579239728245598838407 +58203565325359399008402633568948830189458628227828 +80181199384826282014278194139940567587151170094390 +35398664372827112653829987240784473053190104293586 +86515506006295864861532075273371959191420517255829 +71693888707715466499115593487603532921714970056938 +54370070576826684624621495650076471787294438377604 +53282654108756828443191190634694037855217779295145 +36123272525000296071075082563815656710885258350721 +45876576172410976447339110607218265236877223636045 +17423706905851860660448207621209813287860733969412 +81142660418086830619328460811191061556940512689692 +51934325451728388641918047049293215058642563049483 +62467221648435076201727918039944693004732956340691 +15732444386908125794514089057706229429197107928209 +55037687525678773091862540744969844508330393682126 +18336384825330154686196124348767681297534375946515 +80386287592878490201521685554828717201219257766954 +78182833757993103614740356856449095527097864797581 +16726320100436897842553539920931837441497806860984 +48403098129077791799088218795327364475675590848030 +87086987551392711854517078544161852424320693150332 +59959406895756536782107074926966537676326235447210 +69793950679652694742597709739166693763042633987085 +41052684708299085211399427365734116182760315001271 +65378607361501080857009149939512557028198746004375 +35829035317434717326932123578154982629742552737307 +94953759765105305946966067683156574377167401875275 +88902802571733229619176668713819931811048770190271 +25267680276078003013678680992525463401061632866526 +36270218540497705585629946580636237993140746255962 +24074486908231174977792365466257246923322810917141 +91430288197103288597806669760892938638285025333403 +34413065578016127815921815005561868836468420090470 +23053081172816430487623791969842487255036638784583 +11487696932154902810424020138335124462181441773470 +63783299490636259666498587618221225225512486764533 +67720186971698544312419572409913959008952310058822 +95548255300263520781532296796249481641953868218774 +76085327132285723110424803456124867697064507995236 +37774242535411291684276865538926205024910326572967 +23701913275725675285653248258265463092207058596522 +29798860272258331913126375147341994889534765745501 +18495701454879288984856827726077713721403798879715 +38298203783031473527721580348144513491373226651381 +34829543829199918180278916522431027392251122869539 +40957953066405232632538044100059654939159879593635 +29746152185502371307642255121183693803580388584903 +41698116222072977186158236678424689157993532961922 +62467957194401269043877107275048102390895523597457 +23189706772547915061505504953922979530901129967519 +86188088225875314529584099251203829009407770775672 +11306739708304724483816533873502340845647058077308 +82959174767140363198008187129011875491310547126581 +97623331044818386269515456334926366572897563400500 +42846280183517070527831839425882145521227251250327 +55121603546981200581762165212827652751691296897789 +32238195734329339946437501907836945765883352399886 +75506164965184775180738168837861091527357929701337 +62177842752192623401942399639168044983993173312731 +32924185707147349566916674687634660915035914677504 +99518671430235219628894890102423325116913619626622 +73267460800591547471830798392868535206946944540724 +76841822524674417161514036427982273348055556214818 +97142617910342598647204516893989422179826088076852 +87783646182799346313767754307809363333018982642090 +10848802521674670883215120185883543223812876952786 +71329612474782464538636993009049310363619763878039 +62184073572399794223406235393808339651327408011116 +66627891981488087797941876876144230030984490851411 +60661826293682836764744779239180335110989069790714 +85786944089552990653640447425576083659976645795096 +66024396409905389607120198219976047599490197230297 +64913982680032973156037120041377903785566085089252 +16730939319872750275468906903707539413042652315011 +94809377245048795150954100921645863754710598436791 +78639167021187492431995700641917969777599028300699 +15368713711936614952811305876380278410754449733078 +40789923115535562561142322423255033685442488917353 +44889911501440648020369068063960672322193204149535 +41503128880339536053299340368006977710650566631954 +81234880673210146739058568557934581403627822703280 +82616570773948327592232845941706525094512325230608 +22918802058777319719839450180888072429661980811197 +77158542502016545090413245809786882778948721859617 +72107838435069186155435662884062257473692284509516 +20849603980134001723930671666823555245252804609722 +53503534226472524250874054075591789781264330331690 +*/ +package euler; + +public class p0013 implements IEuler { + @Override + public Object answer() { + long[][] numbers = new long[][]{ + new long[] { 37107287533902L, 102798797998220837L, 590246510135740250L }, + new long[] { 46376937677490L, 9712648124896970L, 78050417018260538L }, + new long[] { 74324986199524L, 741059474233309513L, 58123726617309629L }, + new long[] { 91942213363574L, 161572522430563301L, 811072406154908250L }, + new long[] { 23067588207539L, 346171171980310421L, 47513778063246676L }, + new long[] { 89261670696623L, 633820136378418383L, 684178734361726757L }, + new long[] { 28112879812849L, 979408065481931592L, 621691275889832738L }, + new long[] { 44274228917432L, 520321923589422876L, 796487670272189318L }, + new long[] { 47451445736001L, 306439091167216856L, 844588711603153276L }, + new long[] { 70386486105843L, 25439939619828917L, 593665686757934951L }, + new long[] { 62176457141856L, 560629502157223196L, 586755079324193331L }, + new long[] { 64906352462741L, 904929101432445813L, 822663347944758178L }, + new long[] { 92575867718337L, 217661963751590579L, 239728245598838407L }, + new long[] { 58203565325359L, 399008402633568948L, 830189458628227828L }, + new long[] { 80181199384826L, 282014278194139940L, 567587151170094390L }, + new long[] { 35398664372827L, 112653829987240784L, 473053190104293586L }, + new long[] { 86515506006295L, 864861532075273371L, 959191420517255829L }, + new long[] { 71693888707715L, 466499115593487603L, 532921714970056938L }, + new long[] { 54370070576826L, 684624621495650076L, 471787294438377604L }, + new long[] { 53282654108756L, 828443191190634694L, 37855217779295145L }, + new long[] { 36123272525000L, 296071075082563815L, 656710885258350721L }, + new long[] { 45876576172410L, 976447339110607218L, 265236877223636045L }, + new long[] { 17423706905851L, 860660448207621209L, 813287860733969412L }, + new long[] { 81142660418086L, 830619328460811191L, 61556940512689692L }, + new long[] { 51934325451728L, 388641918047049293L, 215058642563049483L }, + new long[] { 62467221648435L, 76201727918039944L, 693004732956340691L }, + new long[] { 15732444386908L, 125794514089057706L, 229429197107928209L }, + new long[] { 55037687525678L, 773091862540744969L, 844508330393682126L }, + new long[] { 18336384825330L, 154686196124348767L, 681297534375946515L }, + new long[] { 80386287592878L, 490201521685554828L, 717201219257766954L }, + new long[] { 78182833757993L, 103614740356856449L, 95527097864797581L }, + new long[] { 16726320100436L, 897842553539920931L, 837441497806860984L }, + new long[] { 48403098129077L, 791799088218795327L, 364475675590848030L }, + new long[] { 87086987551392L, 711854517078544161L, 852424320693150332L }, + new long[] { 59959406895756L, 536782107074926966L, 537676326235447210L }, + new long[] { 69793950679652L, 694742597709739166L, 693763042633987085L }, + new long[] { 41052684708299L, 85211399427365734L, 116182760315001271L }, + new long[] { 65378607361501L, 80857009149939512L, 557028198746004375L }, + new long[] { 35829035317434L, 717326932123578154L, 982629742552737307L }, + new long[] { 94953759765105L, 305946966067683156L, 574377167401875275L }, + new long[] { 88902802571733L, 229619176668713819L, 931811048770190271L }, + new long[] { 25267680276078L, 3013678680992525L, 463401061632866526L }, + new long[] { 36270218540497L, 705585629946580636L, 237993140746255962L }, + new long[] { 24074486908231L, 174977792365466257L, 246923322810917141L }, + new long[] { 91430288197103L, 288597806669760892L, 938638285025333403L }, + new long[] { 34413065578016L, 127815921815005561L, 868836468420090470L }, + new long[] { 23053081172816L, 430487623791969842L, 487255036638784583L }, + new long[] { 11487696932154L, 902810424020138335L, 124462181441773470L }, + new long[] { 63783299490636L, 259666498587618221L, 225225512486764533L }, + new long[] { 67720186971698L, 544312419572409913L, 959008952310058822L }, + new long[] { 95548255300263L, 520781532296796249L, 481641953868218774L }, + new long[] { 76085327132285L, 723110424803456124L, 867697064507995236L }, + new long[] { 37774242535411L, 291684276865538926L, 205024910326572967L }, + new long[] { 23701913275725L, 675285653248258265L, 463092207058596522L }, + new long[] { 29798860272258L, 331913126375147341L, 994889534765745501L }, + new long[] { 18495701454879L, 288984856827726077L, 713721403798879715L }, + new long[] { 38298203783031L, 473527721580348144L, 513491373226651381L }, + new long[] { 34829543829199L, 918180278916522431L, 27392251122869539L }, + new long[] { 40957953066405L, 232632538044100059L, 654939159879593635L }, + new long[] { 29746152185502L, 371307642255121183L, 693803580388584903L }, + new long[] { 41698116222072L, 977186158236678424L, 689157993532961922L }, + new long[] { 62467957194401L, 269043877107275048L, 102390895523597457L }, + new long[] { 23189706772547L, 915061505504953922L, 979530901129967519L }, + new long[] { 86188088225875L, 314529584099251203L, 829009407770775672L }, + new long[] { 11306739708304L, 724483816533873502L, 340845647058077308L }, + new long[] { 82959174767140L, 363198008187129011L, 875491310547126581L }, + new long[] { 97623331044818L, 386269515456334926L, 366572897563400500L }, + new long[] { 42846280183517L, 70527831839425882L, 145521227251250327L }, + new long[] { 55121603546981L, 200581762165212827L, 652751691296897789L }, + new long[] { 32238195734329L, 339946437501907836L, 945765883352399886L }, + new long[] { 75506164965184L, 775180738168837861L, 91527357929701337L }, + new long[] { 62177842752192L, 623401942399639168L, 44983993173312731L }, + new long[] { 32924185707147L, 349566916674687634L, 660915035914677504L }, + new long[] { 99518671430235L, 219628894890102423L, 325116913619626622L }, + new long[] { 73267460800591L, 547471830798392868L, 535206946944540724L }, + new long[] { 76841822524674L, 417161514036427982L, 273348055556214818L }, + new long[] { 97142617910342L, 598647204516893989L, 422179826088076852L }, + new long[] { 87783646182799L, 346313767754307809L, 363333018982642090L }, + new long[] { 10848802521674L, 670883215120185883L, 543223812876952786L }, + new long[] { 71329612474782L, 464538636993009049L, 310363619763878039L }, + new long[] { 62184073572399L, 794223406235393808L, 339651327408011116L }, + new long[] { 66627891981488L, 87797941876876144L, 230030984490851411L }, + new long[] { 60661826293682L, 836764744779239180L, 335110989069790714L }, + new long[] { 85786944089552L, 990653640447425576L, 83659976645795096L }, + new long[] { 66024396409905L, 389607120198219976L, 47599490197230297L }, + new long[] { 64913982680032L, 973156037120041377L, 903785566085089252L }, + new long[] { 16730939319872L, 750275468906903707L, 539413042652315011L }, + new long[] { 94809377245048L, 795150954100921645L, 863754710598436791L }, + new long[] { 78639167021187L, 492431995700641917L, 969777599028300699L }, + new long[] { 15368713711936L, 614952811305876380L, 278410754449733078L }, + new long[] { 40789923115535L, 562561142322423255L, 33685442488917353L }, + new long[] { 44889911501440L, 648020369068063960L, 672322193204149535L }, + new long[] { 41503128880339L, 536053299340368006L, 977710650566631954L }, + new long[] { 81234880673210L, 146739058568557934L, 581403627822703280L }, + new long[] { 82616570773948L, 327592232845941706L, 525094512325230608L }, + new long[] { 22918802058777L, 319719839450180888L, 72429661980811197L }, + new long[] { 77158542502016L, 545090413245809786L, 882778948721859617L }, + new long[] { 72107838435069L, 186155435662884062L, 257473692284509516L }, + new long[] { 20849603980134L, 1723930671666823L, 555245252804609722L }, + new long[] { 53503534226472L, 524250874054075591L, 789781264330331690L }, + }; + long ten18 = 1000000000000000000L; + long ten10 = 10000000000L; + long high = 0, med = 0, low = 0; + for (byte i = 0; i < 100; ++i) { + low += numbers[i][2]; + med += numbers[i][1]; + high += numbers[i][0]; + if (low > ten18) { + med += low / ten18; + low %= ten18; + } + if (med > ten18) { + high += med / ten18; + med %= ten18; + } + } + while (high > ten10) + high /= 10; + return high; + } +} \ No newline at end of file diff --git a/java/src/test/java/EulerTest.java b/java/src/test/java/EulerTest.java index 6627e3b3..9e32cd5c 100644 --- a/java/src/test/java/EulerTest.java +++ b/java/src/test/java/EulerTest.java @@ -20,10 +20,10 @@ static Stream data() throws IOException { new Object[]{p0002.class, false, Utilities.getAnswer(2)}, new Object[]{p0004.class, false, Utilities.getAnswer(4)}, new Object[]{p0006.class, false, Utilities.getAnswer(6)}, - // new Object[]{p0008.class, false, Utilities.getAnswer(8)}, + new Object[]{p0008.class, false, Utilities.getAnswer(8)}, new Object[]{p0009.class, false, Utilities.getAnswer(9)}, - // new Object[]{p0011.class, false, Utilities.getAnswer(11)}, - // new Object[]{p0013.class, false, Utilities.getAnswer(13)}, + new Object[]{p0011.class, false, Utilities.getAnswer(11)}, + new Object[]{p0013.class, false, Utilities.getAnswer(13)}, // new Object[]{p0014.class, false, Utilities.getAnswer(14)}, // new Object[]{p0015.class, false, Utilities.getAnswer(15)}, // new Object[]{p0016.class, false, Utilities.getAnswer(16)},