06-06-2003, 07:01 PM
This one is a 30% faster than my former post
I'm now figuring out how to sort my arrays, it should be even faster
Code:
WIDTH , 50: CLS
PRINT "PRIME CALCULATOR BY ANTONI GUAL agual@eic.ictnet.es"
INPUT "Maximum prime(5 to 2,100,000,000): "; mp&
CONST stor = 3400 'enough to calculate the 5,000,000 first primes
DIM pr(stor) AS LONG 'keeps 2 * prime (the increment to next multiple)
DIM pr1(stor) AS LONG 'keeps the current muliple of the prime
'seed the tables
pr(2) = 2 * 3
pr1(2) = 3 * 3
'init vars
ind& = 2
ind2% = 2
sqre& = 5 * 5
'first prime tested
test& = 5
'just a trick to avoid W2000 to lower priority
OPEN "nul" FOR OUTPUT AS #1
t! = TIMER
PRINT 2, 3,
CNT% = 2
DO
i% = 1
DO
i% = i% + 1
WHILE pr1(i%) < test&: pr1(i%) = pr1(i%) + pr(i%): WEND
IF pr1(i%) = test& THEN GOTO noprime
LOOP UNTIL i% = ind2%
'it's a prime, print it
ind& = ind& + 1
PRINT test&,
'scroll screen if needed (And keep priority high in W2000)
CNT% = CNT% + 1: IF CNT% = 250 THEN CLS : CNT% = 0: PRINT #1, ".";
'add to primes table
IF ind& <= stor THEN : pr(ind&) = test&: IF ind2% = stor THEN EXIT DO
noprime:
'go for next prime
test& = test& + 2
'if beyond square of last divisor, add a new divisor to tests
IF test& > sqre& THEN
ind2% = ind2% + 1: sqre& = pr(ind2%) * pr(ind2%)
pr1(ind2%) = sqre&
pr(ind2%) = pr(ind2%) + pr(ind2%)
IF LEN(INKEY$) THEN EXIT DO
END IF
LOOP UNTIL test& > mp&
''STOP
ERASE pr, pr1
PRINT : PRINT : PRINT "time for "; ind&; "primes: "; TIMER - t!; "seconds"
a$ = INPUT$(1)
I'm now figuring out how to sort my arrays, it should be even faster