need help with prime

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2008
Posts: 1,459
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: 189
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 Quick reply to this message  
Join Date: Jul 2008
Posts: 338
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 66
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: need help with prime

 
0
  #2
Dec 29th, 2008
Yeay...maks array size is 65535 or 65536...i dont know exactly...
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,459
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: 189
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: need help with prime

 
0
  #3
Dec 29th, 2008
what???
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 338
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 66
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: need help with prime

 
0
  #4
Dec 29th, 2008
ur using 32 bit compiler rit...

see this...
  1.  
  2. __int64 arry[201];
  3.  
  4. // in 32 bit compiler will be
  5. int arry[201];
  6.  
  7. // maks array size = 65535
  8.  
  9. __int64 arry[2000000];
  10.  
  11. // ?????
Last edited by cikara21; Dec 29th, 2008 at 3:43 pm.
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,459
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: 189
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: need help with prime

 
0
  #5
Dec 29th, 2008
Originally Posted by cikara21 View Post
what do u mean...ur using 32 bit compiler rit...
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: need help with prime

 
0
  #6
Dec 29th, 2008
Static memory space is generally very limited, if you want to create large array sizes then you need to use dynamic memory.
  1. __int64 *array = new __int64[2000000];
But be sure to delete the array afterwards
  1. delete []array;
  2. 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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,459
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: 189
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: need help with prime

 
0
  #7
Dec 29th, 2008
Originally Posted by Freaky_Chris View Post
Static memory space is generally very limited, if you want to create large array sizes then you need to use dynamic memory.
  1. __int64 *array = new __int64[2000000];
But be sure to delete the array afterwards
  1. delete []array;
  2. array = NULL; // to make sure you do not use the pointer

Chris
hmm. not sure what you mean. I know the basics about pointers but haven't learned what new is or does. Care to explain?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 18
Reputation: da penguin is an unknown quantity at this point 
Solved Threads: 5
da penguin's Avatar
da penguin da penguin is offline Offline
Newbie Poster

Re: need help with prime

 
0
  #8
Dec 30th, 2008
new allocates dynamic memory (did i choose the right words?).
No, ma'am, we are musicians.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,459
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: 189
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: need help with prime

 
0
  #9
Dec 30th, 2008
So it's like i'm telling the computer to make extra memory by saying

__int64 *array = new __int64[2000000];

Is it the 'new' that tells the computer to make extra room?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: need help with prime

 
0
  #10
Dec 30th, 2008
When you create a pointer to a type you reserve enough space in memory for the the variable. When you call new you actually allocate the memory to the program so it cannot be used by another program.

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 569 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC