Paract BASIC 30n+k篩

 投稿者:たろさ  投稿日:2017年 5月14日(日)02時33分37秒
  CPU                   os_Win7(32bt)
Intel Core i7 4790K
Core Speed 4499.0 MHz
Multiplier x Bus Speed 45.0 x 100.0 MHz

最速で
Paract BASIC 0.8.0.1  +   win32 Lazarus + fpc 3.0.2
1億~1兆 1E8step  3337.21s (55分37.21秒)
1億~10兆 1E8step 45602.56s(12時間40分2.56秒)


------------------------------------------------------------

!Paract BASIC 30n+k篩 Ver.12  10兆  5/9  (1E8) step
DECLARE STRUCTURE struct1: 1 OF NUMERIC
DECLARE STRUCTURE struct2: 1 OF NUMERIC(227650)
DECLARE STRUCTURE struct4: 3 OF NUMERIC
DECLARE SHARED sha OF struct2
DECLARE MESSAGE mes2 OF struct4
DECLARE MESSAGE mes3 OF struct4
DECLARE MESSAGE mes4 OF struct4
DECLARE MESSAGE mes5 OF struct4
DECLARE MESSAGE mes6 OF struct4
DECLARE MESSAGE mes7 OF struct4
DECLARE MESSAGE mes8 OF struct4
DECLARE MESSAGE met2 OF struct1
DECLARE MESSAGE met3 OF struct1
DECLARE MESSAGE met4 OF struct1
DECLARE MESSAGE met5 OF struct1
DECLARE MESSAGE met6 OF struct1
DECLARE MESSAGE met7 OF struct1
DECLARE MESSAGE met8 OF struct1
PARACT PART1
OPTION ARITHMETIC NATIVE
LET k=3162319
LET k3=227650
DECLARE EXTERNAL SUB prime
CALL prime(k,k3)
WAIT EVENT Ok5
LET S=1E8 !pi(1E12),37607912018
LET E=1E9 !pi(1E11),4118054813
LET ST=1E8

START Part2
START PART3
START PART4
START PART5
START PART6
START PART7
START PART8
SEND TO mes2 FROM S,E,ST
SEND TO mes3 FROM S,E,ST
SEND TO mes4 FROM S,E,ST
SEND TO mes5 FROM S,E,ST
SEND TO mes6 FROM S,E,ST
SEND TO mes7 FROM S,E,ST
SEND TO mes8 FROM S,E,ST
LET TOTAL=5761455
DECLARE EXTERNAL FUNCTION cprime
!DECLARE NUMERIC X,Y,Z

FOR I=S TO E-ST STEP ST
   LET t0=TIME
   LET L=cprime(I,I+ST/8)
   RECEIVE FROM met2 TO X
   RECEIVE FROM met3 TO Y
   RECEIVE FROM met4 TO Z
   RECEIVE FROM met5 TO X1
   RECEIVE FROM met6 TO Y1
   RECEIVE FROM met7 TO Z1
   RECEIVE FROM met8 TO X2
   LET L=L+X+Y+Z+X1+Y1+Z1+X2
   LET TOTAL=TOTAL+L
   !IF MOD(I+ST,1E9)=0 THEN PRINT (I+ST)/1E9;TOTAL
   !PRINT TOTAL
   PRINT (I+ST)/1E8;TOTAL;L;
   LET TM=TIME-t0
   PRINT USING"###.##":TM;
   PRINT "秒"
NEXT I
END PARACT

PARACT PART2
OPTION ARITHMETIC NATIVE
RECEIVE FROM mes2 TO X,Y,Z
LET S=X
LET E=Y
LET ST=Z
DECLARE EXTERNAL FUNCTION cprime
DECLARE NUMERIC L
FOR I=S TO E-ST STEP ST
   LET L=cprime(I+ST/8,I+ST/4)
   SEND TO met2 FROM L
NEXT I
END PARACT

PARACT PART3
OPTION ARITHMETIC NATIVE
RECEIVE FROM mes3 TO X,Y,Z
LET S=X
LET E=Y
LET ST=Z
DECLARE EXTERNAL FUNCTION cprime
DECLARE NUMERIC L
FOR I=S TO E-ST STEP ST
   LET L=cprime(I+ST/4,I+3*ST/8)
   SEND TO met3 FROM L
