Prime number generator giving bad output

Hi everybody!
I was the "Prime Number Generator" from SPOJ(Sphere Online judge)
(Link here: http://www.spoj.com/problems/PRIME1/)

I wrote the following code:

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
26
27
28
29
30
31
32
33
34
#include <iostream>

using namespace std;

int main()
{
	int t = 0;
	cin >> t;
	
	for(int x = 0; x < t; x++){
		int max = 0;
		int min = 0;
		
		cin >> min >> max;
			for (int y = min; y <= max; y++){
				bool isPrime = true;
				for (int z = 2; z <= y; z++){
					if(y % z == 0){
						isPrime = false;
						break;
					}
					if(y == 1){
						isPrime = false;
						break;
					}
				}
				if(isPrime){
					cout << y << "\n";
					isPrime = false;
				}
			}
		
	}
}


I will explain its logic:

When the program starts, it asks the user for the number of times he/she/xe wants to generate prime numbers. This is done by the for loop on line 10.

Nested inside this for loop, we have another for loop on line 15. This for loop is associated with calculating the primes.
As soon as this loop starts, it assumes the number to be prime.


Nested inside this loop is the 3rd for loop on line 17 This loop divides the number held by variable y with 2 up to itself. If the modulus result is zero, it marks the number as a non-prime and terminates the loop. I also added a test case that if the number (in y) is 1, it will mark y as a non-prime and terminate the loop.

Then, it will check wether the number has been marked prime, if so, it will print it.
And it will continue iterating,

Oh, and for the output:
I was expecting:

2 [INPUT]
1 10 [INPUT]
2
3
5
7
3 5 [INPUT]
3
5

But I got:

2 [INPUT]
1 10 [INPUT]
1
3 5 [INPUT]


Please help
and excuse my English.
> This loop divides the number held by variable y with 2 up to itself.
> If the modulus result is zero, it marks the number as a non-prime
a = a
a/a = 1
>> This loop divides the number held by variable y with 2 up to itself.
>> If the modulus result is zero, it marks the number as a non-prime
>a = a
>a/a = 1

I didn't quite get you. Please explain.
Line 17.

Consider what happens when y is 1. The loop conditions is z <= y, with z initially being 2 which makes the condition 2 <= 1. As we can see, this will never evaluate to true so 1 will always be considered prime.

Now, what happens in the loop when y is 2? According to the loop condition, the body of the loop can be entered when z is equal to y. Therefore, line 18 will evaluate to true when y is 2. (It will evaluate true for any number for which the body of the loop will be entered on the last iteration of the loop.)

Your program says that 1 is the only prime number.
Last edited on
Now, I have the following code:
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
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

using namespace std;

int main()
{
	int t = 0;
	cin >> t;
	
	for(int x = 0; x < t; x++){
		int max = 0;
		int min = 0;
		
		cin >> min >> max;
		if (min == 1)
			min++;
			
		for (int y = min; y <= max; y++){
			bool isPrime = true;
			for (int z = 2; z <= y; z++){
				if(y % z == 0 && y != 2){
					isPrime = false;
				//	break;
				}
			/*	if(y == 1){
					isPrime = false;
					break;
				} */
			}
			if(isPrime){
				cout << y << "\n";
				isPrime = false;
			}
		}
		
	}
}


It does not think that the prime number is one, but in place of 1, it outputs 2 and asks for input again.
The break on line makes no difference included or commented (or so it seems I get same output with both.)
Last edited on
Again, your loop condition on line 20 allows z to be equal to y. In the body of the function you say that if (y%z == 0 && y!=2) then the number is not prime. But since z can be equal to y, on the last iteration of the loop that condition will always be equivalent to: if (z%z==0 && z!=2) where it should be obvious that every number that is not 2 will be seen as not prime.
It works now!

Final Code:
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
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

using namespace std;

int main()
{
	int t = 0;
	cin >> t;
	
	for(int x = 0; x < t; x++){
		int max = 0;
		int min = 0;
		
		cin >> min >> max;
		if (min == 1)
			min++;
			
		for (int y = min; y <= max; y++){
			bool isPrime = true;
			for (int z = 2; z < y; z++){
				if(y % z == 0 && y != 2){
					isPrime = false;
				//	break;
				}
			/*	if(y == 1){
					isPrime = false;
					break;
				} */
			}
			if(isPrime){
				cout << y << "\n";
				isPrime = false;
			}
		}
		
	}
}


Thanks Everyone!
Topic archived. No new replies allowed.