Prime numbers gen.

 BBS: Inland Empire Archive
Date: 12-03-92 (23:13)             Number: 395
From: ERIC B. FORD                 Refer#: NONE
  To: MICHEL BERTLER                Recvd: NO  
Subj: Prime numbers gen.             Conf: (2) Quik_Bas
 > Your program is quite fast! However, could you make it
 > ask for a user
 > input (upper limit) and redirect its output into a
 > readable Ascii file rather than on the screen?

Well, here's a version to have an upper limit.  (For above
32767 change to LONGs)  To redirect it to an ASCII file you
can just compile and then do something like:

PRIME >Primes.txt

' Load QB with the "/l" option
DIM prime(32767) AS INTEGER: CLS
PRINT "                        Prime Number Generater"
PRINT "                             Eric Ford"
PRINT
PRINT "What number would you like to end checking at?"
INPUT Last%
PRINT : PRINT

INPUT "Filename (Leave blank for screen)"; File$

PRINT "The numbers below are prime.        Now testing..."

tim1 = TIMER: half% = Last% / 2 + 1

    FOR i% = 3 TO half% STEP 2
        j% = 2
        WHILE j% * i% <= Last%
            prime(j% * i%) = 1
            j% = j% + 1
            WEND
    NEXT i%

    FOR j% = 4 TO Last% STEP 2
        prime(j%) = 1
    NEXT j%

FOR First% = 1 TO 3
    prime(First%) = 0
NEXT First%

tim2 = TIMER


IF len(ltrim$(rtrim$(File$))) = 0 then
    FOR i% = 2 TO Last%
        IF prime(i%) = 0 THEN primes = primes + 1: PRINT i%
    NEXT i%
else
    open file$ for output as #1
    for i% = 2 to Last%
        if prime(i%) = 0 then
            primes = primes + 1
            print #1, i%
        end if
    next i%
    close
end if

tim = tim2 - tim1
PRINT "     ---   Done   ---      "; primes;
PRINT " primes computed in"; tim; "seconds"


A good deal of this is from the message editor and not QB,
so beware of minor mistooks.

---
 * Origin: Eric Ford (1:3632/1.6)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET