|
エラトステネスのふるい(2n+1)
LET N=5
FOR I=1 TO 10
LET E=I*10^N
LET PE=PRIMECOUNT(E)
PRINT S+1;"~";E;":";PE-PS;PE
LET S=E
LET PS=PE
NEXT I
END
EXTERNAL FUNCTION PRIMECOUNT(N)
DIM P(N)
! 2を特別扱い
FOR I=1 TO INT(SQR(N)/2) ! ループ SQR(N)/2
LET K=2*I+1
IF N<K THEN EXIT FOR
IF P(K)=0 THEN
FOR L=K*K TO N STEP K*2
LET P(L)=1
NEXT L
END IF
NEXT I
LET COUNT=1 ! 2
FOR I=1 TO INT(N/2)
LET K=2*I+1
IF N<K THEN EXIT FOR
IF P(K)=0 THEN LET COUNT=COUNT+1
NEXT I
LET PRIMECOUNT=COUNT
END FUNCTION
|
|