sample for BAEL-747 Check if a number is prime in Java (#1508)

This commit is contained in:
Mohamed Sanaulla 2017-03-26 23:57:36 +03:00 committed by maibin
parent 6322aa350f
commit e6836c01c7
9 changed files with 151 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.primechecker;
public interface PrimeChecker {
public boolean isPrime( int number );
}

View File

@ -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);
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}