Skip to content

Commit

Permalink
Attempt p7 in Java (5)
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Sep 16, 2024
1 parent cb148ba commit 14d2c65
Showing 1 changed file with 17 additions and 29 deletions.
46 changes: 17 additions & 29 deletions java/src/main/java/euler/lib/Primes.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package euler.lib;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -24,12 +26,10 @@ private static class Cache {

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

// Generate an infinite stream of primes
public static Stream<Long> primes() {
return StreamSupport.stream(new PrimeSpliterator(null), false);
}

// Generate a stream of primes up to a given limit
public static Stream<Long> primesUntil(Long limit) {
return StreamSupport.stream(new PrimeSpliterator(limit), false);
}
Expand Down Expand Up @@ -69,7 +69,7 @@ public int characteristics() {
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
private boolean exhausted = false;

PrimeIterator(Long limit) {
this.limit = limit;
Expand All @@ -78,42 +78,31 @@ private static class PrimeIterator implements Iterator<Long> {

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

// Check if the limit has been reached with cached primes
if (limit != null && CACHE.lastCached >= limit) {
if (exhausted || (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
return null;
}

// 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;
if (primeGenerator.hasNext()) {
long prime = primeGenerator.next();
if (limit != null && prime >= limit) {
exhausted = true;
return null;
}

// Reinitialize primeGenerator if needed
primeGenerator = new PrimeGeneratorIterator();
CACHE.primes.add(prime);
CACHE.lastCached = prime;
return prime;
}

return null;
}
}

Expand All @@ -132,9 +121,8 @@ private static class PrimeGeneratorIterator implements Iterator<Long> {
step = prime * 2;
});
recursivePrimes = new PrimeIterator(null);
if (recursivePrimes.hasNext()) {
currentPrime = recursivePrimes.next();
}
recursivePrimes.next();
currentPrime = recursivePrimes.next();
if (currentPrime != 3) {
throw new IllegalStateException("Unexpected prime value");
}
Expand Down

0 comments on commit 14d2c65

Please sign in to comment.