[futurebasic] Re: [FB] Prime Numbers?

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

From: Jay Reeve <jktr@...>
Date: Fri, 18 Dec 98 20:14:54 -0600
>Why don't you, once you have the prime numbers, write them out as a binary
>string, with the bit set if the corresponding number  is a prime -- i.e.,
>the first bit is 0, because "0" is not a prime, the second is 0 because "1"
>is not a prime, the third and fourth are set to 1, because "2" and "3" are
>prime, etc.
>This takes only 832 bits, is 104 bytes, plus an FN BITTEST, surely less
>than a complete function, and very fast...
>
Hans,

Cool suggestion. I, however, took it as a challenge. I couldn't get my 
code down to 104 bytes, but at 146 byte it's not _that_ far off. This 
works for numbers from 2 to 832:

LOCAL FN IsItAPrime (Num&)
  FOR factor& = Num&/2 TO 2 STEP -1
    IF Num& MOD factor& = 0 THEN factor& = - factor&
  NEXT
END FN = factor& > -3

Of course your suggestion blows this away speed-wise. :-)
 0"0
 =J= a  y
  "