View Single Post
Join Date: Dec 2008
Posts: 1,451
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 188
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

need help with prime

 
0
  #1
Dec 29th, 2008
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:

  1. #include<iostream>
  2. #include<fstream>
  3. #include<cmath>
  4. #include<iomanip>
  5.  
  6. using namespace std;
  7.  
  8. void sieve(__int64 arry[], __int64 upto) // finds prime num.
  9. {
  10.  
  11.  
  12. int next(0);
  13.  
  14. for(__int64 i=0; i < upto; i++)//populate the arry starting from the value 2.
  15. arry[i] = i+2 ;
  16.  
  17.  
  18.  
  19. for(__int64 k=1; k < upto;k++)
  20. {
  21. if(arry[k] >=1)
  22. {
  23. next = arry[k];
  24.  
  25. for(int l = k+1 ; l <= upto ; l++)
  26.  
  27. if(arry[l] % next==0)
  28. arry[l]=0;
  29. }
  30. }
  31.  
  32. for(__int64 i=0;i<=upto;i++) //cout<< arry[] if it has a number greater than 0
  33.  
  34. {
  35. if(arry[i]>0)
  36. cout<<arry[i]<<endl;
  37. }
  38.  
  39.  
  40. }
  41.  
  42.  
  43.  
  44.  
  45.  
  46. int main()
  47. {
  48. __int64 upto =200; //This works but 2 million does not.
  49. __int64 arry[201]; //same problem as above.
  50.  
  51. sieve(arry,upto);
  52. return 0;
  53. }

thanks
Last edited by firstPerson; Dec 29th, 2008 at 3:20 pm.
Reply With Quote