Years ago I wrote my own prime number generator in QBasic. Using only 10 lines of code, it was as efficient as I considered possible. (Correct me if I'm wrong. I didn't have a lot of experience back at the time, and a lot of my BASIC knowledge has faded through the ages.)

10 OPEN "prime.txt" FOR APPEND AS #1
20 PRINT #1, " 2": PRINT , " 2"
30 FOR A& = 3 TO 2147483647 STEP 2
40 FOR X& = 3 TO INT(SQR(A&)) STEP 2
50 Y# = A& / X&
60 IF Y# = INT(Y#) GOTO 90
70 NEXT X&
80 PRINT #1, A&: PRINT , A&
90 NEXT A&
100 END

10: Creates a file called PRIME.TXT to which the prime numbers will be appended.

20: 2 is hard-coded because it is the only even prime. Only checking odd numbers doubles the processing speed. (See line 80 for the explanation of the preceding blank space.)

30: A FOR loop that runs through all odd numbers between 3 (the smallest candidate prime greater than 2) and 2,147,483,647 (the maximum value of a long integer in QBasic).

40: A nested FOR loop that runs through all candidate proper divisors of A&. There's no use searching for divisors greater than the square root, since they can't possibly be integers. As Olathe already pointed out, the integer part of this square root is what you really need for this. At first I absent-mindedly forgot the STEP 2. Since I don't run through even numbers, they can't have even divisors. Eliminating this inefficiency doubled my processing speed again.

50: Assigns the quotient A&/X& to a double precision number. I haven't tested if single precision would produce any false primes, but I just wanted to be safe.

60: If a proper divisor of A& is found, it isn't a prime number. This is done by casting Y# to an INT and comparing it with Y# itself. If they are equal (i.e., Y# has nothing after the decimal point), X& is a proper divisor. The program breaks the nested loop and continues with the next candidate prime.

70: As long as no proper divisor is found, the program patiently tries the next X&.

80: If the nested loop ends without finding a proper divisor (line 60), A& is appended to #1 (the file created on line 10) and printed on the screen. We have found a prime number! For some reason, a blank space precedes the numbers in the file and a tab those on screen. I have no idea why this is, but I'm only interested in functional code, so I didn't waste time and code on the layout.

90: The program checks the next candidate, regardless of the previous result.

100: Ends the program and closes the file.

Although I only had a 80386 at the time, I was very satisfied with the speed it spew results. I checked the first scores of results against an existing table to assure the faultlessness of my program, and of course I checked whether it had recognized 31337. =)


If you think this code is of any use, feel free to borrow it. Supposedly we may assume the program works accurate, but the full responsibility for any inconvenience/damage/financial loss whatsoever lies entirely with the person using the code. I provide it as is without any warranty.