|
northwolves 发表于 2011-1-27 23:21
有点慢。可参考我以前写的代码:
http://blog.csdn.net/northwolves/archive/2006/06/06/775546.aspx
htt ...
'采用普遍的傅立叶级数展开方法pi = 3 + (1 * 1) / (2 * 3 * 4) * [ 3 + (3*3)/(4*5*4) *[ 3 + (5*5)/(6*7*4) * [ 3 + (7*7)/(8*9*4) * [ ... ] ] ] ]
'输入:需计算的圆周率位数,理论精度约5万位
'Add a commandbutton and textbox(multine=true,scrollbars=both) to form1
Option Explicit
Private Sub Command1_Click()
getpi CLng(InputBox("生成多少位数(1-50000)的PI?", "提示", 5000))
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 Sub
请教版主:以上代码怎么在Excel中运行啊?
我用的是Microsoft Office Excel 2003 |
|