class FindPrimes { public static void main(String arguments[]) { int numPrimes = 0; // candidate: the number that might be prime int candidate = 2; System.out.println("First 400 primes:"); while (numPrimes < 400) { if (isPrime(candidate)) { System.out.print(candidate + " "); numPrimes++; } candidate++; } } public static boolean isPrime(int checkNumber) { double root = Math.sqrt(checkNumber); // check every number from 2 to the square // root of checkNumber, to see if any of them // produce a remainder of 0 when checkNumber // is divided by the number // Any have a remainder of 0? Not prime // None? Prime for (int i = 2; i <= root; i++) { if (checkNumber % i == 0) return false; } return true; } }