首 页 | 新 闻 | 技术中心 | 第二书店 | 《程序员》 | 《开发高手》 | 社 区 | 黄 页 | 人 才
移 动专 题SUNIBM微 软微 创精 华Donews人 邮
我的技术中心 
我的分类 我的文档
全部文章 发表文章
专栏管理 使用说明



 RSS 订阅 
最新文档列表
Windows/.NET
.NET  (rss)    
Visual C++  (rss)    
Delphi  (rss)    
Visual Basic  (rss)    
ASP  (rss)    
JavaScript  (rss)    
Java/Linux
Java  (rss)    
Perl  (rss)    
综合
其他开发语言  (rss)    
文件格式  (rss)    
企业开发
游戏开发  (rss)    
网站制作技术  (rss)    
数据库
数据库开发  (rss)    
软件工程
其他  (rss)    

积极原创作者 
tellmenow (22)
cutemouse (22)
softj (78)
iiprogram (69)
qdzx2008 (50)
goodboy1881 (14)
wangchinaking (58)
fancyhf (1)
harrymeng (41)
yjz0065 (113)
CSDN - 文档中心 - Visual Basic 阅读:5737   评论: 1    参与评论
标题   大数阶乘的计算(一)     选择自 northwolves 的 Blog
关键字   阶乘 字符串运算
出处  

整数n的阶乘指 1*2*3*...*(n-1)*n 的值,在n=171时,计算机一般会出错(“溢出”),本文采用字符串模拟数字乘法运算,使计算10000!成为可能:

Function multi(ByVal X As String, ByVal Y As String) As String 'multi of two huge hexnum(两个大数之积)
Dim result As Variant
Dim xl As Long, yl As Long, temp As Long, i As Long
xl = Len(Trim(X))
yl = Len(Trim(Y))
 
ReDim result(1 To xl + yl)
For i = 1 To xl
For temp = 1 To yl
result(i + temp) = result(i + temp) + Val(Mid(X, i, 1)) * Val(Mid(Y, temp, 1))
Next
Next

For i = xl + yl To 2 Step -1
temp = result(i) \ 10
result(i) = result(i) Mod 10
result(i - 1) = result(i - 1) + temp
Next

If result(1) = "0" Then result(1) = ""
multi = Join(result, "")
Erase result

End Function

Private Sub Command1_Click() '节约时间,算到1000!
For i = 1 To 9
calcfactorial i * 100
Next
End Sub


Sub calcfactorial(ByVal n As Integer)
Dim a() As String, i As Long, stimer As Double
ReDim a(1 To n)
a(1) = 1
stimer = Timer
For i = 2 To n
a(i) = multi(a(i - 1), i)
Next
Debug.Print n & "! : 用时 "; Timer - stimer & " 秒, 结果 " & Len(a(n)) & " 位"
Debug.Print a(n)
End Sub


100! : 用时 4.67617187496217E-02 秒, 结果 158 位
200! : 用时 .407124999999724 秒, 结果 375 位
300! : 用时 1.00012499999957 秒, 结果 615 位
400! : 用时 1.92199999999957 秒, 结果 869 位
500! : 用时 3.14013671875 秒, 结果 1135 位
600! : 用时 4.68677343750005 秒, 结果 1409 位
700! : 用时 6.64099999999962 秒, 结果 1690 位
800! : 用时 8.9208984375 秒, 结果 1977 位
900! : 用时 11.5000117187501 秒, 结果 2270 位
1000! : 用时 14.5621367187496 秒, 结果 2568 位

 


相关文章
对该文的评论
kmzs ( 2004-05-29)
8错