diff --git a/README.rst b/README.rst index 4d3a5588..9b712721 100644 --- a/README.rst +++ b/README.rst @@ -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| | @@ -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| | +------------+----------------------------+--------+-------------------+ diff --git a/docs/index.rst b/docs/index.rst index 4f28bab0..a0b0275a 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -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`| +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+ diff --git a/docs/src/java/lib/primes.rst b/docs/src/java/lib/primes.rst new file mode 100644 index 00000000..da4dc17d --- /dev/null +++ b/docs/src/java/lib/primes.rst @@ -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 primes() + + :return: Iterates over the prime numbers + + .. java:method:: public static Stream 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 diff --git a/docs/src/java/p0007.rst b/docs/src/java/p0007.rst new file mode 100644 index 00000000..f766a43d --- /dev/null +++ b/docs/src/java/p0007.rst @@ -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 diff --git a/java/README.rst b/java/README.rst index e9606dbf..d44fe695 100644 --- a/java/README.rst +++ b/java/README.rst @@ -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>`__ diff --git a/java/src/main/java/euler/lib/Primes.java b/java/src/main/java/euler/lib/Primes.java new file mode 100644 index 00000000..74056328 --- /dev/null +++ b/java/src/main/java/euler/lib/Primes.java @@ -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 primes; + + Cache(long lastCached, List primes) { + this.lastCached = lastCached; + this.primes = primes; + } + } + + private static final Cache CACHE = new Cache(0, new ArrayList<>()); + + public static Stream primes() { + return StreamSupport.stream(new PrimeIterator(null).spliterator(), false); + } + + public static Stream primesUntil(Long limit) { + return StreamSupport.stream(new PrimeIterator(limit).spliterator(), false); + } + + private static class PrimeIterator implements Iterator { + private final Long limit; + private Iterator 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 { + private final List initialPrimes = List.of(2L, 3L, 5L, 7L); + private final Map sieve = new HashMap<>(); + private Iterator 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; + } + } + } +} diff --git a/java/src/main/java/euler/p0007.java b/java/src/main/java/euler/p0007.java new file mode 100644 index 00000000..7c640cdf --- /dev/null +++ b/java/src/main/java/euler/p0007.java @@ -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")); + } +} \ No newline at end of file diff --git a/java/src/test/java/EulerTest.java b/java/src/test/java/EulerTest.java index 0733fef6..5c0246a3 100644 --- a/java/src/test/java/EulerTest.java +++ b/java/src/test/java/EulerTest.java @@ -19,6 +19,7 @@ static Stream 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) },