forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sample for BAEL-747 Check if a number is prime in Java (eugenp#1508)
- Loading branch information
1 parent
6322aa3
commit e6836c0
Showing
9 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
core-java/src/main/java/com/baeldung/primechecker/BigIntegerPrimeChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.baeldung.primechecker; | ||
|
||
import java.math.BigInteger; | ||
|
||
public class BigIntegerPrimeChecker implements PrimeChecker{ | ||
|
||
@Override | ||
public boolean isPrime(int number) { | ||
BigInteger bigInt = BigInteger.valueOf(number); | ||
return bigInt.isProbablePrime(100); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
core-java/src/main/java/com/baeldung/primechecker/BruteForcePrimeChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.baeldung.primechecker; | ||
|
||
import java.util.stream.IntStream; | ||
|
||
public class BruteForcePrimeChecker implements PrimeChecker{ | ||
|
||
@Override | ||
public boolean isPrime(int number) { | ||
return IntStream.range(2, number).filter(n -> (number % n == 0)).count() == 0; | ||
} | ||
|
||
|
||
} |
15 changes: 15 additions & 0 deletions
15
core-java/src/main/java/com/baeldung/primechecker/OptimisedPrimeChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.baeldung.primechecker; | ||
|
||
import java.util.stream.IntStream; | ||
|
||
public class OptimisedPrimeChecker implements PrimeChecker{ | ||
|
||
@Override | ||
public boolean isPrime(int number) { | ||
return IntStream.range(2, (int)Math.sqrt(number) + 1) | ||
.filter(n -> (number % n == 0)) | ||
.count() == 0; | ||
} | ||
|
||
|
||
} |
6 changes: 6 additions & 0 deletions
6
core-java/src/main/java/com/baeldung/primechecker/PrimeChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.baeldung.primechecker; | ||
|
||
public interface PrimeChecker { | ||
|
||
public boolean isPrime( int number ); | ||
} |
12 changes: 12 additions & 0 deletions
12
core-java/src/main/java/com/baeldung/primechecker/PrimesPrimeChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.baeldung.primechecker; | ||
|
||
import org.apache.commons.math3.primes.Primes; | ||
|
||
public class PrimesPrimeChecker implements PrimeChecker{ | ||
|
||
@Override | ||
public boolean isPrime(int number) { | ||
return Primes.isPrime(number); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
core-java/src/test/java/com/baeldung/primechecker/BigIntegerPrimeCheckerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.baeldung.primechecker; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.Test; | ||
|
||
public class BigIntegerPrimeCheckerTest { | ||
|
||
PrimeChecker primeChecker = new BigIntegerPrimeChecker(); | ||
|
||
@Test | ||
public void givenPrimeNumber_whenCheckIsPrime_thenTrue(){ | ||
assertTrue(primeChecker.isPrime(13)); | ||
assertTrue(primeChecker.isPrime(1009)); | ||
} | ||
|
||
@Test | ||
public void givenNonPrimeNumber_whenCheckIsPrime_thenFalse(){ | ||
assertTrue(!primeChecker.isPrime(50)); | ||
assertTrue(!primeChecker.isPrime(1001)); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
core-java/src/test/java/com/baeldung/primechecker/BruteForcePrimeCheckerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.baeldung.primechecker; | ||
|
||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
|
||
public class BruteForcePrimeCheckerTest { | ||
|
||
BruteForcePrimeChecker primeChecker = new BruteForcePrimeChecker(); | ||
|
||
@Test | ||
public void givenPrimeNumber_whenCheckIsPrime_thenTrue(){ | ||
assertTrue(primeChecker.isPrime(13)); | ||
assertTrue(primeChecker.isPrime(1009)); | ||
} | ||
|
||
@Test | ||
public void givenNonPrimeNumber_whenCheckIsPrime_thenFalse(){ | ||
assertTrue(!primeChecker.isPrime(50)); | ||
assertTrue(!primeChecker.isPrime(1001)); | ||
} | ||
|
||
|
||
|
||
} |
23 changes: 23 additions & 0 deletions
23
core-java/src/test/java/com/baeldung/primechecker/OptimisedPrimeCheckerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.baeldung.primechecker; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.Test; | ||
|
||
public class OptimisedPrimeCheckerTest { | ||
|
||
PrimeChecker primeChecker = new OptimisedPrimeChecker(); | ||
|
||
@Test | ||
public void givenPrimeNumber_whenCheckIsPrime_thenTrue(){ | ||
assertTrue(primeChecker.isPrime(13)); | ||
assertTrue(primeChecker.isPrime(1009)); | ||
} | ||
|
||
@Test | ||
public void givenNonPrimeNumber_whenCheckIsPrime_thenFalse(){ | ||
assertTrue(!primeChecker.isPrime(50)); | ||
assertTrue(!primeChecker.isPrime(1001)); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
core-java/src/test/java/com/baeldung/primechecker/PrimesPrimeCheckerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.baeldung.primechecker; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.Test; | ||
|
||
public class PrimesPrimeCheckerTest { | ||
PrimeChecker primeChecker = new PrimesPrimeChecker(); | ||
|
||
@Test | ||
public void givenPrimeNumber_whenCheckIsPrime_thenTrue() { | ||
assertTrue(primeChecker.isPrime(13)); | ||
assertTrue(primeChecker.isPrime(1009)); | ||
} | ||
|
||
@Test | ||
public void givenNonPrimeNumber_whenCheckIsPrime_thenFalse() { | ||
assertTrue(!primeChecker.isPrime(50)); | ||
assertTrue(!primeChecker.isPrime(1001)); | ||
} | ||
|
||
} |