943,733 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 685
  • C++ RSS
Dec 29th, 2008
0

need help with prime

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Dec 29th, 2008
0

Re: need help with prime

Yeay...maks array size is 65535 or 65536...i dont know exactly...
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Dec 29th, 2008
0

Re: need help with prime

what???
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Dec 29th, 2008
0

Re: need help with prime

ur using 32 bit compiler rit...

see this...
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Dec 29th, 2008
0

Re: need help with prime

Click to Expand / Collapse  Quote originally posted by cikara21 ...
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.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Dec 29th, 2008
0

Re: need help with prime

Static memory space is generally very limited, if you want to create large array sizes then you need to use dynamic memory.
C++ Syntax (Toggle Plain Text)
  1. __int64 *array = new __int64[2000000];
But be sure to delete the array afterwards
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Dec 29th, 2008
0

Re: need help with prime

Static memory space is generally very limited, if you want to create large array sizes then you need to use dynamic memory.
C++ Syntax (Toggle Plain Text)
  1. __int64 *array = new __int64[2000000];
But be sure to delete the array afterwards
C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Dec 30th, 2008
0

Re: need help with prime

new allocates dynamic memory (did i choose the right words?).
Reputation Points: 36
Solved Threads: 5
Newbie Poster
da penguin is offline Offline
18 posts
since Dec 2008
Dec 30th, 2008
0

Re: need help with prime

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?
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Dec 30th, 2008
0

Re: need help with prime

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
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Excercise in class design - code review
Next Thread in C++ Forum Timeline: toolTip1 for textBox





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC