Area: Quik_Bas Msg: #289 Date: 02-14-93 13:16 (Public) From: Eric B. Ford To: ALL Subject: Disk Buffering/ Prime Num
Somebody asked about buffering. Here's an example with a prime number generator. (A crumby one, but that's ok.) CLS PRINT " Prime Number Generater" PRINT : PRINT DIM First AS LONG, Last AS LONG INPUT "What number would you like to start checking at"; First INPUT "What number would you like to end checking at"; Last IF First MOD 2 = 0 THEN First = First + 1 IF Last MOD 2 = 0 THEN Last = Last - 1 IF First < 0 THEN First = 0 IF Last < 0 THEN Last = 0 IF Last - First <= 0 THEN END PRINT : PRINT WHILE OutPutTo <> 1 AND OutPutTo <> 2 AND OutPutTo <> 3 CLS PRINT "Would you like Prime Number to be printed to:" PRINT " (1) The Screen" PRINT " (2) A Data File" PRINT " (3) Both" INPUT " Chioce"; OutPutTo WEND IF OutPutTo = 2 OR OutPutTo = 3 THEN DIM dat(8192) AS LONG DIM i AS LONG, j AS LONG, ii AS LONG IF OutPutTo = 1 THEN tim1 = TIMER CLS x = 1: y = 2 PRINT "The numbers below are prime. Now testing..."; FOR i = First TO Last STEP 2 TEST = 1 LOCATE 1, 51: PRINT i ii = i / 2 IF ii MOD 2 = 0 THEN ii = ii - 1 FOR j = 3 TO ii STEP 2 IF i MOD j = 0 THEN TEST = 0 j = ii END IF NEXT IF TEST = 1 THEN primes = primes + 1 LOCATE y, x PRINT "***"; i; "***"; x = x + 20 IF x > 61 THEN x = 1: y = y + 1 IF y > 22 THEN y = 2 LOCATE y, x: PRINT " " END IF NEXT tim2 = TIMER: tim = tim2 - tim1 LOCATE 1, 1: PRINT " --- Done --- "; primes; "primes computed in"; tim; "seconds" END IF IF OutPutTo = 2 THEN INPUT "File name for output"; File$ File$ = RTRIM$(LTRIM$(File$)) IF LEN(File$) > 8 OR LEN(File$) = 0 THEN END IF INSTR(File$, ".") = 0 THEN File$ = File$ + ".txt" OPEN File$ FOR OUTPUT AS #1 CLS tim1 = TIMER PRINT "Now testing..."; FOR i = First TO Last STEP 2 LOCATE 1, 16: PRINT i; TEST = 1 ii = i / 2 IF ii MOD 2 = 0 THEN ii = ii - 1 FOR j = 3 TO ii STEP 2 IF i MOD j = 0 THEN TEST = 0 j = ii END IF NEXT IF TEST = 1 THEN primes = primes + 1 dat(0) = dat(0) + 1 dat(dat(0)) = i IF dat(0) = 8192 THEN FOR dit = 1 TO 8192 PRINT #1, dat(dit) dat(0) = 0 NEXT END IF END IF NEXT FOR dit = 1 TO dat(o) PRINT #1, dat(dit) NEXT tim2 = TIMER: tim = tim2 - tim1 LOCATE 1, 1: PRINT " --- Done --- "; primes; "primes computed in"; tim; "seconds" PRINT #1, " --- Done --- "; primes; "primes computed in"; tim; "seconds" END IF IF OutPutTo = 3 THEN INPUT "File name for output"; File$ File$ = RTRIM$(LTRIM$(File$)) IF LEN(File$) > 8 OR LEN(File$) = 0 THEN END IF INSTR(File$, ".") = 0 THEN File$ = File$ + ".txt" OPEN File$ FOR OUTPUT AS #1 tim1 = TIMER CLS x = 1: y = 2 PRINT "The numbers below are prime. Now testing..."; FOR i = First TO Last STEP 2 TEST = 1 LOCATE 1, 51: PRINT i ii = i / 2 IF ii MOD 2 = 0 THEN ii = ii - 1 FOR j = 3 TO ii STEP 2 IF i MOD j = 0 THEN TEST = 0 j = ii END IF NEXT IF TEST = 1 THEN primes = primes + 1 LOCATE y, x PRINT "***"; i; "***" x = x + 20 IF x > 61 THEN x = 1: y = y + 1 IF y > 22 THEN y = 2 LOCATE y, x: PRINT " " dat(0) = dat(0) + 1 dat(dat(0)) = i IF dat(0) = 8192 THEN FOR dit = 1 TO 8192 PRINT #1, dat(dit) dat(0) = 0 NEXT END IF END IF NEXT FOR dit = 1 TO dat(o) PRINT #1, dat(dit) NEXT tim2 = TIMER tim = tim2 - tim1 LOCATE 1, 1: PRINT " --- Done --- "; primes; "primes computed in"; tim; "seconds" PRINT #1, " --- Done --- "; primes; "primes computed in"; tim; "seconds" END IF CLOSE END --- * Origin: Eric Ford (1:3632/1.6)
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