Skip to content

Commit

Permalink
Attempt p3 in Java
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Sep 16, 2024
1 parent a165fe2 commit 9bdeba9
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Olivia's Project Euler Solutions
| | | | |CodeQL| |br| |
| | | | |C#-lint| |
+------------+----------------------------+--------+-------------------+
| Java | Java 8+ in Corretto, |br| | 20 | |Javai| |br| |
| Java | Java 8+ in Corretto, |br| | 21 | |Javai| |br| |
| | Dragonwell, Liberica, |br| | | |Ja-Cov| |br| |
| | Microsoft, Oracle, |br| | | |CodeQL| |br| |
| | Semeru, Temurin, & Zulu | | |Java-lint| |
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Problems Solved
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`002`|:c-d:`0002`|:cp-d:`0002`|:cs-d:`0002`|:ja-d:`0002`|:js-d:`0002`|:lu-d:`0002`|:py-d:`0002`|:rs-d:`0002`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`003`|:c-d:`0003`|:cp-d:`0003`|:cs-d:`0003`| |:js-d:`0003`|:lu-d:`0003`|:py-d:`0003`|:rs-d:`0003`|
|:prob:`003`|:c-d:`0003`|:cp-d:`0003`|:cs-d:`0003`|:ja-d:`0003`|:js-d:`0003`|:lu-d:`0003`|:py-d:`0003`|:rs-d:`0003`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`004`|:c-d:`0004`|:cp-d:`0004`|:cs-d:`0004`|:ja-d:`0004`|:js-d:`0004`|:lu-d:`0004`|:py-d:`0004`|:rs-d:`0004`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand Down
6 changes: 5 additions & 1 deletion docs/src/java/lib/primes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ View source code :source:`java/src/main/java/euler/lib/Primes.java`
:return: Iterates over the prime numbers

.. java:method:: public static Stream<Long> primesUntil(Long limit)
.. java:method:: public static Stream<Long> primesUntil(long limit)
:return: Iterates over the prime numbers, up until a given limit

.. java:method:: public static Stream<Long> primeFactors(long target)
:return: Iterates over the prime factors of a number

.. literalinclude:: ../../../../java/src/main/java/euler/lib/Primes.java
:language: java
:linenos:
Expand Down
24 changes: 24 additions & 0 deletions docs/src/java/p0003.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Java Implementation of Problem 3
================================

View source code :source:`java/src/main/java/euler/p0003.java`

Includes
--------

- `Primes.java <./lib/primes.html>`_

Problem Solution
----------------

.. java:type:: public class p0003 implements IEuler
.. java:method:: Object answer()
:return: The answer to Project Euler problem 3

.. literalinclude:: ../../../java/src/main/java/euler/p0003.java
:language: java
:linenos:

.. tags:: factorization, prime-number, java-iterator
1 change: 1 addition & 0 deletions java/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Problems Solved

- ☒ `1 <./src/main/java/p0001.java>`__
- ☒ `2 <./src/main/java/p0002.java>`__
- ☒ `3 <./src/main/java/p0003.java>`__
- ☒ `4 <./src/main/java/p0004.java>`__
- ☒ `6 <./src/main/java/p0006.java>`__
- ☒ `7 <./src/main/java/p0007.java>`__
Expand Down
75 changes: 74 additions & 1 deletion java/src/main/java/euler/lib/Primes.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static Stream<Long> primes() {
return StreamSupport.stream(new PrimeSpliterator(null), false);
}

public static Stream<Long> primesUntil(Long limit) {
public static Stream<Long> primesUntil(long limit) {
return StreamSupport.stream(new PrimeSpliterator(limit), false);
}

Expand Down Expand Up @@ -171,4 +171,77 @@ public Long next() {
}
}
}

public static Stream<Long> primeFactors(long target) {
return StreamSupport.stream(new PrimeFactorSpliterator(target), false);
}

private static class PrimeFactorSpliterator implements Spliterator<Long> {
private final PrimeFactorIterator primeFactorIterator;

PrimeFactorSpliterator(Long limit) {
primeFactorIterator = new PrimeFactorIterator(limit);
}

@Override
public boolean tryAdvance(Consumer<? super Long> action) {
if (primeFactorIterator.hasNext()) {
action.accept(primeFactorIterator.next());
return true;
}
return false;
}

@Override
public Spliterator<Long> trySplit() {
return null; // Sequential iteration only
}

@Override
public long estimateSize() {
return Long.MAX_VALUE; // Unknown size
}

@Override
public int characteristics() {
return ORDERED | SIZED | IMMUTABLE | NONNULL;
}
}

private static class PrimeFactorIterator implements Iterator<Long> {
private final Long target;
private Long lastPrime;
private Iterator<Long> primeGenerator;

PrimeIterator(Long target) {
this.target = target;
primeGenerator = new PrimeIterator();
lastPrime = primeGenerator.next();
}

@Override
public boolean hasNext() {
return this.target != 1;
}

@Override
public Long next() {
if (target < 0) {
target = -target;
return -1L;
}
if (target == 0) {
target = 1;
return 0L;
}
while (hasNext()) {
while (target % lastPrime != 0L && target < lastPrime) {
lastPrime = primeGenerator.next()
}
target /= lastPrime;
return lastPrime;
}
return null;
}
}
}
25 changes: 25 additions & 0 deletions java/src/main/java/euler/p0003.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Project Euler Problem 3
The lesson I've taken from these is that Streams are significantly more cumbersome in Java than in other languages
Problem:
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
*/
package euler;

import java.util.Comparator;

import euler.lib.Primes;

public class p0003 implements IEuler {
@Override
public Object answer() {
return Primes.primeFactors(600851475143L)
.max(Comparator.naturalOrder())
.get();
}
}
1 change: 1 addition & 0 deletions java/src/test/java/EulerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ static Stream<Object[]> data() throws IOException {
return Stream.of(new Object[] { p0000.class, false, 0 },
new Object[] { p0001.class, false, Utilities.getAnswer(1) },
new Object[] { p0002.class, false, Utilities.getAnswer(2) },
new Object[] { p0003.class, false, Utilities.getAnswer(3) },
new Object[] { p0004.class, false, Utilities.getAnswer(4) },
new Object[] { p0006.class, false, Utilities.getAnswer(6) },
new Object[] { p0007.class, false, Utilities.getAnswer(7) },
Expand Down

0 comments on commit 9bdeba9

Please sign in to comment.