NEXT I
END PARACT

PARACT PART4
OPTION ARITHMETIC NATIVE
RECEIVE FROM mes4 TO X,Y,Z
LET S=X
LET E=Y
LET ST=Z
DECLARE EXTERNAL FUNCTION cprime
DECLARE NUMERIC L
FOR I=S TO E-ST STEP ST
   LET L=cprime(I+3*ST/8,I+ST/2)
   SEND TO met4 FROM L
NEXT I
END PARACT

PARACT PART5
OPTION ARITHMETIC NATIVE
RECEIVE FROM mes5 TO X,Y,Z
LET S=X
LET E=Y
LET ST=Z
DECLARE EXTERNAL FUNCTION cprime
DECLARE NUMERIC L
FOR I=S TO E-ST STEP ST
   LET L=cprime(I+ST/2,I+5*ST/8)
   SEND TO met5 FROM L
NEXT I
END PARACT

PARACT PART6
OPTION ARITHMETIC NATIVE
RECEIVE FROM mes6 TO X,Y,Z
LET S=X
LET E=Y
LET ST=Z
DECLARE EXTERNAL FUNCTION cprime
DECLARE NUMERIC L
FOR I=S TO E-ST STEP ST
   LET L=cprime(I+5*ST/8,I+3*ST/4)
   SEND TO met6 FROM L
NEXT I
END PARACT

PARACT PART7
OPTION ARITHMETIC NATIVE
RECEIVE FROM mes7 TO X,Y,Z
LET S=X
LET E=Y
LET ST=Z
DECLARE EXTERNAL FUNCTION cprime
DECLARE NUMERIC L
FOR I=S TO E-ST STEP ST
   LET L=cprime(I+3*ST/4,I+7*ST/8)
   SEND TO met7 FROM L
NEXT I
END PARACT

PARACT PART8
OPTION ARITHMETIC NATIVE
RECEIVE FROM mes8 TO X,Y,Z
LET S=X
LET E=Y
LET ST=Z
DECLARE EXTERNAL FUNCTION cprime
DECLARE NUMERIC L
FOR I=S TO E-ST STEP ST
   LET L=cprime(I+7*ST/8,I+ST)
   SEND TO met8 FROM L
NEXT I
END PARACT

EXTERNAL FUNCTION cprime(k4,k6)
OPTION ARITHMETIC NATIVE
DECLARE NUMERIC G(227650) !素数
GET FROM sha TO G

DIM B(8)
DATA 1,7,11,13,17,19,23,29
MAT READ B
LET Q=30
LET U=INT(k6/Q)
LET W=INT(k4/Q)
LET kd=INT(k6/207)
DIM D(0 TO U-W)
DIM BA(48)
DATA 17,19,23,7,11,13,29,13,11,19,17,1,7,29,13,17,1,23,11,7,29,1,23,19
DATA 19,23,1,29,7,11,23,1,17,13,29,7,1,17,19,11,13,29,13,11,7,23,19,17
MAT READ BA
LET L=0
LET COUNT=0
FOR r=1 TO 8
   LET rr=B(r)
   MAT D = ZER
   LET MD=0
   LET t=4
   DO
      LET x=G(t)
      IF x^2>k6 THEN EXIT DO
      LET G1=INT(W/x)
      LET Lz=1
      IF MOD(x+rr,Q)=0 THEN
         LET y=-(x+rr)/Q
         GOTO 800
      END IF

      IF MOD(x-rr,Q)=0 THEN
         LET y=(x-rr)/Q
         GOTO 800
      END IF
      DO
         IF BA(Lz+L)=MOD(x,Q) THEN
            LET y=-(INT(x/(Q/B(Lz+1)))+1)
            GOTO 800
            EXIT DO
         END IF
         LET Lz=Lz+1
      LOOP UNTIL Lz=7

800       FOR f=G1 TO kd
             IF x*f+y<W THEN GOTO 900
             IF x*f+y>U THEN GOTO 1000
             LET D(x*f+y-W)=1
