Skip to content

Commit 288f47e

Browse files
committed
One more solution and small fix for AmountOfPrimeNumbers
1 parent 5287a34 commit 288f47e

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/main/java/by/andd3dfx/numeric/AmountOfPrimeNumbers.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class AmountOfPrimeNumbers {
1212

1313
public static int determine_usingPrimeDividersOfNumberSolution(int n) {
1414
int result = 0;
15-
for (int i = 1; i < n; i++) {
15+
for (int i = 2; i < n; i++) {
1616
if (isPrime(i)) {
1717
result++;
1818
}
@@ -24,6 +24,25 @@ private static boolean isPrime(int number) {
2424
return PrimeDividersOfNumber.determine(number).length == 1;
2525
}
2626

27+
public static int determine_usingCustomIsPrimeWithEarlyReturn(int n) {
28+
int result = 0;
29+
for (int i = 2; i < n; i++) {
30+
if (isPrimeEnhanced(i)) {
31+
result++;
32+
}
33+
}
34+
return result;
35+
}
36+
37+
private static boolean isPrimeEnhanced(int number) {
38+
for (int i = 2; i <= Math.sqrt(number); i++) {
39+
if (number % i == 0) {
40+
return false;
41+
}
42+
}
43+
return true;
44+
}
45+
2746
public static int determine_usingEratosthenesSieve(int n) {
2847
var isPrime = new boolean[n];
2948
Arrays.fill(isPrime, 2, n, true);

src/test/java/by/andd3dfx/numeric/AmountOfPrimeNumbersTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ public void testDetermine_usingPrimeDividersOfNumberSolution() {
1313
check(AmountOfPrimeNumbers::determine_usingPrimeDividersOfNumberSolution);
1414
}
1515

16+
@Test
17+
public void testDetermine_usingCustomIsPrimeWithEarlyReturn() {
18+
check(AmountOfPrimeNumbers::determine_usingCustomIsPrimeWithEarlyReturn);
19+
}
20+
1621
@Test
1722
public void testDetermine_usingEratosthenesSieve() {
1823
check(AmountOfPrimeNumbers::determine_usingEratosthenesSieve);

0 commit comments

Comments
 (0)