From 1c341f29f0f9da93001d3bde8b4a0bd14d65c499 Mon Sep 17 00:00:00 2001 From: Artemis Git Integration Date: Thu, 23 Oct 2025 15:45:44 +0100 Subject: [PATCH] feat: Implement prime number utilities --- app/src/main/java/algorithms/Primes.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/algorithms/Primes.java b/app/src/main/java/algorithms/Primes.java index bfa903e..974e3cd 100644 --- a/app/src/main/java/algorithms/Primes.java +++ b/app/src/main/java/algorithms/Primes.java @@ -21,10 +21,10 @@ public static boolean IsPrime(int n) { } /** - * Sums all prime numbers from 0 to n + * Sums all prime numbers less than n. * - * @param n The number of prime numbers to sum. - * @return The sum of the first n prime numbers. + * @param n The upper limit (exclusive) for summing prime numbers. + * @return The sum of all prime numbers less than n. */ public static int SumPrimes(int n) { int sum = 0; @@ -46,14 +46,16 @@ public static Vector PrimeFactors(int n) { Vector ret = new Vector(); for (int i = 2; i * i <= n; i++) { // Optimized loop condition - while (n % i == 0 && IsPrime(i)) { // Optimized to handle repeated factors + // A factor 'i' found at this point is guaranteed to be prime + // because any composite factors smaller than 'i' would have already divided 'n'. + while (n % i == 0) { // Removed redundant IsPrime(i) check for performance ret.add(i); n /= i; // Reduce n to avoid redundant checks. } } - if (n > 1) { // Add any remaining prime factor. + if (n > 1) { // Add any remaining prime factor. This covers the case where n itself is a prime greater than 1. ret.add(n); } return ret; } -} +} \ No newline at end of file