From 547f6addc7a9919c7b41d9cf42b54afe70674c57 Mon Sep 17 00:00:00 2001 From: lourenzo <81989+lourenzo@users.noreply.github.com> Date: Mon, 13 Jul 2026 03:39:39 +0000 Subject: [PATCH] refactor: optimize isPrime by replacing stream with standard for loop Replaced the LongStream implementation in isPrime with a standard primitive for-loop to eliminate stream allocation overhead. Fixed an underlying off-by-one bounds issue to correctly identify non-primes (like perfect squares 4 and 9) by using `<= limit`. Tested benchmark shows significant performance improvement. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- src/PrimeToolKit.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/PrimeToolKit.java b/src/PrimeToolKit.java index dc543dc..2a92dad 100644 --- a/src/PrimeToolKit.java +++ b/src/PrimeToolKit.java @@ -5,7 +5,12 @@ public class PrimeToolKit { // From exercise 003 static boolean isPrime(long number) { - return number > 1 && range(2, Math.round(Math.sqrt(number))).noneMatch(i -> number % i == 0); + if (number <= 1) return false; + long limit = Math.round(Math.sqrt(number)); + for (long i = 2; i <= limit; i++) { + if (number % i == 0) return false; + } + return true; } // From exercise 007