Print Prime Numbers(using for loop)

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Print Prime Numbers(using for loop)

 
0
  #11
Apr 26th, 2006
Ah it's just occured to me what Salem meant by square root !

A revised edition:

  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. static const int MAX = 500;
  7. static const int MIN = 0;
  8. static const int MAXCOL = 10;
  9.  
  10. bool isPrime(int r);
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14. int col = 0;
  15. int n;
  16.  
  17. for(n=MIN; n<=MAX; n++)
  18. if(isPrime(n)){
  19. cout << n << '\t';
  20. col++;
  21. if(col == MAXCOL){
  22. cout << '\n';
  23. col = 0;
  24. }
  25. }
  26.  
  27. system("PAUSE");
  28. return 0;
  29.  
  30. }
  31.  
  32. bool isPrime(int n)
  33. {
  34. if((n == 0)||(n == 1))
  35. return false;
  36.  
  37. int factors = 2;
  38.  
  39. for(int i=2; i<=((int)sqrt((double)n)); i++){
  40. if(n%i == 0){
  41. factors++;
  42. break;
  43. }
  44. }
  45.  
  46. if(factors == 2)
  47. return true;
  48.  
  49. return false;
  50. }

Now if only I could make i++ step in primes!
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Print Prime Numbers(using for loop)

 
0
  #12
Apr 26th, 2006
>can anyone better it?

Yes, there are more faster prime number finding Al-Gore-it-hims out there.
http://en.wikipedia.org/wiki/Sieve_of_Atkin

Note I said faster as opposed to efficient. An efficient prime number list finding strategy would be rather elusive.

Also, I'm sure you can think of a much better way to pause the program than using system("PAUSE");?

:lol:
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Print Prime Numbers(using for loop)

 
0
  #13
Apr 26th, 2006
Originally Posted by iamthwee

Also, I'm sure you can think of a much better way to pause the program than using system("PAUSE");?

:lol:
Ok whats wrong with system("PAUSE") ?

I
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 26
Reputation: dude543 is an unknown quantity at this point 
Solved Threads: 0
dude543 dude543 is offline Offline
Light Poster

Re: Print Prime Numbers(using for loop)

 
1
  #14
Apr 26th, 2006
I dont know that high math, but I think I can do better.

  1. bool isPrime(int n)
  2. {
  3. bool prime ;
  4. int lim;
  5.  
  6. if ( n == 0 || n == 1 || n % 2 == 0)
  7. return(false);
  8.  
  9. prime = true;
  10. lim = (int)sqrt(n);
  11.  
  12. for(int i=3; i<= lim && prime ; i+=2)
  13. prime = n % i;
  14. return(prime);
  15. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Print Prime Numbers(using for loop)

 
1
  #15
Apr 26th, 2006
> for(int i=2; i<=((int)sqrt((double)n)); i++)
And it would be so much quicker if you didn't call sqrt() on every iteration of the loop!
n is constant (in this function), so it's root is constant also. Calculate it once and store in another variable to compare against.

Also, since 2 is prime, that's an easy case to get rid of, and you can start at 3.

Also, if you start at 3, then you can do i+=2 to only check all the odd numbers from there on.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Print Prime Numbers(using for loop)

 
0
  #16
Apr 26th, 2006
Dude543 yes I like that very much.

Salem good feedback.


So system("PAUSE") then, what gives ? I havn't found anything negative on the internet yet.

Ok I've found it:

http://www.gidnetwork.com/b-61.html
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Print Prime Numbers(using for loop)

 
0
  #17
Apr 26th, 2006
Actually dud543 I think this is wrong:|| n % 2 == 0)

That would return false for 2 which is prime.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 26
Reputation: dude543 is an unknown quantity at this point 
Solved Threads: 0
dude543 dude543 is offline Offline
Light Poster

Re: Print Prime Numbers(using for loop)

 
0
  #18
Apr 27th, 2006
Well. I also think that it is wrong.
But poor me, does'nt know wheter 0,1,2 are prime.
I know that prime will divide by himself and one only.
I Guess you are right.
Anyway. is there someway for me to edit the post ?
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Print Prime Numbers(using for loop)

 
0
  #19
Apr 27th, 2006
you can't edit a post more than 30 minutes after you posted in this forum.

0 and 1 are not prime. Yes 1 is divisible by 1 and itself, but that's the same thing it's only one factor not two factors so it's not prime.

2 is a special case, it is the only even number that is a prime.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 490
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Print Prime Numbers(using for loop)

 
0
  #20
Apr 28th, 2006
I wouldn't call 2 a special case - it fits the general description perfectly; A number evenly divisible only by 1 and itself.
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