Prime algorithm for SPOJ

Hey guys I'm doing this problem on SPOJ and basically when provided a beginning and end integer limit m and n respectively, it outputs all the prime in that range.

The specifics to the output is here:
http://www.spoj.com/problems/PRIME1/

I wrote my code and it runs perfectly see below
However, the system evaluates the answer as wrong.

Here's the 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int numberOfLines;
	int m; //beginning of prime numbers
	int n; //ending of prime numbers
	int x;
	int *arrayPtr;
	//Read in the numbers 
	std::cin >> numberOfLines;
	
	//Loop and create/delete array per line
	for (int i = 0; i < numberOfLines; i++){
		std::cin >> m >> n;
		arrayPtr = new int[(n-m)+1];
		int counter = 0;
	
		//Check condition m >= 1 && n >=1
		if (m >= 1 && n >= 1)
			
			//check second condition
			if ( m <= 1000000000 && n <= 1000000000)
				//check third condition
				if (n-m<= 100000){
					for (int x = m; x <= n; x++){
						if (x == 1){ //do nothing if 1
						}
						else if (x == 2 || x == 3 || x == 5 || x == 7 || x == 11){ 
							arrayPtr[counter] = x;
							counter++;
						}
						else if (x%2 == 0 || x%3 == 0 || x%5 == 0 || x%7 == 0 || x%11 == 0) 
							{}
						else
						{
							arrayPtr[counter] = x;		
							counter++;
						}
					}
				}
				
	for (int i = 0; i < counter; i++)
		std::cout << arrayPtr[i] << "\n";
	std::cout << "\n";
	delete arrayPtr;
	}	
	
	return 0;
}


When I input my test cases I get all the primes that are possible within that range:
http://ideone.com/wfgutK

I can't tell why but the SPOJ system keeps evaluating my answer as wrong.
:(
Last edited on
169 (13^2) is a composite number indivisible by 2, 3, 5, 7, or 11.
17^2 is also a composite number, as is 19^2.
In fact, there exists no finite list of primes sufficient to find all primes and no composites.
Last edited on
Ahhh I see I had it all wrong :S
I thought I was on to something... I'm just going to give up and read up on this Sieve of Eratosthese algorithm

Thanks for the correction! I couldn't have found it without you helios
Topic archived. No new replies allowed.