[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Problem in prime number calculation (was: no subject)
From:       Kevin Krammer <kevin.krammer () gmx ! at>
Date:       2004-04-23 16:07:15
Message-ID: 200404231807.22954.kevin.krammer () gmx ! at
[Download RAW message or body]

[Attachment #2 (multipart/signed)]


On Friday 23 April 2004 17:36, M2George wrote:
> Hello all,
>
> I'm on a steep learning curve and need some assitance please.

Step one, use subjects in mails :)

> I'm changing some code to print out prime numbers in a range for a maths
> tutoring program.
> 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?
> case FindPrime: //Find Prime-Numbers
>         {
>             QString qsNumber1 = KRestrictedLineFirstInfo->text();
>             QString qsNumber2 = KRestrictedLineSecondInfo->text();

Perhaps QSpinBox is better suitedfor the job of number input.

>             int iNumber1 = qsNumber1.toInt();
>             int iNumber2 = qsNumber2.toInt();

You should check the two values before entering the loop
is iNumber1 <= iNumber2?
is iNumber1 >= 2?
actually you could set 2 as the minimum value of the spinboxes.

>             QString qsResult = i18n("These are the prime numbers ");
>             bool i_IsPrime = TRUE;
>             for(int i = iNumber1; i <= iNumber2; i++)
>             {
>                 for (int iFactor = 2; iFactor < i; ++iFactor)
>                 {
>                     if(i % iFactor == 0)
>                     {
>                         i_IsPrime = FALSE;
>                         break;
>                     }
>                     if(i == 1)                //broken
>                     {
>                         i_IsPrime = FALSE;
>                         break;
>                     }
>                     else
>                         i_IsPrime = TRUE;
>                 }
>                 if (i_IsPrime == TRUE)
>                 {
>                     qsNumber1 = qsNumber1.number(i);
>                     qsResult += qsNumber1 + (", ");
>                 }
>             }
>             KTextBrowserResult->setText(qsResult);
>             break;
>         }

Just some optimization hints.
if iNumer1 is an even number (divideable by 2), start the loop at the next 
number and increase by 2 (even numbers cannot be primes wit the only 
exception being 2 itself)

Keep your already calculated primes in a list and only check division by them.
(any other number is just a product of primes)

Cheers,
Kevin
-- 
Kevin Krammer <kevin.krammer@gmx.at>
Qt/KDE Developer, Debian User
www.mrunix.de - Unix/Linux programming forum
www.qtforum.org - Qt programming forum

[Attachment #5 (application/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