[futurebasic] X-FB (New on the block)

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : November 2001 : Group Archive : Group : All Groups

From: Ken Shmidheiser <k.shmidheiser@...>
Date: Thu, 22 Nov 2001 18:57:00 -0500
Henry Wright, introducing himself wrote:

>By way of introduction: I'm Henry Wright, from Tucson, AZ, just retired
>(that's why I've got some time; and my be a bit slow), married, using
>FB3 #3 (just ordered #5), my interest is in prime numbers, games and
>just learning a little - having a bit of fun (that's way cool kind of
>thing). I've got a lot to learn....

Greetings Henry!

Here's a little welcome-wagon gift to play with tonight.

Best,

Ken Shmidheiser

'----------- BEGIN FB^3 CODE -----------

// Run in Console (Fast) mode

'~FN GetPrimeNumbers

//  Build an array of prime numbers

BEGIN GLOBALS
// Create a reusable global dynamic array
// to hold our prime numbers
DIM DYNAMIC primes( _maxLong ) AS LONG
END GLOBALS

LOCAL FN GetPrimeNumbers( numberOfPrimes AS LONG )
DIM found AS LONG
DIM n     AS LONG
DIM i     AS LONG

// Empty the primes() array so we can start fresh
KILL DYNAMIC primes

// Bail out if number is 0 or negative
IF numberOfPrimes <= 0 THEN EXIT FN

// 2 is the first prime number, so we start with it
primes(1) = 2
     found = 1
         n = 1

DO
// After 2, all other prime numbers are odd
// so our function can skip them
n = n + 2

// Check to see if n is a prime number
FOR i = 1 TO found
IF (n MOD primes(i)) = 0 THEN EXIT FOR
NEXT i

LONG IF i > found
// No prime number < n is a divisor for n
// therefore n is prime
found = found + 1
primes(found) = n
IF found = numberOfPrimes THEN EXIT DO
END IF
UNTIL 0

// Let FB^3 save us a little memory
// by squishing our array
COMPRESS DYNAMIC primes

END FN = primes

// Build array of the first 500 prime numbers
FN GetPrimeNumbers( 500 )

DIM i AS INTEGER

// Now let's print it
FOR i = 1 to 500
PRINT "Prime No."; i; " =";primes( i )
NEXT i

'-------------- END CODE -----------