エラトステネスのふるい(30n+k)

 投稿者:しばっち  投稿日:2021年11月 3日(水)18時08分34秒
  エラトステネスのふるい(30n+k)

LET N=3
FOR I=0 TO 9
   LET S=ERATOS(I*10^N,(I+1)*10^N)
   LET SUM=SUM+S
   PRINT I*10^N+1;"~";(I+1)*10^N;":";S;SUM
NEXT I
END

EXTERNAL  FUNCTION ERATOS(S,E) ! S>=0 , S<E , E>5
DIM P(0 TO E-S+1),A(8)
MAT READ A
DATA 1,7,11,13,17,19,23,29
FOR I=0 TO INT(SQR(E)/30)
   FOR J=1 TO 8
      IF I=0 AND J=1 THEN LET J=2
      LET K=30*I+A(J)
      LET KK=INT(S/K)*K
      IF KK=0 THEN LET KK=K*K
      IF KK < S THEN LET KK=KK+K
      IF MOD(KK,K)<>0 THEN LET KK=KK+K
      IF MOD(INT(KK/K),2)=0 THEN LET KK=KK+K
      FOR L=KK TO E STEP K*2
         LET P(L-S)=1
      NEXT L
   NEXT  J
NEXT  I
IF S<=5 THEN LET COUNT=1
IF S<=3 THEN LET COUNT=2
IF S<=2 THEN LET COUNT=3
FOR I=INT(S/30) TO INT(E/30)
   FOR J=1 TO 8
      IF I=0 AND J=1 THEN LET J=2
      LET K=30*I+A(J)
      IF K>E THEN EXIT FOR
      IF K>=S AND P(K-S)=0 THEN LET COUNT=COUNT+1
   NEXT J
NEXT I
LET ERATOS=COUNT
END FUNCTION
 

戻る