Skip to content

Commit

Permalink
sample for BAEL-747 Check if a number is prime in Java (eugenp#1508)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanaulla123 authored and maibin committed Mar 26, 2017
1 parent 6322aa3 commit e6836c0
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 0 deletions.
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);
}

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


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


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

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

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



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

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

}

0 comments on commit e6836c0

Please sign in to comment.