Skip to content

Commit

Permalink
Solve many problems in java (22 is expected failure)
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 9, 2024
1 parent 95de053 commit 5a92297
Show file tree
Hide file tree
Showing 20 changed files with 323 additions and 21 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+ | 2 | |Javai| |br| |
| Java | Java 8+ | 6 | |Javai| |br| |
| | | | |Ja-Cov| |br| |
| | | | |CodeQL| |br| |
| | | | |Java-lint| |
Expand Down
2 changes: 1 addition & 1 deletion docs/csharp/utils.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
utils.cs
=======
========

View source code :source:`csharp/include/utils.cs`

Expand Down
14 changes: 7 additions & 7 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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| |
+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand All @@ -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`|
+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand All @@ -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`| |
+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand Down
12 changes: 12 additions & 0 deletions docs/java/p0002.rst
Original file line number Diff line number Diff line change
@@ -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:
12 changes: 12 additions & 0 deletions docs/java/p0004.rst
Original file line number Diff line number Diff line change
@@ -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:
12 changes: 12 additions & 0 deletions docs/java/p0006.rst
Original file line number Diff line number Diff line change
@@ -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:
12 changes: 12 additions & 0 deletions docs/java/p0009.rst
Original file line number Diff line number Diff line change
@@ -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:
12 changes: 12 additions & 0 deletions docs/java/p0022.rst
Original file line number Diff line number Diff line change
@@ -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:
16 changes: 16 additions & 0 deletions docs/java/utils.rst
Original file line number Diff line number Diff line change
@@ -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:
8 changes: 7 additions & 1 deletion java/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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>`__
9 changes: 4 additions & 5 deletions java/src/main/java/euler/lib/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down
9 changes: 9 additions & 0 deletions java/src/main/java/euler/p0000.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions java/src/main/java/euler/p0001.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
36 changes: 36 additions & 0 deletions java/src/main/java/euler/p0002.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
41 changes: 41 additions & 0 deletions java/src/main/java/euler/p0004.java
Original file line number Diff line number Diff line change
@@ -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(uint x) {
String rep = x.toString();
int length = rep.length();
for (int i = 0; i < length; i += 1) {
if (rep[i] != rep[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;
}
}
35 changes: 35 additions & 0 deletions java/src/main/java/euler/p0006.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
33 changes: 33 additions & 0 deletions java/src/main/java/euler/p0009.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
}
Loading

0 comments on commit 5a92297

Please sign in to comment.