900       NEXT f
1000       LET t=t+1
        LOOP

        FOR n=0 TO U-W
           LET ST=n+W
           IF D(n)=0 THEN
              IF Q*ST+rr>k4 AND Q*ST+rr<k6 THEN LET COUNT=COUNT+1
           END IF
        NEXT n
        LET L=L+6
     NEXT r
     LET cprime=COUNT
  END FUNCTION

  EXTERNAL SUB prime(k,k2)
     OPTION ARITHMETIC NATIVE
     DECLARE NUMERIC G(227650) !素数
     !エラトステネスの篩
     LET Fu=1783 !5633
     LET Fm=276  !739
     DIM P(Fu)
     DIM A(Fm)
     MAT P=ZER
     MAT A=ZER
     LET A(1)=2
     LET H1=1
     FOR I=3 TO SQR(Fu) STEP 2
        IF P(I)=0 THEN
           FOR J=I*I TO Fu STEP I
              LET P(J)=1
           NEXT J
        END IF
     NEXT I
     FOR I=3 TO Fu STEP 2
        IF P(I)=0 THEN
           LET H1=H1+1
           LET A(H1)=I
        END IF
     NEXT I

     LET Q=6
     LET k7=k          !篩の計算範囲
     LET k5=IP(k7/Q)+1
     DIM Au(k5),Av(k5)

     MAT Au = ZER     !(6*n-1)
     MAT Av = ZER     !(6*n+1)

     FOR n=3 TO Fm
        LET Pu=A(n)
        IF MOD(Pu+1,Q)=0 THEN !(6*n-1)
           LET ru=(Pu+1)/Q
           FOR i=1 TO k5
              IF Pu*i+ru>k5 THEN EXIT FOR
              LET Au(Pu*i+ru)=1
           NEXT i
        END IF

        IF MOD(Pu-1,Q)=0 THEN
           LET ru=(Pu-1)/Q
           FOR i=1 TO k5
              IF Pu*i-ru>k5 THEN EXIT FOR
              LET Au(Pu*i-ru)=1
           NEXT i
        END IF

        IF MOD(Pu+1,Q)=0 THEN !(6*n+1)
           LET ru=(Pu+1)/Q
           FOR i=1 TO k5
              IF Pu*i-ru>k5 THEN EXIT FOR
              LET Av(Pu*i-ru)=1
           NEXT i
        END IF

        IF MOD(Pu-1,Q)=0 THEN
           LET ru=(Pu-1)/Q
           FOR i=1 TO k5
              IF Pu*i+ru>k5 THEN EXIT FOR
              LET Av(Pu*i+ru)=1
           NEXT i
        END IF
     NEXT n

     LET G(1)=2
     LET G(2)=3
     LET cz=2
     FOR n=1 TO k5
        IF 6*n-1>k7 THEN GOTO 100
        IF Au(n)=0 THEN
           LET cz=cz+1
           LET G(cz)=6*n-1
        END IF
100    IF 6*n+1>k7 THEN EXIT FOR
       IF Av(n)=0  THEN
          LET cz=cz+1
          LET G(cz)=6*n+1
       END IF
    NEXT n
    PUT TO sha FROM G
    SIGNAL Ok5
END SUB
----------------------------------------------------

AMD Athlon(tm) 64 Processor 3800+
  周波数 : 2.40 GHz
メモリー : 3.00 GB
Microsoft Windows 8.1(32 ビット)

動作確認しています。が、計算速度にムラ?
2  11078937  5317482   1.79秒
3  16252325  5173388   8.22秒
4  21336326  5084001   5.62秒
5  26355867  5019541   9.27秒
6  31324703  4968836   4.22秒
7  36252931  4928228   9.04秒
8  41146179  4893248   7.00秒
9  46009215  4863036   6.76秒
10  50847534  4838319   4.71秒


追記

10兆
6n+k
1E7 TO 1E13
59814.48s (16時間36分54.48秒)

30n+k_v1
1E8 TO 2E13
51822.95s(14時間23分42.95秒)

30n+k_v9
47980.82s(13時間19分40.82秒)


20兆
1E13 TO 2E13
6n+k
80947.29s (22時間29分7.29秒)

30n+k_v9
60730.73s(16時間52分10.73秒)

http://blogs.yahoo.co.jp/donald_stinger

 

戻る