public class PrimeNumbers { public static void main (String b[]) { int count = 1; int x = 3; while (count < 1000000) { boolean a=true; int q = (int)Math.sqrt(x); //We can simply stop division once we hit the square root of x. Otherwise, x would have been divisible by a number less that the square root of x for (int y=3; y <= q; y += 2) //The only even prime number is 2. Can increment x by 2, so x can only be odd { if (x%y==0) // Primes can be divided by an even number if and only if they are even, but x cannot be even ==> y can be incremented by 2 too ==> y can only be odd. { a=false; break; } } if (a) { System.out.println(x); ++count; } x += 2; } } }
Also, I wanted to make it so you could put in a command line input to specify how many prime numbers you wanted to add, I was thinking like adding. "(String args[])" to the main method and making it something like "while count < args[0]{ as the counter.
Any help would be greatly appreciated, thanks
