Move History

Fork Selected
  • Code
    public class Primes {
      public static boolean isAPrime(int number) {    
        if (number == 1) {
          return false;
        }
      
        if (number == 2) {
          return true;
        }
        
        if (number % 2 == 0) {
          return false;
        }
      
        for (int i = 3; i*i <= number; i += 2) {
          if (number % i == 0)
            return false;
        }
        
        return true;
      }
    }
    Test Cases
    import org.junit.Test;
    import static org.junit.Assert.assertEquals;
    import org.junit.runners.JUnit4;
    import java.util.Random;
    
    public class SolutionTest {
        private static final Random RANDOM = new Random();
    
        @Test
        public void twoShouldBeAPrime() {
          testPrime(2, true);
        }
    
        @Test
        public void fourShouldNotBeAPrime() {
          testPrime(4, false);
        }
        
        @Test
        public void aSquareShouldNotAPrime() {
          for (int i = 0; i < 10000; ++i) {
            int randomNumber = RANDOM.nextInt(Short.MAX_VALUE) + 2;
            int numberToCheck = randomNumber * randomNumber;
            testPrime(numberToCheck, false);
          }
        }
        
        @Test
        public void aProductOfTwoIntegersShouldNotBeAPrime() {
          for (int i = 0; i < 100000; ++i) {
            int a = RANDOM.nextInt(Short.MAX_VALUE) + 2;
            int b = RANDOM.nextInt(Short.MAX_VALUE) + 2;
            int numberToCheck = a * b;
            testPrime(numberToCheck, false);
          }
        }
        
        private static void testPrime(int numberToCheck, boolean expected) {     
          boolean actual = Primes.isAPrime(numberToCheck);
          assertEquals(Integer.toString(numberToCheck), expected, actual);
        }
    }
  • Code
    • public class Primes {
    • public static boolean isAPrime(int number) {
    • for (int i=2;i*i<=number;i++)
    • {
    • public static boolean isAPrime(int number) {
    • if (number == 1) {
    • return false;
    • }
    • if (number == 2) {
    • return true;
    • }
    • if (number % 2 == 0) {
    • return false;
    • }
    • for (int i = 3; i*i <= number; i += 2) {
    • if (number % i == 0)
    • return false;
    • }
    • return true;
    • }
    • }
    Test Cases
    • import org.junit.Test;
    • import static org.junit.Assert.assertEquals;
    • import org.junit.runners.JUnit4;
    • import java.util.Random;
    • public class SolutionTest {
    • private static final Random RANDOM = new Random();
    • @Test
    • public void twoShouldBeAPrime() {
    • int numberToCheck = 2;
    • boolean expected = true;
    • testPrime(2, true);
    • }
    • @Test
    • public void fourShouldNotBeAPrime() {
    • testPrime(4, false);
    • }
    • @Test
    • public void aSquareShouldNotAPrime() {
    • for (int i = 0; i < 10000; ++i) {
    • int randomNumber = RANDOM.nextInt(Short.MAX_VALUE) + 2;
    • int numberToCheck = randomNumber * randomNumber;
    • testPrime(numberToCheck, false);
    • }
    • }
    • @Test
    • public void aProductOfTwoIntegersShouldNotBeAPrime() {
    • for (int i = 0; i < 100000; ++i) {
    • int a = RANDOM.nextInt(Short.MAX_VALUE) + 2;
    • int b = RANDOM.nextInt(Short.MAX_VALUE) + 2;
    • int numberToCheck = a * b;
    • testPrime(numberToCheck, false);
    • }
    • }
    • private static void testPrime(int numberToCheck, boolean expected) {
    • boolean actual = Primes.isAPrime(numberToCheck);
    • assertEquals(expected, actual);
    • assertEquals(Integer.toString(numberToCheck), expected, actual);
    • }
    • }