06-03-2003, 05:37 AM
Well this one abides by all rules set by Touf:
-You can set the maximum prime you want
-It prints them all (after saving them into a file)
-You can set the maximum prime you want
-It prints them all (after saving them into a file)
Code:
CLS
PRINT "PRIME CALCULATOR BY ANTONI GUAL agual@eic.ictnet.es"
INPUT "Maximum prime(1 to 1,000,000,000): "; mp&
CONST pp = 1024& * 16 - 1
CONST tabs = 10
CONST stor = 3400 'enough to calculate the 10,000,000 first primes
DIM pr(stor + 2) AS INTEGER
DIM pr1(stor + 2) AS LONG
DIM pr2(stor + 2) AS LONG
DIM ind AS LONG
'pr saves primes found
'pr1 saves primes multiplied by factor pr2
'We save pr2 to stop scanning the array if pr2<pr, this indicates that
' pr> sqr(test&) without having to calculate the square root.
'
'pr() saves primes, pr1()=pr()*pr2()
'init the tables with 3 and test our first candidate, 5
pr(2) = 3
pr1(2) = 3
pr2(2) = 1
ind = 2
test& = 5
t! = TIMER
OPEN "primes.txt" FOR OUTPUT AS #1
tt% = tt% + tabs: PRINT #1, 2; TAB(tt%);
tt% = tt% + tabs: PRINT #1, 3; TAB(tt%);
DO
isprime% = -1
i% = 1
DO
i% = i% + 1
WHILE pr1(i%) < test&: pr1(i%) = pr1(i%) + pr(i%): pr2(i%) = pr2(i%) + 1: WEND
IF pr1(i%) = test& THEN isprime% = 0: EXIT DO
LOOP UNTIL pr2(i%) <= pr(i%)
IF isprime% THEN
ind = ind + 1
IF (ind AND pp) = 0 THEN PRINT USING "####### th prime is: ##########. Time:###.# sec."; ind; test&; TIMER - t!
tt% = tt% + tabs: PRINT #1, test&; TAB(tt%);
IF (ind AND 7) = 0 THEN PRINT #1, : tt% = 0
IF ind <= stor THEN
pr(ind) = test&
pr1(ind) = test&
pr2(ind) = 1
END IF
END IF
test& = test& + 2
LOOP UNTIL (i% = stor) OR test& > mp& OR LEN(INKEY$)
ERASE pr, pr1, pr2
CLOSE
'That's all!
OPEN "primes.txt" FOR INPUT AS #1
WHILE NOT EOF(1)
LINE INPUT #1, a$
PRINT a$
WEND
close
PRINT
PRINT "Total time "; TIMER - t!; "seconds. Results are in the file PRIMES.TXT"
Antoni