Private Sub Command1_Click()
getpi CLng(InputBox("生成多少位数(1-50000)的PI?", "提示", 30000))
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 As Long, j As Long, t, d As Long, g, k As Long, f()
laptime = Timer
max = 18 * nums
ReDim f(0 To max)
ReDim result(nums)
For i = 0 To max
f(i) = 20000
Next
result(0) = "π=" & vbCrLf
g = 20000
For j = max To 1 Step -18
t = 0
For i = j To 1 Step -1
t = t + f(i) * 100000
d = 2 * i + 1
f(i) = t - Int(t / d) * d
t = Int(t / d) * i
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 Sub