Skip to content

Commit

Permalink
Solve p20, p34, p76 in Java
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 11, 2024
1 parent b121fb9 commit 4d336bb
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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| |
Expand Down
6 changes: 3 additions & 3 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`| |
+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand All @@ -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`| |
+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand Down Expand Up @@ -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`| |
+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand Down
8 changes: 8 additions & 0 deletions docs/java/p0015.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 14 additions & 0 deletions docs/java/p0020.rst
Original file line number Diff line number Diff line change
@@ -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:
7 changes: 7 additions & 0 deletions docs/java/p0022.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 14 additions & 0 deletions docs/java/p0034.rst
Original file line number Diff line number Diff line change
@@ -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:
14 changes: 14 additions & 0 deletions docs/java/p0076.rst
Original file line number Diff line number Diff line change
@@ -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:
3 changes: 3 additions & 0 deletions java/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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>`__
47 changes: 47 additions & 0 deletions java/src/main/java/euler/p0020.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
4 changes: 3 additions & 1 deletion java/src/main/java/euler/p0022.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
35 changes: 35 additions & 0 deletions java/src/main/java/euler/p0034.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
50 changes: 50 additions & 0 deletions java/src/main/java/euler/p0076.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
6 changes: 3 additions & 3 deletions java/src/test/java/EulerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ static Stream<Object[]> 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) });
}

Expand Down

0 comments on commit 4d336bb

Please sign in to comment.