[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-devel
Subject: Re: Prime number problems
From: Michael Pyne <pynm0001 () comcast ! net>
Date: 2004-04-23 18:28:27
Message-ID: 200404231428.34000.pynm0001 () comcast ! net
[Download RAW message or body]
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Friday 23 April 2004 11:36, M2George wrote:
> The following code fragment works partialy, it list all primes in range but
> also includes "1" as a prime.
> How do I get rid of it?
This is your problem:
> for(int i = iNumber1; i <= iNumber2; i++)
> {
> for (int iFactor = 2; iFactor < i; ++iFactor)
Let's say that iNumber1 equals 1. You have this:
for (i = 1; i < iNumber2; i++)
for (iFactor = 2; iFactor < 1; ++iFactor)
...
Your loop starts off by setting isPrime to true, but your second for loop will
never run because 2 is greater than, not less than 1. Since the second loop
doesn't run, the i == 1 test won't be encountered, and isPrime will remain
true.
The simplest fix is to set isPrime to be false right before the second for
loop. However, if I were you, I'd improve that loop using the suggestions of
the other replies to your e-mail.
Regards,
- Michael Pyne
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)
iD8DBQFAiWBPqjQYp5Omm0oRAtqcAJ915OvYjfo4skSvH34TQqvJz/hvEwCgiDaV
ElaIF4Smw/On94x6MsbD7PE=
=rYuo
-----END PGP SIGNATURE-----
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic