|
サンダラムのふるい
https://en.wikipedia.org/wiki/Sieve_of_Sundaram
FOR I=1 TO 6
LET N=10^I
PRINT N;"th prime";NTHPRIME(N)
NEXT I
END
EXTERNAL FUNCTION NTHPRIME(M)
LET N=M*(LOG(M)+LOG(LOG(M)))
LET K=INT((N-2)/2)
DIM A(K)
FOR I=1 TO INT(K/3)
LET J=I
DO WHILE I+J+2*I*J<=K
LET S=I+J+2*I*J
LET A(S)=1
LET J=J+1
LOOP
NEXT I
LET COUNT=1
FOR I=1 TO K
IF A(I)=0 THEN
LET COUNT=COUNT+1
IF COUNT=M THEN
LET NTHPRIME=I*2+1
EXIT FUNCTION
END IF
END IF
NEXT I
END FUNCTION
|
|