回 帖 发 新 帖 刷新版面

主题:[讨论]求1000000以内素数的个数的程序,比一比速度

[url=http://bbs.pfan.cn/post-379076.html]http://bbs.pfan.cn/post-379076.html[/url]


求1000000以内素数的个数的程序,比一比速度

回复列表 (共15个回复)

11 楼


[color=000000]进一步优化楼上代码,QB编译后运行时间已小于1秒。

[font=宋体]
DEFLNG A-Z
'
DIM B(0 TO 15) AS INTEGER
    FOR I = 0 TO 14
        B(I) = 2 ^ I
    NEXT
    B(15) = CINT(-32768)
AAA:
    INPUT "Input a number within 2-1000000:"; N
    IF N < 2 THEN END
    IF N > 1000000 THEN
        PRINT "Out of range!": GOTO AAA
    END IF
    T! = TIMER
    K = N \ 32 + 1
    '
    REDIM A(K) AS INTEGER
    A(0) = 1
    Q = INT(SQR(N)) \ 2
    H = N \ 2
    FOR I = 1 TO Q
        IF (A(I \ 16) AND B(I AND 15)) = 0 THEN
            P = I + I + 1
            M = I * (P + 1)
            FOR j = M TO H STEP P
                R = j \ 16
                A(R) = A(R) OR B(j AND 15)
            NEXT j
        END IF
    NEXT I
    '
    Np = 1   ' Num 2 is prime
    P1 = 0: P2 = 1
    FOR j = 1 TO H
        Np = Np - ((A(P1) AND B(P2)) = 0)
        P2 = P2 + 1
        IF P2 = 16 THEN P1 = P1 + 1: P2 = 0
    NEXT j
BBB:
    PRINT : PRINT "TOTAL:"; Np
    PRINT "Time(s):"; TIMER - T!
    GOTO AAA
END


[/font]
[/color]

12 楼

【运行结果】(PentiumIV 3G)

Input a number within 2-1000000:? 1000000

TOTAL: 78498
Time(s): .7070313
Input a number within 2-1000000:? 0

【启动编译】
BC D:\BAS\PRIME1.BAS/O;
Microsoft (R) QuickBASIC Compiler  Version 4.00
Copyright (C) Microsoft Corp. 1982-1987. All rights reserved.

43148 Bytes Available
41362 Bytes Free

    0 Warning Error(s)
    0 Severe  Error(s)
LINK /EX PRIME1,D:\BAS\PRIME1.EXE,NUL,;
...
【命令行运行】
D:\BAS>prime1
Input a number within 2-1000000:? 1000000

TOTAL: 78498
Time(s): .3808594
Input a number within 2-1000000:? 0

13 楼



VB 的优化编译实在是厉害,同样的代码在VB编译后比QB编译后快许多许多倍。

计算1亿以内的素数个数只要 2秒多!!!

代码如下(编译时打开所有优化开关)

[color=000000][font=宋体]
DefLng A-Z
' Program for Sieving the primes by base 2.
' Author: Idealguy, Shanghai, 2011,12
Sub Main()
    ' Initializtion
    Dim B(0 To 31)
    For I = 0 To 30
        B(I) = 2 ^ I
    Next
    B(31) = &H80000000
    ' Input N
    N = InputBox("N Range:[2-100000000], N=?", "Prime Sieving", 1000000)
    If N < 2 Then End
    If N > 100000000 Then N = 100000000
    '
    T! = Timer
    ' Sieving orimes
    K = N \ 64 + 2
    ReDim A(K) As Long
    A(0) = 1   ' Num 2 is prime
    Q = Int(Sqr(N)) \ 2
    H = N \ 2
    For I = 1 To Q
        If (A(I \ 32) And B(I And 31)) = 0 Then
            P = I + I + 1
            M = I * (P + 1)
            For J = M To H Step P
                R = J \ 32
                A(R) = A(R) Or B(J And 31)
            Next J
        End If
    Next I
    ' Accumulating found primes
    Np = 0
    P1 = 0: P2 = 1
    For J = 0 To H
        Np = Np - ((A(P1) And B(P2)) = 0)
        P2 = P2 + 1
        If P2 = 32 Then P1 = P1 + 1: P2 = 0
    Next J
    ' Output the message
    MsgBox "Total primes in [2.." & N & "]:" & Np & vbCrLf & "Calculating Time(s):" & Timer - T!
End

End Sub


[/font]
[/color]

14 楼

这个basic小程序求10000000(y=10000000)以内素数要多长时间?不好意思,初学。就请哪位帮运行一下。
5 print 2;
10 input y
20 for i=3 to y step 2
30 for j=3 to i step 2
40 p=i/j-int(i/j)
50 if p=0 and i<>j then goto 80
60 next j
70 ptint i;
80 next i

15 楼


算10万要1分多。1000万,可能几个小时

我来回复

您尚未登录,请登录后再回复。点此登录或注册