c++ - The following prime generator code shows an error when i want prime numbers greater than 1000000.Why? -
the following prime generator code shows error when want prime numbers greater 1000000.why?? @ first seemed occur b'cuz of int changed long error still there.... technically speaking nit error program after running displays message "primegen.exe has stopped working"
#include <iostream> using namespace std; int main() { long int a,c,k,d; k=0; cin>>a; cin>>d; long int b[a]; b[a-1]=0; for(long int i=2;i<=a;i++) { for(long int j=2;j<=(i/2);j++) { c=1; if ( i%j!=0 ) { continue; } else { c=0; break; } } if (c!=0) { b[k]=i; //++k; } else b[k]=0; ++k; } for(long int i=d;i<a;i++) { if (b[i]!=0) { cout<<b[i]<<"\t"; } } cin.ignore(); cin.get(); return 0; }
defining static array won't in case. because you're not allowed declare such long static array in c++; might want try
int *b = new int [a];
declaring array size dynamically (i.e array size decided @ run time). code should work until variable 'a' exceeds range of int (–2,147,483,648 2,147,483,647).
Comments
Post a Comment