用VB计算圆周率,步骤写清楚精确到1000位

1个回答

  • Private Sub Command1_Click()

    getpi 1000

    End Sub

    Sub getpi(Optional ByVal nums As Long = 10000)

    nums = nums / 5

    Dim max As Long, laptime As Single, result() As String

    Dim i, j As Long, d As Long, t, g, r, k As Long, f()

    laptime = Timer

    max = 10 * nums

    ReDim f(1 To max)

    ReDim result(nums)

    result(0) = vbCrLf

    For i = 1 To max

    f(i) = 30000

    Next

    For j = max To 1 Step -10

    t = 0

    For i = j To 1 Step -1

    If j = max Then

    t = t + f(i) * 1000000

    Else

    t = t + f(i) * 100000

    End If

    r = 8 * i * (2 * i + 1)

    f(i) = t - Int(t / r) * r

    d = 2 * i - 1

    t = Int(t / r) * d * d

    Next

    k = k + 1

    result(k) = Format(Int(g + t / 100000) Mod 100000, "00000")

    If k Mod 20 = 0 Then result(k) = result(k) & vbCrLf

    If k Mod 200 = 0 Then result(k) = result(k) & "---[" & k * 5 & "]---" & vbCrLf

    g = t Mod 100000

    Next

    Text1.Text = Join(result, " ")

    Me.Caption = "计算完毕!总计用时" & Timer - laptime & "秒!"

    End Su