06-03-2003, 04:48 AM
Well, actually I could do it faster:
-Limited size of tables so they fit in dgroup.
-Saves primes into a file so you can check them
-It displays one prime every 16000 so you can see it's working
-It calculates and saves 300,000 primes in 1 minute (compiled)
The highest prime it can calculate should be under 2^30, it makes around 5,000,000 primes. However,I have not patiented to see the end , it can take hours and probably it ends by an error.
-Limited size of tables so they fit in dgroup.
-Saves primes into a file so you can check them
-It displays one prime every 16000 so you can see it's working
-It calculates and saves 300,000 primes in 1 minute (compiled)
The highest prime it can calculate should be under 2^30, it makes around 5,000,000 primes. However,I have not patiented to see the end , it can take hours and probably it ends by an error.
Code:
CLS
PRINT "PRIME CALCULATOR BY ANTONI GUAL agual@eic.ictnet.es"
'Version 2.
CONST pp = 1024& * 16 - 1
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
maxx& = 2
ind = 2
test& = 5
t! = TIMER
OPEN "primes.txt" FOR OUTPUT AS #1
PRINT #1, 2, 3,
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!
PRINT #1, test&,
IF (ind AND 7) = 0 THEN PRINT #1,
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 LEN(INKEY$)
ERASE pr, pr1, pr2
CLOSE
Antoni