[futurebasic] Re: [FB] Prime Numbers?

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : December 1998 : Group Archive : Group : All Groups

From: Hans van Maanen <hvmaanen@...>
Date: Wed, 23 Dec 1998 08:44:02 +0100
>Robert wrote:
>> I claim (at least temporarily) the prize.
>
>Great job!  Rather than try to compete with that, I offer a suggestion
>for another speed enhancement to this (already speedy) routine:
>
>Right now you're checking the divisor 2, plus all odd divisors (up to
>the square root).  The odd divisors are stored in register D0 by
>initializing it to 3 and adding 2 each time you loop.  Instead, I
>suggest this:
>
>1. First, explicitly check divisors 2 and 3.
>2. Initialize D0 to 5, and use that as the next divisor.
>3. Instead of incrementing D0 by 2 every time, increment
>   it alternately by 2 and by 4.
>
>That way, you'll get a divisor list like this:
>
> 2,3,5,7,11,13,17,19,23,25,29,31,35,37,41,...
>
>in other words, you've cut from the list all the mulitples of 3 (except
>3 itself).  That makes your lists about 33% shorter than the case where
>you check every odd number.
>
>- Rick
>
>

I don't know if I can still follow this, but prime numbers over 3 are
always of the form either 6n + 1 or 6n - 1. So after 2 and 3 you should be
able do a loop like

FOR i = 5 TO x STEP 6
 IF n MOD  i = 0  THEN FN IsPrime(i)
 IF n MOD (i+2) THEN FN IsPrime(i+2)
NEXT

where x = SQR(n) and one wants to exit the loop if i > x +1.

This is just a quick thought, I haven't tested it, and I don't know how to
write it in ass.

Still,

Hans