声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2875|回复: 0

[VB] [分享]大整数的阶乘计算

[复制链接]
发表于 2005-9-12 09:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
<P>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))</P>
<P>考虑到当n很大时,输出具体的结果并无多大实际意义,遂另写代码如下:</P>
<P>Sub calcfactorial(ByVal num As Long, Optional ByRef POWER As String, Optional ByRef FIRSTNUM As String)<BR>Dim I As Long, TEMP As Double, temp2 As Long, STIME As Single<BR>TEMP = 0<BR>temp2 = 0<BR>STIME = Timer<BR>For I = 1 To num<BR>TEMP = TEMP + Log(I) / Log(10)<BR>If TEMP &gt; 1000000 Then<BR>temp2 = temp2 + 1<BR>TEMP = TEMP - 1000000<BR>End If<BR>Next<BR>POWER = Format(Int(TEMP + 1), "000000")<BR>TEMP = TEMP - Val(POWER)<BR>POWER = temp2 &amp; POWER<BR>If Val(POWER) &lt; 10 ^ 6 Then POWER = Val(POWER)<BR>FIRSTNUM = Left(Replace(10 ^ (TEMP), ".", ""), 10)<BR>Debug.Print Right(Space(9) &amp; num, 9) &amp; "! : 用时 " &amp; Right(Space(8) &amp; Format(Timer - STIME, "0.00000"), 8) &amp; " 秒, 结果 " &amp; Right(Space(10) &amp; POWER, 10) &amp; " 位,前10位为 " &amp; FIRSTNUM<BR>End Sub</P>
<P><BR>Private Sub Command1_Click()<BR>Dim I As Long, J As Long<BR>For J = 2 To 7<BR>For I = 1 To 10<BR>calcfactorial I * 10 ^ J<BR>Next<BR>Debug.Print<BR>Next<BR>End Sub</P>
回复
分享到:

使用道具 举报

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-12-26 00:07 , Processed in 0.085063 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表