>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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
What narue said applies. But I'll help you a little: isPrime(int x)
sillyboy
Practically a Master Poster
686 posts since Mar 2007
Reputation Points: 85
Solved Threads: 64
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
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
>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:
for ( int i = 0; i < 100; i++ ) {
if ( IsPrime ( i ) )
cout<< i <<" is prime\n";
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>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
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75
I am surprised reading this, I would have thought most of you knew the +2,+4 rule --
but it hasn't come up, just the old +2 rule.
Assuming that you are not going to use the memory that the typical sieve methods use, or the complexity of the more you can extend the idea that you increment the test by +2 to +2,+4 as below.
bool isPrime2(unsigned int& number)
{
if (number > 3)
{
if (number % 2 == 0) return false;
if (number % 3 == 0) return false;
const int MAX = (int)sqrt(number) + 1;
int i=5;
while(i<MAX)
{
if ( (number % i) == 0) return false;
i+=2;
if ( (number % i) == 0) return false;
i+=4;
}
}
else if (number<2)
{
return false;
}
return true;
}
The code as given is about 45% quicker than the simple +2 system. However, there are many other ways of doing things.
The next obvious extension is sometimes called the wheel factorization , e.g. http://primes.utm.edu/ That reference has lots of other algorithms as well.
StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
I think a sieve that uses a bit of memory is way better than using that much CPU time, you can generate millions of primes in a few seconds using a sieve.
pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111
pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111