I think the problem is that you don't reset the starting prime:
int long sieve_prime = 2;
Currently that is outside your loop. On second thoughts... That's not the problem. Has this code been edited to incorporate the suggestions in Mats Petersson's answer? I just corrected the bad indentation.
Anyway, for the other part of your question, I suggest you use char
instead of int
for Num_Array
. There is no use using int
to store a boolean. By using char
you should be able to store about 4 times as many values in the same amount of memory (assuming your int
is 32-bit, which it probably is).
That means you could handle numbers up to almost 2 billion. Since you are using signed long
as your type instead of unsigned long
, that is approaching the numeric limits for your calculation anyway.
If you want to use even less memory, you could use std::bitset
, but be aware that performance could be significantly impaired.
By the way, you should declare your timing array at the top of main
:
float Time_Array[100];
Putting it inside the loop just before it is used is a bit whack.
Oh, and just in case you're interested, here is my own implementation of the sieve which, personally, I find easier to read than yours....
std::vector<char> isPrime( N, 1 );
for( int i = 2; i < N; i++ )
{
if( !isPrime[i] ) continue;
for( int x = i*2; x < N; x+=i ) isPrime[x] = 0;
}