From 9fb0a1562acafb6fbdeb6245eaf71566846c1787 Mon Sep 17 00:00:00 2001 From: Olivia Appleton Date: Fri, 9 Aug 2024 18:52:44 -0500 Subject: [PATCH] Solve many problems in java (22 is expected failure) --- README.rst | 2 +- docs/csharp/utils.rst | 2 +- docs/index.rst | 14 +++---- docs/java/p0002.rst | 12 ++++++ docs/java/p0004.rst | 12 ++++++ docs/java/p0006.rst | 12 ++++++ docs/java/p0009.rst | 12 ++++++ docs/java/p0022.rst | 12 ++++++ docs/java/utils.rst | 16 ++++++++ java/README.rst | 8 +++- java/src/main/java/euler/lib/Utilities.java | 9 ++--- java/src/main/java/euler/p0000.java | 9 +++++ java/src/main/java/euler/p0001.java | 14 +++++++ java/src/main/java/euler/p0002.java | 36 ++++++++++++++++++ java/src/main/java/euler/p0004.java | 41 +++++++++++++++++++++ java/src/main/java/euler/p0006.java | 35 ++++++++++++++++++ java/src/main/java/euler/p0009.java | 33 +++++++++++++++++ java/src/main/java/euler/p0022.java | 41 +++++++++++++++++++++ java/src/main/java/euler/p0836.java | 16 ++++++++ java/src/test/java/EulerTest.java | 12 +++--- 20 files changed, 327 insertions(+), 21 deletions(-) create mode 100644 docs/java/p0002.rst create mode 100644 docs/java/p0004.rst create mode 100644 docs/java/p0006.rst create mode 100644 docs/java/p0009.rst create mode 100644 docs/java/p0022.rst create mode 100644 docs/java/utils.rst create mode 100644 java/src/main/java/euler/p0002.java create mode 100644 java/src/main/java/euler/p0004.java create mode 100644 java/src/main/java/euler/p0006.java create mode 100644 java/src/main/java/euler/p0009.java create mode 100644 java/src/main/java/euler/p0022.java diff --git a/README.rst b/README.rst index 35a207d5..080858bb 100644 --- a/README.rst +++ b/README.rst @@ -73,7 +73,7 @@ Olivia's Project Euler Solutions | | | | |CodeQL| |br| | | | | | |C#-lint| | +------------+--------------------------+--------+-------------------+ -| Java | Java 8+ | 2 | |Javai| |br| | +| Java | Java 8+ | 6 | |Javai| |br| | | | | | |Ja-Cov| |br| | | | | | |CodeQL| |br| | | | | | |Java-lint| | diff --git a/docs/csharp/utils.rst b/docs/csharp/utils.rst index eb2a402b..4cd68649 100644 --- a/docs/csharp/utils.rst +++ b/docs/csharp/utils.rst @@ -1,5 +1,5 @@ utils.cs -======= +======== View source code :source:`csharp/include/utils.cs` diff --git a/docs/index.rst b/docs/index.rst index a49e906d..187e8503 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -60,7 +60,7 @@ Problems Solved +-----------+------------+------------+------------+------------+------------+------------+------------+ | | |C| | |Cp| | |C#| | |Ja| | |Js| | |Py| | |Rs| | +===========+============+============+============+============+============+============+============+ -|Coverage | |d| | |d| | |d| | |ip| | |d| | |d| | |d| | +|Coverage | |d| | |d| | |d| | |d| | |d| | |d| | |d| | +-----------+------------+------------+------------+------------+------------+------------+------------+ |Docs | |d| | |d| | |d| | |ip| | |d| | |d| | |d| | +-----------+------------+------------+------------+------------+------------+------------+------------+ @@ -69,23 +69,23 @@ Problems Solved |Testing | |d| | |d| | |d| | |ip| | |d| | |d| | |d| | +-----------+------------+------------+------------+------------+------------+------------+------------+ +-----------+------------+------------+------------+------------+------------+------------+------------+ -|:prob:`1` |:c-d:`0001` |:cp-d:`0001`|:cs-d:`0001`|:ja-i:`0001`|:js-d:`0001`|:py-d:`0001`|:rs-d:`0001`| +|:prob:`1` |:c-d:`0001` |:cp-d:`0001`|:cs-d:`0001`|:ja-d:`0001`|:js-d:`0001`|:py-d:`0001`|:rs-d:`0001`| +-----------+------------+------------+------------+------------+------------+------------+------------+ -|:prob:`2` |:c-d:`0002` |:cp-d:`0002`|:cs-d:`0002`| |:js-d:`0002`|:py-d:`0002`|:rs-d:`0002`| +|:prob:`2` |:c-d:`0002` |:cp-d:`0002`|:cs-d:`0002`|:ja-d:`0002`|:js-d:`0002`|:py-d:`0002`|:rs-d:`0002`| +-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`3` |:c-d:`0003` | | | |:js-d:`0003`|:py-d:`0003`|:rs-d:`0003`| +-----------+------------+------------+------------+------------+------------+------------+------------+ -|:prob:`4` |:c-d:`0004` |:cp-d:`0004`|:cs-d:`0004`| |:js-d:`0004`|:py-d:`0004`|:rs-d:`0004`| +|:prob:`4` |:c-d:`0004` |:cp-d:`0004`|:cs-d:`0004`|:ja-d:`0004`|:js-d:`0004`|:py-d:`0004`|:rs-d:`0004`| +-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`5` |:c-d:`0005` | | | | |:py-d:`0005`|:rs-d:`0005`| +-----------+------------+------------+------------+------------+------------+------------+------------+ -|:prob:`6` |:c-d:`0006` |:cp-d:`0006`|:cs-d:`0006`| |:js-d:`0006`|:py-d:`0006`|:rs-d:`0006`| +|:prob:`6` |:c-d:`0006` |:cp-d:`0006`|:cs-d:`0006`|:ja-d:`0006`|:js-d:`0006`|:py-d:`0006`|:rs-d:`0006`| +-----------+------------+------------+------------+------------+------------+------------+------------+ |: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:`9` |:c-d:`0009` |:cp-d:`0009`|:cs-d:`0009`| |:js-d:`0009`|:py-d:`0009`|:rs-d:`0009`| +|: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`| +-----------+------------+------------+------------+------------+------------+------------+------------+ @@ -111,7 +111,7 @@ Problems Solved +-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`21` | | | | | |:py-d:`0021`| | +-----------+------------+------------+------------+------------+------------+------------+------------+ -|:prob:`22` |:c-d:`0022` |:cp-d:`0022`|:cs-d:`0022`| |:js-d:`0022`|:py-d:`0022`|:rs-d:`0022`| +|:prob:`22` |:c-d:`0022` |:cp-d:`0022`|:cs-d:`0022`|:ja-i:`0022`|:js-d:`0022`|:py-d:`0022`|:rs-d:`0022`| +-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`23` | | | | | |:py-d:`0023`| | +-----------+------------+------------+------------+------------+------------+------------+------------+ diff --git a/docs/java/p0002.rst b/docs/java/p0002.rst new file mode 100644 index 00000000..bd1d5e39 --- /dev/null +++ b/docs/java/p0002.rst @@ -0,0 +1,12 @@ +Java Implementation of Problem 2 +================================ + +View source code :source:`java/src/main/java/euler/p0002.java` + +.. java:class:: p0002 implements Euler.IEuler + + .. java:method:: Object answer() + +.. literalinclude:: ../../java/src/main/java/euler/p0002.java + :language: java + :linenos: diff --git a/docs/java/p0004.rst b/docs/java/p0004.rst new file mode 100644 index 00000000..b0630518 --- /dev/null +++ b/docs/java/p0004.rst @@ -0,0 +1,12 @@ +Java Implementation of Problem 4 +================================ + +View source code :source:`java/src/main/java/euler/p0004.java` + +.. java:class:: p0004 implements Euler.IEuler + + .. java:method:: Object answer() + +.. literalinclude:: ../../java/src/main/java/euler/p0004.java + :language: java + :linenos: diff --git a/docs/java/p0006.rst b/docs/java/p0006.rst new file mode 100644 index 00000000..554afef0 --- /dev/null +++ b/docs/java/p0006.rst @@ -0,0 +1,12 @@ +Java Implementation of Problem 6 +================================ + +View source code :source:`java/src/main/java/euler/p0006.java` + +.. java:class:: p0006 implements Euler.IEuler + + .. java:method:: Object answer() + +.. literalinclude:: ../../java/src/main/java/euler/p0006.java + :language: java + :linenos: diff --git a/docs/java/p0009.rst b/docs/java/p0009.rst new file mode 100644 index 00000000..f6e14b86 --- /dev/null +++ b/docs/java/p0009.rst @@ -0,0 +1,12 @@ +Java Implementation of Problem 9 +================================ + +View source code :source:`java/src/main/java/euler/p0009.java` + +.. java:class:: p0009 implements Euler.IEuler + + .. java:method:: Object answer() + +.. literalinclude:: ../../java/src/main/java/euler/p0009.java + :language: java + :linenos: diff --git a/docs/java/p0022.rst b/docs/java/p0022.rst new file mode 100644 index 00000000..3c610f9f --- /dev/null +++ b/docs/java/p0022.rst @@ -0,0 +1,12 @@ +Java Implementation of Problem 22 +================================= + +View source code :source:`java/src/main/java/euler/p0022.java` + +.. java:class:: p0022 implements Euler.IEuler + + .. java:method:: Object answer() + +.. literalinclude:: ../../java/src/main/java/euler/p0022.java + :language: java + :linenos: diff --git a/docs/java/utils.rst b/docs/java/utils.rst new file mode 100644 index 00000000..5dcf34e8 --- /dev/null +++ b/docs/java/utils.rst @@ -0,0 +1,16 @@ +Utilities.java +============== + +View source code :source:`java/src/main/java/euler/lib/Utilities.java` + +.. java:class:: Utilities + + .. java:method:: static string GetDataFileText(string name) + + .. java:method:: static byte[] GetDataFileBytes(string name) + + .. java:method:: static object GetAnswer(ulong n) + +.. literalinclude:: ../../java/src/main/java/euler/lib/Utilities.java + :language: java + :linenos: diff --git a/java/README.rst b/java/README.rst index 487a1929..a3771108 100644 --- a/java/README.rst +++ b/java/README.rst @@ -75,4 +75,10 @@ dependencies for this section. Problems Solved --------------- -- ☒ `1 <./src/main/p0001.java>`__ +- ☒ `1 <./src/main/java/p0001.java>`__ +- ☒ `2 <./src/main/java/p0002.java>`__ +- ☒ `4 <./src/main/java/p0004.java>`__ +- ☒ `6 <./src/main/java/p0006.java>`__ +- ☒ `9 <./src/main/java/p0009.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 5ecd9fd6..a1620ca0 100644 --- a/java/src/main/java/euler/lib/Utilities.java +++ b/java/src/main/java/euler/lib/Utilities.java @@ -28,16 +28,15 @@ private static Path getDataPath(String name) throws IOException { return filePath.toAbsolutePath(); } - public static String getDataFileText(String name) throws IOException { - Path filePath = getDataPath(name); - return new String(Files.readAllBytes(filePath)); - } - public static byte[] getDataFileBytes(String name) throws IOException { Path filePath = getDataPath(name); return Files.readAllBytes(filePath); } + public static String getDataFileText(String name) throws IOException { + return new String(getDataFileBytes(name)); + } + public static Object getAnswer(long n) throws IOException { String csvContent = getDataFileText("answers.csv"); try (BufferedReader reader = new BufferedReader(new StringReader(csvContent))) { diff --git a/java/src/main/java/euler/p0000.java b/java/src/main/java/euler/p0000.java index 144103ee..cc24e613 100644 --- a/java/src/main/java/euler/p0000.java +++ b/java/src/main/java/euler/p0000.java @@ -1,3 +1,12 @@ +/* +Project Euler Template + +This template is used to format Project Euler solution scripts. This paragraph +should be replaced by a description of how I approached the problem, as well as +critque. + +This paragraph should be replaced by the problem description, excluding images. +*/ package euler; public class p0000 implements IEuler { diff --git a/java/src/main/java/euler/p0001.java b/java/src/main/java/euler/p0001.java index 318888f9..14ad3545 100644 --- a/java/src/main/java/euler/p0001.java +++ b/java/src/main/java/euler/p0001.java @@ -1,3 +1,17 @@ +/* +Project Euler Problem 1 + +I know that this could be done faster with a traditional for loop, but I wanted +to see if iterators were reasonably possible in C, since it makes the prime +number infrastructure a lot easier to set up. + +Problem: + +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. +*/ package euler; public class p0001 implements IEuler { diff --git a/java/src/main/java/euler/p0002.java b/java/src/main/java/euler/p0002.java new file mode 100644 index 00000000..d097bb46 --- /dev/null +++ b/java/src/main/java/euler/p0002.java @@ -0,0 +1,36 @@ +/* +Project Euler Problem 2 + +This is a port of the optimized version found in python. For a proof of why this +works, see that implementation + +Problem: + +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, ... + +By considering the terms in the Fibonacci sequence whose values do not exceed +four million, find the sum of the even-valued terms. +*/ +package euler; + +public class p0002 implements IEuler { + @Override + public Object answer() { + int answer = 0, + i = 2, + j = 8, + tmp = 0; + + while (i < 4000000) { + answer += i; + tmp = 4 * j + i; + i = j; + j = tmp; + } + + return answer; + } +} \ No newline at end of file diff --git a/java/src/main/java/euler/p0004.java b/java/src/main/java/euler/p0004.java new file mode 100644 index 00000000..19eb78b3 --- /dev/null +++ b/java/src/main/java/euler/p0004.java @@ -0,0 +1,41 @@ +/* +Project Euler Problem 4 + +I couldn't figure out how to do this as efficiently as I would have liked. I am +SURE that there is a better way to check if a number is a palindrome, but I +could not think of one. + +Problem: + +A palindromic number reads the same both ways. The largest palindrome made from +the product of two 2-digit numbers is 9009 = 91 × 99. + +Find the largest palindrome made from the product of two 3-digit numbers. +*/ +package euler; + +public class p0004 implements IEuler { + private boolean IsPalindrome(int x) { + String rep = x.toString(); + int length = rep.length(); + for (int i = 0; i < length; i += 1) { + if (rep.charAt(i) != rep.charAt(length - i - 1)) + return false; + } + return true; + } + + @Override + public Object answer() { + int answer = 0; + for (int v = 101; v < 1000; v++) { + for (int u = 100; u < v; u++) { + int p = u * v; + if (IsPalindrome(p) && p > answer) + answer = p; + } + } + + return answer; + } +} \ No newline at end of file diff --git a/java/src/main/java/euler/p0006.java b/java/src/main/java/euler/p0006.java new file mode 100644 index 00000000..233e5cec --- /dev/null +++ b/java/src/main/java/euler/p0006.java @@ -0,0 +1,35 @@ +/* +Project Euler Problem 6 + +This turned out to be really easy + +Problem: + +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. +*/ +package euler; + +public class p0006 implements IEuler { + @Override + public Object answer() { + int sum_of_squares = 0, + sum = 0; + for (int i = 1; i < 101; i++) { + sum += i; + sum_of_squares += i * i; + } + + int square_of_sum = sum * sum; + return square_of_sum - sum_of_squares; + } +} \ No newline at end of file diff --git a/java/src/main/java/euler/p0009.java b/java/src/main/java/euler/p0009.java new file mode 100644 index 00000000..0c484b59 --- /dev/null +++ b/java/src/main/java/euler/p0009.java @@ -0,0 +1,33 @@ +/* +Project Euler Problem 9 + + + +Problem: + +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. +*/ +package euler; + +public class p0009 implements IEuler { + @Override + public Object answer() { + for (int c = 3; ; c++) { + int c_square = c * c; + for (int b = 2; b < c; b++) { + int b_square = b * b; + for (int a = 1; a < b; a++) { + int a_square = a * a; + if (a_square + b_square == c_square && a + b + c == 1000) + return a * b * c; + } + } + } + } +} \ No newline at end of file diff --git a/java/src/main/java/euler/p0022.java b/java/src/main/java/euler/p0022.java new file mode 100644 index 00000000..714c73f8 --- /dev/null +++ b/java/src/main/java/euler/p0022.java @@ -0,0 +1,41 @@ +/* +Project Euler Problem 22 + +Porting my file reader from C# was a bit more troublesome than I expected + +Problem: + +Using names.txt (right click and 'Save Link/Target As...'), 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? +*/ +package euler; + +import java.utils.Arrays; + +import euler.Utilities; + +public class p0022 implements IEuler { + @Override + public Object answer() { + int answer = 0; + String[] names = Utilities.GetDataFileText("p0022_names.txt").replace("\"", "").split(',', 0); + Arrays.Sort(names); + for (int i = 0; i < names.length; i += 1) { + int sum = 0; + for (int j = 0; j < names[i].length(); j += 1) { + sum += names[i].charAt(j) & 0x3F; + } + answer += sum * (i + 1); + } + return answer; + } +} \ No newline at end of file diff --git a/java/src/main/java/euler/p0836.java b/java/src/main/java/euler/p0836.java index b8b7a755..d2a6b0bf 100644 --- a/java/src/main/java/euler/p0836.java +++ b/java/src/main/java/euler/p0836.java @@ -1,3 +1,19 @@ +/* +Project Euler Problem 836 + +Alright, this was funny + +Problem: + +Let A be an **affine plane** over a **radically integral local field** F with residual characteristic p. + +We consider an **open oriented line section** U of A with normalized Haar measure m. + +Define f(m,p) as the maximal possible discriminant of the **jacobian** associated to the +**orthogonal kernel embedding** of U into A. + +Find f(20230401, 57). Give as your answer the concatenation of the first letters of each bolded word. +*/ package euler; public class p0836 implements IEuler { diff --git a/java/src/test/java/EulerTest.java b/java/src/test/java/EulerTest.java index 252bb22e..1790e0fc 100644 --- a/java/src/test/java/EulerTest.java +++ b/java/src/test/java/EulerTest.java @@ -16,11 +16,11 @@ static Stream data() throws IOException { return Stream.of( new Object[]{p0000.class, false, 0}, new Object[]{p0001.class, false, 233168}, - // 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[]{p0002.class, false, 4613732}, + new Object[]{p0004.class, false, 906609}, + new Object[]{p0006.class, false, 25164150}, // new Object[]{p0008.class, false, Utilities.getAnswer(8)}, - // new Object[]{p0009.class, false, Utilities.getAnswer(9)}, + new Object[]{p0009.class, false, 31875000}, // new Object[]{p0011.class, false, Utilities.getAnswer(11)}, // new Object[]{p0013.class, false, Utilities.getAnswer(13)}, // new Object[]{p0014.class, false, Utilities.getAnswer(14)}, @@ -28,7 +28,7 @@ static Stream data() throws IOException { // new Object[]{p0016.class, false, Utilities.getAnswer(16)}, // new Object[]{p0017.class, false, Utilities.getAnswer(17)}, // new Object[]{p0020.class, false, Utilities.getAnswer(20)}, - // new Object[]{p0022.class, false, Utilities.getAnswer(22)}, + new Object[]{p0022.class, false, 871198282}, // new Object[]{p0034.class, false, Utilities.getAnswer(34)}, // new Object[]{p0076.class, true, Utilities.getAnswer(76)}, new Object[]{p0836.class, false, "aprilfoolsjoke"} @@ -43,7 +43,7 @@ void eulerTestProblem(Class problemClass, boolean isSlow, Object expected) th Method answerMethod = problemClass.getMethod("answer"); IEuler instance = (IEuler)problemClass.getDeclaredConstructor().newInstance(); long startTime = System.nanoTime(); - Object result = answerMethod.invoke(instance); // Instance method invocation + Object result = answerMethod.invoke(instance); long elapsedTime = System.nanoTime() - startTime; Assertions.assertEquals(expected, result); if (!isSlow) {