diff --git a/README.rst b/README.rst index 5c80b4b6..24286157 100644 --- a/README.rst +++ b/README.rst @@ -73,7 +73,7 @@ Olivia's Project Euler Solutions | | | | |CodeQL| |br| | | | | | |C#-lint| | +------------+--------------------------+--------+-------------------+ -| Java | Java 8+ | 14 | |Javai| |br| | +| Java | Java 8+ | 17 | |Javai| |br| | | | | | |Ja-Cov| |br| | | | | | |CodeQL| |br| | | | | | |Java-lint| | diff --git a/docs/index.rst b/docs/index.rst index e784fcf6..a1a05127 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -107,7 +107,7 @@ Problems Solved +-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`19` | | | | | |:py-d:`0019`|:rs-d:`0019`| +-----------+------------+------------+------------+------------+------------+------------+------------+ -|:prob:`20` |:c-d:`0020` |:cp-d:`0020`|:cs-d:`0020`| |:js-d:`0020`|:py-d:`0020`|:rs-d:`0020`| +|:prob:`20` |:c-d:`0020` |:cp-d:`0020`|:cs-d:`0020`|:ja-d:`0020`|:js-d:`0020`|:py-d:`0020`|:rs-d:`0020`| +-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`21` | | | | | |:py-d:`0021`| | +-----------+------------+------------+------------+------------+------------+------------+------------+ @@ -133,7 +133,7 @@ Problems Solved +-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`33` | | | | | |:py-d:`0033`| | +-----------+------------+------------+------------+------------+------------+------------+------------+ -|:prob:`34` |:c-d:`0034` |:cp-d:`0034`|:cs-d:`0034`| |:js-d:`0034`|:py-d:`0034`|:rs-d:`0034`| +|:prob:`34` |:c-d:`0034` |:cp-d:`0034`|:cs-d:`0034`|:ja-d:`0034`|:js-d:`0034`|:py-d:`0034`|:rs-d:`0034`| +-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`35` | | | | | |:py-d:`0035`| | +-----------+------------+------------+------------+------------+------------+------------+------------+ @@ -195,7 +195,7 @@ Problems Solved +-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`74` | | | | | |:py-d:`0074`| | +-----------+------------+------------+------------+------------+------------+------------+------------+ -|:prob:`76` |:c-d:`0076` |:cp-d:`0076`|:cs-s:`0076`| |:js-s:`0076`|:py-d:`0076`|:rs-d:`0076`| +|:prob:`76` |:c-d:`0076` |:cp-d:`0076`|:cs-s:`0076`|:ja-s:`0076`|:js-s:`0076`|:py-d:`0076`|:rs-d:`0076`| +-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`77` | | | | | |:py-d:`0077`| | +-----------+------------+------------+------------+------------+------------+------------+------------+ diff --git a/docs/java/p0015.rst b/docs/java/p0015.rst index 8f3da9f8..8bf2adb8 100644 --- a/docs/java/p0015.rst +++ b/docs/java/p0015.rst @@ -3,6 +3,14 @@ Java Implementation of Problem 15 View source code :source:`java/src/main/java/euler/p0015.java` +Includes +-------- + +- `Mathematics.java <./math.html>`_ + +Problem Solution +---------------- + .. java:type:: public class p0015 implements IEuler .. java:method:: Object answer() diff --git a/docs/java/p0020.rst b/docs/java/p0020.rst new file mode 100644 index 00000000..f72bc564 --- /dev/null +++ b/docs/java/p0020.rst @@ -0,0 +1,14 @@ +Java Implementation of Problem 20 +================================= + +View source code :source:`java/src/main/java/euler/p0020.java` + +.. java:type:: public class p0020 implements IEuler + + .. java:method:: Object answer() + + :return: The answer to Project Euler problem 20 + +.. literalinclude:: ../../java/src/main/java/euler/p0020.java + :language: java + :linenos: diff --git a/docs/java/p0022.rst b/docs/java/p0022.rst index ef90a151..8b04ee5b 100644 --- a/docs/java/p0022.rst +++ b/docs/java/p0022.rst @@ -3,6 +3,13 @@ Java Implementation of Problem 22 View source code :source:`java/src/main/java/euler/p0022.java` +Includes +-------- + +- `Utilities.java <./utils.html>`_ + +Problem Solution +---------------- .. java:type:: public class p0022 implements IEuler .. java:method:: Object answer() diff --git a/docs/java/p0034.rst b/docs/java/p0034.rst new file mode 100644 index 00000000..9924de48 --- /dev/null +++ b/docs/java/p0034.rst @@ -0,0 +1,14 @@ +Java Implementation of Problem 34 +================================= + +View source code :source:`java/src/main/java/euler/p0034.java` + +.. java:type:: public class p0034 implements IEuler + + .. java:method:: Object answer() + + :return: The answer to Project Euler problem 34 + +.. literalinclude:: ../../java/src/main/java/euler/p0034.java + :language: java + :linenos: diff --git a/docs/java/p0076.rst b/docs/java/p0076.rst new file mode 100644 index 00000000..f96082c2 --- /dev/null +++ b/docs/java/p0076.rst @@ -0,0 +1,14 @@ +Java Implementation of Problem 76 +================================= + +View source code :source:`java/src/main/java/euler/p0076.java` + +.. java:type:: public class p0076 implements IEuler + + .. java:method:: Object answer() + + :return: The answer to Project Euler problem 76 + +.. literalinclude:: ../../java/src/main/java/euler/p0076.java + :language: java + :linenos: diff --git a/java/README.rst b/java/README.rst index 10792253..80957272 100644 --- a/java/README.rst +++ b/java/README.rst @@ -87,5 +87,8 @@ Problems Solved - ☒ `15 <./src/main/java/p0015.java>`__ - ☒ `16 <./src/main/java/p0016.java>`__ - ☒ `17 <./src/main/java/p0017.java>`__ +- ☒ `20 <./src/main/java/p0020.java>`__ - ☒ `22 <./src/main/java/p0022.java>`__ +- ☒ `34 <./src/main/java/p0034.java>`__ +- ☒ `76 <./src/main/java/p0076.java>`__ - ☒ `836 <./src/main/java/p0836.java>`__ diff --git a/java/src/main/java/euler/p0020.java b/java/src/main/java/euler/p0020.java new file mode 100644 index 00000000..d858585c --- /dev/null +++ b/java/src/main/java/euler/p0020.java @@ -0,0 +1,47 @@ +/* +Project Euler Problem 34 + +This ended up being a filtering problem. The problem with my solution is that I +am not satisfied with my filter at all. I feel like there is a more efficient +way to go about it. + +Problem: + +145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. + +Find the sum of all numbers which are equal to the sum of the factorial of +their digits. + +Note: as 1! = 1 and 2! = 2 are not sums they are not included. +*/ +package euler; + +public class p0020 implements IEuler { + @Override + public Object answer() { + long[] numbers = new long[10]; + long ten17 = 100000000000000000L; + numbers[0] = 1; + for (byte i = 2; i <= 100; i++) { + for (byte j = 0; j < 10; j++) { + numbers[j] *= i; + } + for (byte j = 0; j < 9; j++) { + if (numbers[j] > ten17) { + numbers[j + 1] += numbers[j] / ten17; + numbers[j] %= ten17; + } + } + } + long answer = 0; + long power = 1; + for (byte i = 0; i < 19; i++) { + for (byte j = 0; j < 10; j++) { + long value = numbers[j] / power; + answer += value % 10; + } + power *= 10; + } + return (short) answer; + } +} \ No newline at end of file diff --git a/java/src/main/java/euler/p0022.java b/java/src/main/java/euler/p0022.java index 5e216b9e..a7496333 100644 --- a/java/src/main/java/euler/p0022.java +++ b/java/src/main/java/euler/p0022.java @@ -30,7 +30,9 @@ public Object answer() { int answer = 0; String[] names; try { - names = Utilities.getDataFileText("p0022_names.txt").replace("\"", "").split(",", 0); + names = Utilities.getDataFileText("p0022_names.txt") + .replace("\"", "") + .split(",", 0); } catch (IOException e) { return null; } diff --git a/java/src/main/java/euler/p0034.java b/java/src/main/java/euler/p0034.java new file mode 100644 index 00000000..e29a727d --- /dev/null +++ b/java/src/main/java/euler/p0034.java @@ -0,0 +1,35 @@ +/* +Project Euler Problem 34 + +This ended up being a filtering problem. The problem with my solution is that I +am not satisfied with my filter at all. I feel like there is a more efficient +way to go about it. + +Problem: + +145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. + +Find the sum of all numbers which are equal to the sum of the factorial of +their digits. + +Note: as 1! = 1 and 2! = 2 are not sums they are not included. +*/ +package euler; + +import euler.lib.Mathematics; + +public class p0034 implements IEuler { + @Override + public Object answer() { + int answer = 0; + for (int x = 10; x < 100000; x += 1) { + String xs = Integer.toString(x); + int sum = 0; + for (byte i = 0; i < xs.length(); i += 1) + sum += (int) Mathematics.factorial(xs.charAt(i) - '0'); + if (sum == x) + answer += x; + } + return (short) answer; + } +} \ No newline at end of file diff --git a/java/src/main/java/euler/p0076.java b/java/src/main/java/euler/p0076.java new file mode 100644 index 00000000..8e14dbde --- /dev/null +++ b/java/src/main/java/euler/p0076.java @@ -0,0 +1,50 @@ +/* +Project Euler Problem 76 + +I ended up having to do this iteratively, which I'm not super happy with. I feel like there is almost certainly a +closed-form solution to this, but I was unable to figure it out. + +Problem: + +It is possible to write five as a sum in exactly six different ways: + +4 + 1 +3 + 2 +3 + 1 + 1 +2 + 2 + 1 +2 + 1 + 1 + 1 +1 + 1 + 1 + 1 + 1 + +How many different ways can one hundred be written as a sum of at least two +positive integers? +*/ +package euler; + +public class p0076 implements IEuler { + @Override + public Object answer() { + byte idx, sum = 100; + int answer = 0; + byte[] counts = new byte[101]; + counts[2] = 100; + while (counts[100] == 0) { + counts[2] += 2; + if (sum >= 100) { + answer += (100 + counts[2] - sum) / 2; + idx = 2; + do { + counts[idx] = 0; + idx += 1; + counts[idx] += idx; + sum = 0; + for (byte i = (byte) (idx - 1); i < 101; i += 1) + sum += counts[i]; + } while (sum > 100); + } + sum = 0; + for (byte i = 0; i < 101; i += 1) + sum += counts[i]; + } + return answer; + } +} \ No newline at end of file diff --git a/java/src/test/java/EulerTest.java b/java/src/test/java/EulerTest.java index 0ec47645..a2ff03d8 100644 --- a/java/src/test/java/EulerTest.java +++ b/java/src/test/java/EulerTest.java @@ -27,10 +27,10 @@ static Stream data() throws IOException { new Object[] { p0015.class, false, Utilities.getAnswer(15) }, 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[] { p0020.class, false, Utilities.getAnswer(20) }, new Object[] { p0022.class, false, Utilities.getAnswer(22) }, - // new Object[]{p0034.class, false, Utilities.getAnswer(34)}, - // new Object[]{p0076.class, true, Utilities.getAnswer(76)}, + new Object[] { p0034.class, false, Utilities.getAnswer(34) }, + new Object[] { p0076.class, true, Utilities.getAnswer(76) }, new Object[] { p0836.class, false, Utilities.getAnswer(836) }); }