Skip to content

Commit

Permalink
Attempt p7 in Java
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Sep 16, 2024
1 parent 7d33721 commit d7c604d
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 3 deletions.
4 changes: 2 additions & 2 deletions 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| | 18 | |Javai| |br| |
| Java | Java 8+ in Corretto, |br| | 19 | |Javai| |br| |
| | Dragonwell, Liberica, |br| | | |Ja-Cov| |br| |
| | Microsoft, Oracle, |br| | | |CodeQL| |br| |
| | Semeru, Temurin, & Zulu | | |Java-lint| |
Expand All @@ -95,7 +95,7 @@ Olivia's Project Euler Solutions
| | Browser [#]_ | | |CodeQL| |br| |
| | | | |ESLint| |
+------------+----------------------------+--------+-------------------+
| Lua | Lua 5+ [#]_ | 17 | |Luai| |br| |
| Lua | PUC-Rio Lua 5+ [#]_ | 17 | |Luai| |br| |
| | | | |Lu-Cov| |br| |
| | | | |LuaCheck| |
+------------+----------------------------+--------+-------------------+
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Problems Solved
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`006`|:c-d:`0006`|:cp-d:`0006`|:cs-d:`0006`|:ja-d:`0006`|:js-d:`0006`|:lu-d:`0006`|:py-d:`0006`|:rs-d:`0006`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`007`|:c-d:`0007`|:cp-d:`0007`|:cs-d:`0007`| |:js-d:`0007`|:lu-d:`0007`|:py-d:`0007`|:rs-d:`0007`|
|:prob:`007`|:c-d:`0007`|:cp-d:`0007`|:cs-d:`0007`|:ja-d:`0007`|:js-d:`0007`|:lu-d:`0007`|:py-d:`0007`|:rs-d:`0007`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`008`|:c-d:`0008`|:cp-d:`0008`|:cs-d:`0008`|:ja-d:`0008`|:js-d:`0008`|:lu-d:`0008`|:py-d:`0008`|:rs-d:`0008`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand Down
20 changes: 20 additions & 0 deletions docs/src/java/lib/primes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Primes.java
===========

View source code :source:`java/src/main/java/euler/lib/Primes.java`

.. java:type:: public class PrimeGenerator
.. java:method:: public static Stream<Long> primes()
:return: Iterates over the prime numbers

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

.. literalinclude:: ../../../../java/src/main/java/euler/lib/Primes.java
:language: java
:linenos:

.. tags:: prime-number, java-iterator
16 changes: 16 additions & 0 deletions docs/src/java/p0007.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Java Implementation of Problem 7
================================

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

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

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

.. tags:: 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 @@ -85,6 +85,7 @@ Problems Solved
- ☒ `2 <./src/main/java/p0002.java>`__
- ☒ `4 <./src/main/java/p0004.java>`__
- ☒ `6 <./src/main/java/p0006.java>`__
- ☒ `7 <./src/main/java/p0007.java>`__
- ☒ `8 <./src/main/java/p0008.java>`__
- ☒ `9 <./src/main/java/p0009.java>`__
- ☒ `11 <./src/main/java/p0011.java>`__
Expand Down
139 changes: 139 additions & 0 deletions java/src/main/java/euler/lib/Primes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class PrimeGenerator {

private static class Cache {
long lastCached;
List<Long> primes;

Cache(long lastCached, List<Long> primes) {
this.lastCached = lastCached;
this.primes = primes;
}
}

private static final Cache CACHE = new Cache(0, new ArrayList<>());

public static Stream<Long> primes() {
return StreamSupport.stream(new PrimeIterator(null).spliterator(), false);
}

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

private static class PrimeIterator implements Iterator<Long> {
private final Long limit;
private Iterator<Long> primeGenerator;
private boolean exhausted = false; // Flag to indicate if we have exhausted all primes

PrimeIterator(Long limit) {
this.limit = limit;
// Initialize primeGenerator with a recursive prime generator
primeGenerator = new PrimeGeneratorIterator(null);
}

@Override
public boolean hasNext() {
if (exhausted) {
return false;
}

// Check if the limit has been reached with cached primes
if (limit != null && CACHE.lastCached >= limit) {
return false;
}

// Check if there are more primes to generate
return primeGenerator.hasNext();
}

@Override
public Long next() {
if (limit != null && CACHE.lastCached >= limit) {
exhausted = true;
return null; // Indicate end of stream
}

// Generate and yield new primes
while (true) {
if (primeGenerator.hasNext()) {
long prime = primeGenerator.next();
if (limit != null && prime >= limit) {
exhausted = true;
return null; // Indicate end of stream
}
CACHE.primes.add(prime);
CACHE.lastCached = prime;
return prime;
}

// Reinitialize primeGenerator if needed
primeGenerator = new PrimeGeneratorIterator(null);
}
}
}

private static class PrimeGeneratorIterator implements Iterator<Long> {
private final List<Long> initialPrimes = List.of(2L, 3L, 5L, 7L);
private final Map<Long, Long> sieve = new HashMap<>();
private Iterator<Long> recursivePrimes;
private long currentPrime;
private long primeSquared;
private long step = 2;
private long candidate = 9;

PrimeGeneratorIterator(Long stop) {
// Initialize with initial primes and state
initialPrimes.forEach(prime -> {
sieve.put(prime, step);
step = prime * 2;
});
recursivePrimes = new PrimeGeneratorIterator(null);
if (recursivePrimes.hasNext()) {
currentPrime = recursivePrimes.next();
}
if (currentPrime != 3) {
throw new IllegalStateException("Unexpected prime value");
}
primeSquared = currentPrime * currentPrime;
}

@Override
public boolean hasNext() {
return true; // Infinite sequence
}

@Override
public Long next() {
while (true) {
if (sieve.containsKey(candidate)) {
step = sieve.remove(candidate);
} else if (candidate < primeSquared) {
long result = candidate;
candidate += 2;
return result;
} else {
step = currentPrime * 2;
if (recursivePrimes.hasNext()) {
currentPrime = recursivePrimes.next();
}
primeSquared = currentPrime * currentPrime;
}
long multiple = candidate;
while (sieve.containsKey(multiple)) {
multiple += step;
}
sieve.put(multiple, step);
candidate += 2;
}
}
}
}
21 changes: 21 additions & 0 deletions java/src/main/java/euler/p0007.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Project Euler Problem 7
Problem:
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that
the 6th prime is 13.
What is the 10 001st prime number?
*/
package euler;

public class p0007 implements IEuler {
@Override
public Object answer() {
return PrimeGenerator.primes()
.skip(10000)
.findFirst()
.orElseThrow(() -> new RuntimeException("Prime number not found"));
}
}
1 change: 1 addition & 0 deletions java/src/test/java/EulerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ static Stream<Object[]> data() throws IOException {
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[] { p0007.class, false, Utilities.getAnswer(7) },
new Object[] { p0008.class, false, Utilities.getAnswer(8) },
new Object[] { p0009.class, false, Utilities.getAnswer(9) },
new Object[] { p0011.class, false, Utilities.getAnswer(11) },
Expand Down

0 comments on commit d7c604d

Please sign in to comment.