hi,
I was trying to implement the sieve of eroathoses (I know I spelt this wrong) and it works. But i was trying to find all prime under 2 million.
The .cmd for visual studio just corrupts if I try to find all prime under 2million. I am guessing It's because overflow or something but I don't know for sure.
can you give me some hints on how to fix my code so the program won't corrupt. here is the code:
#include<iostream>
#include<fstream>
#include<cmath>
#include<iomanip>
using namespace std;
void sieve(__int64 arry[], __int64 upto) // finds prime num.
{
int next(0);
for(__int64 i=0; i < upto; i++)//populate the arry starting from the value 2.
arry[i] = i+2 ;
for(__int64 k=1; k < upto;k++)
{
if(arry[k] >=1)
{
next = arry[k];
for(int l = k+1 ; l <= upto ; l++)
if(arry[l] % next==0)
arry[l]=0;
}
}
for(__int64 i=0;i<=upto;i++) //cout<< arry[] if it has a number greater than 0
{
if(arry[i]>0)
cout<<arry[i]<<endl;
}
}
int main()
{
__int64 upto =200; //This works but 2 million does not.
__int64 arry[201]; //same problem as above.
sieve(arry,upto);
return 0;
}
thanks