大数阶乘的计算(六)[1]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 northwolves 的 blog

前些时间写的关于大整数的阶乘计算,效率总是再难以提高:(http://dev.csdn.net/develop/article/28/28306.shtmhttp://dev.csdn.net/develop/article/28/28308.shtmhttp://dev.csdn.net/develop/article/28/28432.shtmhttp://dev.csdn.net/develop/article/28/28641.shtm

今日突然想到

n!=1*2*3*....*n     →    lg(n!)=lg(1*2*3*....*n)=lg(1)+lg(2)+ lg(3)+..+lg(n)     →    n!=10^(lg(1)+lg(2)+ lg(3)+..+lg(n))

考虑到当n很大时,输出具体的结果并无多大实际意义,遂另写代码如下:

 

sub calcfactorial(byval num as long, optional byref power as string, optional byref firstnum as string)
dim i as long, temp as double, temp2 as long, stime as single
temp = 0
temp2 = 0
stime = timer
for i = 1 to num
temp = temp + log(i) / log(10)
if temp > 1000000 then
temp2 = temp2 + 1
temp = temp - 1000000
end if
next
power = format(int(temp + 1), "000000")
temp = temp - val(power)
power = temp2 & power
if val(power) < 10 ^ 6 then power = val(power)
firstnum = left(replace(10 ^ (temp), ".", ""), 10)
debug.print right(space(9) & num, 9) & "! : 用时 " & right(space(8) & format(timer - stime, "0.00000"), 8) & " 秒, 结果 " & right(space(10) & power, 10) & " 位,前10位为 " & firstnum
end sub


private sub command1_click()
dim i as long, j as long
for j = 2 to 7
for i = 1 to 10
calcfactorial i * 10 ^ j
next
debug.print
next
end sub

输出结果:

'      100! : 用时  0.00000 秒, 结果        158 位,前10位为 9332621544
'      200! : 用时  0.00000 秒, 结果        375 位,前10位为 7886578673
'      300! : 用时  0.00000 秒, 结果        615 位,前10位为 3060575122
'      400! : 用时  0.00000 秒, 结果        869 位,前10位为 6403452284
'      500! : 用时  0.00000 秒, 结果       1135 位,前10位为 1220136825
'      600! : 用时  0.00000 秒, 结果       1409 位,前10位为 1265572316
'      700! : 用时  0.01563 秒, 结果       1690 位,前10位为 2422040124
'      800! : 用时  0.00000 秒, 结果       1977 位,前10位为 7710530113
'      900! : 用时  0.00000 秒, 结果       2270 位,前10位为 6752680220
'     1000! : 用时  0.00000 秒, 结果       2568 位,前10位为 4023872600
'
'     1000! : 用时  0.00000 秒, 结果       2568 位,前10位为 4023872600
'     2000! : 用时  0.01563 秒, 结果       5736 位,前10位为 3316275092
'     3000! : 用时  0.00000 秒, 结果       9131 位,前10位为 4149359603
'     4000! : 用时  0.00000 秒, 结果      12674 位,前10位为 1828801951
'     5000! : 用时  0.00000 秒, 结果      16326 位,前10位为 4228577926
'     6000! : 用时  0.01563 秒, 结果      20066 位,前10位为 2683999765
'     7000! : 用时  0.00000 秒, 结果      23878 位,前10位为 8842007956
'     8000! : 用时  0.00000 秒, 结果      27753 位,前10位为 5184181060
'     9000! : 用时  0.01563 秒, 结果      31682 位,前10位为 8099589986
'    10000! : 用时  0.00000 秒, 结果      35660 位,前10位为 2846259680
'
'    10000! : 用时  0.00000 秒, 结果      35660 位,前10位为 2846259680
'    20000! : 用时  0.00000 秒, 结果      77338 位,前10位为 1819206320
'    30000! : 用时  0.04688 秒, 结果     121288 位,前10位为 2759537248
'    40000! : 用时  0.01563 秒, 结果     166714 位,前10位为 2091692420
'    50000! : 用时  0.03125 秒, 结果     213237 位,前10位为 3347320501
'    60000! : 用时  0.01563 秒, 结果     260635 位,前10位为 1564137704
'    70000! : 用时  0.03125 秒, 结果     308760 位,前10位为 1176812415

本文关键:阶乘
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top