diff --git a/README.rst b/README.rst index 6cdaea9c..d7e4d05b 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| | 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| | diff --git a/docs/index.rst b/docs/index.rst index 45d8226a..01c09dd8 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -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`| +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+ diff --git a/docs/src/java/lib/primes.rst b/docs/src/java/lib/primes.rst index a7c53612..414cc912 100644 --- a/docs/src/java/lib/primes.rst +++ b/docs/src/java/lib/primes.rst @@ -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 primesUntil(Long limit) + .. java:method:: public static Stream primesUntil(long limit) :return: Iterates over the prime numbers, up until a given limit + .. java:method:: public static Stream primeFactors(long target) + + :return: Iterates over the prime factors of a number + .. literalinclude:: ../../../../java/src/main/java/euler/lib/Primes.java :language: java :linenos: diff --git a/docs/src/java/p0003.rst b/docs/src/java/p0003.rst new file mode 100644 index 00000000..b2b4ac17 --- /dev/null +++ b/docs/src/java/p0003.rst @@ -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 diff --git a/java/README.rst b/java/README.rst index 3f201d08..fbdd8533 100644 --- a/java/README.rst +++ b/java/README.rst @@ -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>`__ diff --git a/java/src/main/java/euler/lib/Primes.java b/java/src/main/java/euler/lib/Primes.java index 72a4f36c..f57d6b72 100644 --- a/java/src/main/java/euler/lib/Primes.java +++ b/java/src/main/java/euler/lib/Primes.java @@ -30,7 +30,7 @@ public static Stream primes() { return StreamSupport.stream(new PrimeSpliterator(null), false); } - public static Stream primesUntil(Long limit) { + public static Stream primesUntil(long limit) { return StreamSupport.stream(new PrimeSpliterator(limit), false); } @@ -171,4 +171,77 @@ public Long next() { } } } + + public static Stream primeFactors(long target) { + return StreamSupport.stream(new PrimeFactorSpliterator(target), false); + } + + private static class PrimeFactorSpliterator implements Spliterator { + private final PrimeFactorIterator primeFactorIterator; + + PrimeFactorSpliterator(Long limit) { + primeFactorIterator = new PrimeFactorIterator(limit); + } + + @Override + public boolean tryAdvance(Consumer action) { + if (primeFactorIterator.hasNext()) { + action.accept(primeFactorIterator.next()); + return true; + } + return false; + } + + @Override + public Spliterator 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 { + private final Long target; + private Long lastPrime; + private Iterator 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; + } + } } \ No newline at end of file diff --git a/java/src/main/java/euler/p0003.java b/java/src/main/java/euler/p0003.java new file mode 100644 index 00000000..f133fada --- /dev/null +++ b/java/src/main/java/euler/p0003.java @@ -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(); + } +} \ No newline at end of file diff --git a/java/src/test/java/EulerTest.java b/java/src/test/java/EulerTest.java index 0431b5fc..b2c6e68d 100644 --- a/java/src/test/java/EulerTest.java +++ b/java/src/test/java/EulerTest.java @@ -17,6 +17,7 @@ static Stream 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) },