1

The statement is: Write a program that reads an integer N and prints the first N prime numbers.

public static void main(String[] args) 
{
    Scanner scan = new Scanner(System.in);

    int N = scan.nextInt();
    int x = 2;

    for(int i = 0; i <= N; i++)
    {
        int count = 0;

        for(int j = 1; j <= x; j++)
            if(x%j == 0)
                count++;

        if(count == 2)
            System.out.print(x + " ");

        x++;
    }
}

When I run this code it's not giving me the exact N numbers. For example for N=1 & 2 it's printing the first 2 prime numbers, for N = 3 & 4, it's printing the first 3 prime numbers, for N = 5 & 6, it's printing the first 4 prime numbers, and so on. What is the problem with this code?

Kj45
  • 79
  • 2
  • 2
  • 11

5 Answers5

6

I think that there are many flaws in your program to fix, so I decided to write a more simple, elegant program.

Scanner scan = new Scanner(System.in);
int N = Integer.parseInt( scan.nextLine());
int count = 0;
int num = 2;
while(count != N) { // while count!= number of prime numbers entered keep searching..
    boolean prime = true;// to determine whether the number is prime or not
    for (int i = 2; i <= Math.sqrt(num); i++) { //efficiency matters
        if (num % i == 0) {
            prime = false; // if number divides any other number its not a prime so set prime to false and break the loop.
            break;
        }

    }
    if (prime) {
        count++;
        System.out.println(num);
    }
    num++; see if next number is prime or not.
}
QuakeCore
  • 1,886
  • 2
  • 15
  • 33
2

Using dynamic programming:

Any number that is not prime is always divisible by at least one prime that comes before it in the number series. If any number doesn’t divide itself with all the prime numbers before it then that number is also prime

void printPrimeNumbers(int n) {
    ArrayList<Integer> primeNumbers = new ArrayList<>();
    primeNumbers.add(2);
    for (int i = 3; i < n; i+=2) { // skip over even numbers since they are not prime
        boolean isPrime = true;
        for (Integer prime : primeNumbers) { // check current prime numbers to see if it evenly divides into number
            if (i % prime == 0) { // when number is not prime
                isPrime = false;
                break; // optimization: stop checking when number is already not prime
            }
        }
        if (isPrime) {
            primeNumbers.add(i);
        }
    }
    System.out.println(primeNumbers);
}
Tim
  • 149
  • 1
  • 5
1
// Java Program to generate first 'N' Prime Numbers
public class prime
{
    public static void main(String[]args)
    {
        int count = 0, max_count = 100, i;
        System.out.println("First "+max_count+" Prime Numbers:");

        for(int num=1; count<max_count; num++)
        {
            for(i=2; num%i != 0; i++);

            if(i == num)
            {
                System.out.print(" "+num);
                count++;
            }
        }
    }
}
Pratik Patil
  • 3,662
  • 3
  • 31
  • 31
0

Others have writen better code than yours, I'll explain why your code doesn't work.

You're not searching the first N prime number, you're searching the prime number inferieur to N+2. In your code, x starts at 2, and goes up to N+2.

If you want to find the N first prime numbers, you must loop while N is not equals 0, and decrease N everytime you found a prime number.

(wait this question is from 2015 ... damn)

Asoub
  • 2,273
  • 1
  • 20
  • 33
0
public static void main(String... commandline_args)
{
  try
  { 
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Please enter the limit of prime nos");
        int limit=Integer.parseInt(br.readLine());
        int checkLimit=0;
        int check=0;
    outer:  for(int i=2;;i++){
            check=0;
            for(int j=1;j<=i;j++)
            {
                if(i%j==0)
                {
                    check++;

                }
            }
            if(check==2)
            {
                checkLimit++;
                System.out.println(i);
                if(checkLimit==limit) break outer;                  
            }               
        }
  }
  catch(Exception e) 
  {
    System.out.println(e);
  }
}
zx485
  • 28,498
  • 28
  • 50
  • 59
  • Welcome to SO. Surrounding your code by some explanation would seriously improve your answer. – zx485 Dec 31 '16 at 14:43