import java.util.stream.IntStream; // runs in < 300 ms public class Primes { public static boolean isAPrime(int num) { if (num % 2 == 0) return num == 2; int maxtest = (int) Math.sqrt(num); return IntStream.iterate(3, i -> i <= maxtest, i -> 2 + i).noneMatch(divider -> (num % divider) == 0); } }
- import java.util.stream.IntStream;
- // runs in < 300 ms
- public class Primes {
- public static boolean isAPrime(int num) {
return IntStream.range(2, (num/2 +1)).noneMatch(divider -> (num % divider) == 0);- if (num % 2 == 0) return num == 2;
- int maxtest = (int) Math.sqrt(num);
- return IntStream.iterate(3, i -> i <= maxtest, i -> 2 + i).noneMatch(divider -> (num % divider) == 0);
- }
- }
// this is pretty fast: < 100 ms public class Primes { public static boolean isAPrime(int number) { if (number < 2 || (number % 2 == 0 && number != 2)) return false; int endvalue = (int) Math.sqrt(number); for (int i = 3; i <= endvalue; i += 2) { if (number % i == 0) return false; } return true; } }
- // this is pretty fast: < 100 ms
- public class Primes {
- public static boolean isAPrime(int number) {
- if (number < 2 || (number % 2 == 0 && number != 2)) return false;
for (int i = 3; i*i <= number; i += 2) {- int endvalue = (int) Math.sqrt(number);
- for (int i = 3; i <= endvalue; i += 2) {
- if (number % i == 0) return false;
- }
- return true;
- }
- }
public class Primes { public static boolean isAPrime(int number) { if (number < 2 || (number % 2 == 0 && number != 2)) return false; for (int i = 3; i*i <= number; i += 2) { if (number % i == 0) return false; } return true; } }
- public class Primes {
- public static boolean isAPrime(int number) {
if(number > 1 && number == 2) return true; //1 is not a prime number by definitionelse {for(int i = 3; i*i < number; i +=2) {if (number % i == 0) return false;}- if (number < 2 || (number % 2 == 0 && number != 2)) return false;
- for (int i = 3; i*i <= number; i += 2) {
- if (number % i == 0) return false;
- }
- return true;
- }
- }
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; boolean actual = Primes.isAPrime(numberToCheck); assertEquals(expected, actual); } @Test public void fortySevenShouldBeAPrime() { int numberToCheck = 47; boolean expected = true; boolean actual = Primes.isAPrime(numberToCheck); assertEquals(expected, actual); } @Test public void nineShouldNotBeAPrime() { int numberToCheck = 9; boolean expected = false; boolean actual = Primes.isAPrime(numberToCheck); assertEquals(expected, actual); } @Test public void n123ShouldNotBeAPrime() { int numberToCheck = 123; boolean expected = false; boolean actual = Primes.isAPrime(numberToCheck); assertEquals(expected, actual); } @Test public void n91ShouldNotBeAPrime() { int numberToCheck = 91; boolean expected = false; boolean actual = Primes.isAPrime(numberToCheck); assertEquals(expected, actual); } @Test public void n17977ShouldBeAPrime() { int numberToCheck = 17977; boolean expected = true; boolean actual = Primes.isAPrime(numberToCheck); assertEquals(expected, actual); } // better tests from @spacebison @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); } }
- 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;
- boolean actual = Primes.isAPrime(numberToCheck);
- assertEquals(expected, actual);
- }
@Test- @Test
- public void fortySevenShouldBeAPrime() {
- int numberToCheck = 47;
- boolean expected = true;
- boolean actual = Primes.isAPrime(numberToCheck);
- assertEquals(expected, actual);
- }
- @Test
- public void nineShouldNotBeAPrime() {
- int numberToCheck = 9;
- boolean expected = false;
- boolean actual = Primes.isAPrime(numberToCheck);
- assertEquals(expected, actual);
- }
- @Test
- public void n123ShouldNotBeAPrime() {
- int numberToCheck = 123;
- boolean expected = false;
- boolean actual = Primes.isAPrime(numberToCheck);
- assertEquals(expected, actual);
- }
- @Test
- public void n91ShouldNotBeAPrime() {
- int numberToCheck = 91;
- boolean expected = false;
- boolean actual = Primes.isAPrime(numberToCheck);
- assertEquals(expected, actual);
- }
- @Test
- public void n17977ShouldBeAPrime() {
- int numberToCheck = 17977;
- boolean expected = true;
- boolean actual = Primes.isAPrime(numberToCheck);
- assertEquals(expected, actual);
- }
- // better tests from @spacebison
- @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);
- }
- }