>>CREATE CURSOR Prime (primenumber i) >>INSERT INTO Prime VALUES (3) >> >>FOR I = 5 TO 100000 STEP 2 >> nSQRT = SQRT(i) >> GO TOP >> LOCATE WHILE prime.primenumber =< nSQRT FOR I % prime.primenumber = 0 >> IF !FOUND() >> INSERT INTO Prime VALUES ( i ) >> ENDIF >>ENDFOR >>Well this algoritm works well up to primes to 1,000,000 or so. After that the LOCATE line is getting a read hard time. IOW the program get slower and slower for each new Prime.
CREATE CURSOR Prime (primenumber I, nextoccur B) INSERT INTO Prime VALUES (3, 9) INDEX ON NextOccur TAG occurance t = seconds() FOR nT = 5 TO 200000 STEP 2 IF KEYMATCH(nT) DO WHILE SEEK(nT) nNext=NextOccur + 2*PrimeNumber DO WHILE KEYMATCH(nNext) nNext=nNext+2*PrimeNumber ENDDO REPLACE NextOccur WITH nNext ENDDO ELSE INSERT INTO Prime VALUES(nT,nT^2) ENDIF ENDFOR ? seconds() - tFor finding the largest primenumber this approach is unsuitable because it takes to long to calculate all primenumbers up to a certain number. It takes a little more math. You can easely calculate a number that not even fits in a normal newspaper and is or contains a Primenumber larger than the largers primenumber used to calculate the number by multiplication of all known primenumber up to a certain point and add 1 to it: