Identify Prime numbers

Question: Write a program to read a number N from the user and then find the first N primes. A prime number is a number that only has two divisors, one and itself.

Instead of my code printing out a sequence of prime numbers, it repeats some of the prime numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  int main(){

	int N;
	cin >> N;

	for (int i = 2; i < N; i++){
		for (int j = 2; j < N; j++){

			if (i%j ==0){
				break;

			}
			else
				cout << i << " " << "is a prime number\n";
			
		}
		
			
	}
	

}
“cout" is the loop .it should be this
int main(){

int N;
cin >> N;

for (int i = 2; i < N; i++){
int j;
for (j = 2; j < N; j++){

if (i%j ==0){
break;
}
}
if(j==N)cout << i << " " << "is a prime number\n";

}


}
I am using phone device, so it do not look nice
Close, but not quite.

First of all, that code doesn't output anything.
You can fix it by changing N to i in the second for loop and also in the if(j==N).

Then that code will only print the prime numbers up to (but not including) N.

The OP wants the first N primes.

So instead, use another variable to keep track of how many primes have been found so far, and use that instead of i < N as the condition in the outer for loop.
You should really use a prime sieve especially if n is a large number. Doing a sieve you want to use a std::vector<bool>.
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

Here is a very basic example that isn't fully optimized which you may need if you want a verrry large range of primes within a timely manner and with restricted memory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
unsigned const primes = 100;
std::vector<bool> sieve(primes+1, true);

for(int i = 2; i < std::sqrt(primes); ++i)
{
    if(primes.at(i))
    {
        for(int j = i*i; j <= primes; j += i)
        {
            sieve.at(j) = false;
        }
    }
}

std::cout << "The primes less than or equal to " << primes << " are: " << std::endl;
std::cout << 2 << ' ';
for(int i = 3; i <= primes; i += 2)
{
    if(sieve.at(i))
    {
        std::cout << i << ' ';
    }
}

std::cout << std::endl;


*edit I misread it wants the first n primes not primes up to n. Basically you want to use a modified version of the prime number theorem to find the number that has the n number of primes less than it. Then sieve up to that number and output all the primes (You can use a counter to double check but the theorem should be pretty accurate).
Last edited on
Can I have further explanation? I still don't understand what is wrong with my code
1
2
3
4
5
6
			if (i%j ==0){
				break;

			}
			else
				cout << i << " " << "is a prime number\n";
This outputs after the first iteration in j. You should assign a variable to false if it breaks. Then outside the inner loop if it is still prime (true) output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
        bool prime = true;
	for (int i = 2; i < N; i++){
                prime = true;
		for (int j = 2; j < N; j++){

			if (i%j ==0){
				break;
                                prime = false;
			}
		}
                if(prime)
                    cout << i << " " << "is a prime number\n";
		
			
	}


Your indentation is kind of hard for me to read. I tried to match yours.
Topic archived. No new replies allowed.