prime numbers homework trouble

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 5
Reputation: bluegoo06 is an unknown quantity at this point 
Solved Threads: 0
bluegoo06 bluegoo06 is offline Offline
Newbie Poster

prime numbers homework trouble

 
0
  #1
Mar 9th, 2005
i have to define a function that tests number from 2 to 10000 to see if they are prime.
heres what i have so far.
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. bool find_prime(int);
  6.  
  7. int main ()
  8. {
  9. int i, counter =0;
  10. bool test;
  11.  
  12. for (i =2; i <= 500; i++)
  13. {
  14. test = find_prime(i);
  15. if ( test == true )
  16. {
  17. counter++;
  18. cout << setw(6) << i << "\t";
  19. }
  20. }
  21. cout << "\nThe total number of the prime numbers between 2 and " << i << " is " << counter << endl;
  22. return 0;
  23. }
  24.  
  25. bool find_prime (int m)
  26. {
  27. int temp, j;
  28. for ( j = 2; j < m - 1 ; j++ )
  29. {
  30. temp = m % j;
  31. if (temp == 0)
  32. {
  33. return false;
  34. break;
  35. }
  36. else
  37. {
  38. return true;
  39. break;
  40. }
  41. }}
but it only tests prime numbers until about 500 or so then it gets most of the numbers to be prime but it still includes non prime numbers for some reason. ANy ideaS?
thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: prime numbers homework trouble

 
0
  #2
Mar 9th, 2005
Originally Posted by bluegoo06
i have to define a function that tests number from 2 to 10000 to see if they are prime.
for (i =2; i <= 500; i++)
but it only tests prime numbers until about 500 or so
That's what you tell it to do.

Originally Posted by bluegoo06
then it gets most of the numbers to be prime but it still includes non prime numbers for some reason. ANy ideaS?
You might try Google to see if there are any other algorithms to compare yours to.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 2
Reputation: samarth is an unknown quantity at this point 
Solved Threads: 0
samarth samarth is offline Offline
Newbie Poster

Re: prime numbers homework trouble

 
0
  #3
Mar 12th, 2005
Hi,

1) The "break" statement after the return does not have any effect
2) I think the logic of the program is incorrect and that may be the reason why the program returns non primes. Can u try the following code. I havent checked it but I think it should work


rgds
Sam

  1. bool find_prime (int m)
  2. {
  3. int temp, j;
  4. for ( j = 2; j < m - 1 ; j++ )
  5. {
  6. temp = m % j;
  7. if (temp == 0)
  8. {
  9. return false;
  10. }
  11. }
  12. return true;
  13. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC