| | |
need help with prime
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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:
thanks
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:
C++ Syntax (Toggle Plain Text)
#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
Last edited by firstPerson; Dec 29th, 2008 at 3:20 pm.
ur using 32 bit compiler rit...
see this...
see this...
C++ Syntax (Toggle Plain Text)
__int64 arry[201]; // in 32 bit compiler will be int arry[201]; // maks array size = 65535 __int64 arry[2000000]; // ?????
Last edited by cikara21; Dec 29th, 2008 at 3:43 pm.
I mean that when i try to find the all prime numbers below 2 million,
the program corrupts, but if i try to find all prime number below say 2000, it works perfectly. Try the program to see what i mean.
I don't get why it does not work for a large number.
the program corrupts, but if i try to find all prime number below say 2000, it works perfectly. Try the program to see what i mean.
I don't get why it does not work for a large number.
Static memory space is generally very limited, if you want to create large array sizes then you need to use dynamic memory. But be sure to delete the array afterwards
Chris
C++ Syntax (Toggle Plain Text)
__int64 *array = new __int64[2000000];
C++ Syntax (Toggle Plain Text)
delete []array; array = NULL; // to make sure you do not use the pointer
Chris
Last edited by Freaky_Chris; Dec 29th, 2008 at 3:59 pm.
Knowledge is power -- But experience is everything
•
•
•
•
Static memory space is generally very limited, if you want to create large array sizes then you need to use dynamic memory.But be sure to delete the array afterwardsC++ Syntax (Toggle Plain Text)
__int64 *array = new __int64[2000000];C++ Syntax (Toggle Plain Text)
delete []array; array = NULL; // to make sure you do not use the pointer
Chris
![]() |
Similar Threads
- prime numbers (C++)
- Recursive prime number f(x) (C)
Other Threads in the C++ Forum
- Previous Thread: Excercise in class design - code review
- Next Thread: toolTip1 for textBox
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






