943,516 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 13570
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 6th, 2007
0

isPrime()

Expand Post »
I need to write a main() function, that loops over a bunch of
numbers from 0 to 100, calls another function called isPrime() for each of them, and print out the numbers that are prime.
I'm new to C++ and any help would be appreciated.
Last edited by forumposters; Jul 6th, 2007 at 9:08 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
forumposters is offline Offline
25 posts
since May 2006
Jul 6th, 2007
0

Re: isPrime()

>I'm new to C++ and any help would be appreciated.
At least start trying to solve your problem before asking for help. It makes us like you more and ignore you less.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 6th, 2007
0

Re: isPrime()

What narue said applies. But I'll help you a little: isPrime(int x)
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Jul 7th, 2007
0

Re: isPrime()

Here's what I've come up with so far:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. bool IsPrime(int num)
  4. {
  5. if(num == 0)
  6. return true;
  7.  
  8. num = abs(num);
  9.  
  10. for(int i = 2; i <= sqrt(num); i++)
  11. if(num % i == 0)
  12. return false;
  13.  
  14. return true;
  15. }
Last edited by ~s.o.s~; Jul 7th, 2007 at 3:38 am. Reason: Added code tags, learn to use them.
Reputation Points: 10
Solved Threads: 1
Light Poster
forumposters is offline Offline
25 posts
since May 2006
Jul 7th, 2007
0

Re: isPrime()

You've been around over a year and haven't read the Rules yet? You also need to read this. It will explain that you need to tell us what problem you are having so we don't have to guess -- among other things
Moderator
Reputation Points: 3275
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,716 posts
since May 2006
Jul 7th, 2007
0

Re: isPrime()

>Here's what I've come up with so far
Somehow I find it hard to believe that you can write IsPrime but not the main driver to print the primes in the range [0..100). I'm guessing you were given IsPrime and told to write main, which means you've come up with nothing so far. But, the hard part is done already. All you need to do is loop from 0 to 100 and call IsPrime on the current number. If it returns true, print the number:
C++ Syntax (Toggle Plain Text)
  1. for ( int i = 0; i < 100; i++ ) {
  2. if ( IsPrime ( i ) )
  3. cout<< i <<" is prime\n";
  4. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 7th, 2007
0

Re: isPrime()

Click to Expand / Collapse  Quote originally posted by Narue ...
>Here's what I've come up with so far
Somehow I find it hard to believe that you can write IsPrime but not the main driver to print the primes in the range [0..100). I'm guessing you were given IsPrime and told to write main, which means you've come up with nothing so far.
[/code]
LOL! If some people would put half of the effort that they put into "avoiding work" into something productive, they would have had the task acomplished by now!

If laziness is truely the mother of invention, we have quite a group of potential inventors here!
JRM
Reputation Points: 130
Solved Threads: 75
Practically a Master Poster
JRM is offline Offline
618 posts
since Oct 2006
Aug 28th, 2010
0
Re: isPrime()
This is more efficient.

#include <iostream>

bool IsPrime(int num)
{
if(num == 0)
return true;

num = abs(num);

if(num % 2 == 0) return true;

for(int i = 3; i <= sqrt(num); i+=2)
if(num % i == 0)
return false;

return true;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
soeja is offline Offline
2 posts
since Aug 2010
Aug 28th, 2010
-1
Re: isPrime()
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. bool IsPrime(int num)
  4. {
  5. if(num == 0)
  6. return true;
  7.  
  8. num = abs(num);
  9.  
  10. if(num % 2 == 0) return true;
  11.  
  12. for(int i = 3; i <= sqrt(num); i+=2)
  13. if(num % i == 0)
  14. return false;
  15.  
  16. return true;
  17. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
soeja is offline Offline
2 posts
since Aug 2010
Apr 11th, 2011
-1

IsPrime - optimized

Hi Soeja,
This is more efficient.. and for 4 , 6 , 8(i.e., even no) .. ur code snippet will give wrong ans..
  1. #include <iostream>
  2.  
  3. bool IsPrime (int num)
  4. {
  5. if ( 2 == num ) return true ;
  6. int l = sqrt(num) ;
  7. for(int i = 3; i <= l; i+=2)
  8. if ( 0 == ( num % i ) )
  9. return false;
  10.  
  11. return true;
  12. }
Last edited by Ezzaral; Jun 2nd, 2011 at 1:39 pm. Reason: Snipped blog link. Please confine personal links to your user signature.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
parameshyss is offline Offline
2 posts
since Apr 2011

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: Modal Dialog Box
Next Thread in C++ Forum Timeline: .def Files, dll, Linking Error





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


Follow us on Twitter


© 2011 DaniWeb® LLC