看了 的文章大数阶乘的计算(三) http://dev.csdn.net/article/28/28432.shtm 很有启发,联想起以前自己编写过一个类似的函数,方法与其差不多,都是动态数组的增位保存方式。效率上比兄要差一些,大约相差50%;当时以为是最快的阶乘算法了,所以看到这篇文章很是钦佩!
不过,我总觉得,这个算法还有可优化的余地,于是,又重新做了点新尝试,不想大获成功,效率比大数阶乘的计算(三)一下子又提高了5倍多,而且算法简单得惊人,故不敢独吞,赶紧贴出与大家共享。
option explicit
private function cacl(num as long) as string
dim numlen as long, last as long, x as long
dim i as long, m as long, n as long
dim result() as long, starttime as single
numlen = 1
starttime = timer
redim result(1 to numlen)
result(1) = 1
x = 1
do while x <= num
last = 0
for i = 1 to numlen
m = result(i) * x + last
result(i) = m mod 10
last = m \ 10
next
if last > 0 then
n = len(cstr(last))
redim preserve result(1 to numlen + n)
for i = 1 to n
result(numlen + i) = last mod 10
last = last \ 10
next
numlen = ubound(result)
end if
x = x + 1
loop
redim s(1 to numlen)
for i = 1 to numlen
s(i) = result(numlen + 1 - i)
next
cacl = join(s, "")
debug.print num & "! : 用时 "; timer - starttime & " 秒, 结果 " & numlen & " 位"
end function
private sub command1_click()
dim i as long
for i = 1 to 10
cacl i * 100
next
'for i = 1 to 10
'cacl i * 1000
'next
end sub
输出结果:(我的机器在运行“计算(三)”时,与其结果相当,所以应是与兄,同档次的机器)
100! : 用时 0 秒, 结果 158 位
200! : 用时 1.171875e-02 秒, 结果 375 位
300! : 用时 3.515625e-02 秒, 结果 615 位
400! : 用时 4.296875e-02 秒, 结果 869 位
500! : 用时 8.203125e-02 秒, 结果 1135 位
600! : 用时 .1054688 秒, 结果 1409 位
700! : 用时 .15625 秒, 结果 1690 位
800! : 用时 .1992188 秒, 结果 1977 位
900! : 用时 .2617188 秒, 结果 2270 位
1000! : 用时 .3632813 秒, 结果 2568 位
2000! : 用时 1.53125 秒, 结果 5736 位
3000! : 用时 3.75 秒, 结果 9131 位
4000! : 用时 7.171875 秒, 结果 12674 位
5000! : 用时 11.45313 秒, 结果 16326 位
6000! : 用时 16.41016 秒, 结果 20066 位
7000! : 用时 22.83984 秒, 结果 23878 位
8000! : 用时 30.375 秒, 结果 27753 位
9000! : 用时 41.75 秒, 结果 31682 位
10000! : 用时 54.57813 秒, 结果 35660 位
这次输出慢了些,因为我在第一次测试10000!时,只用了47秒。不知会不会还有更快的算法。