|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
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 > 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 & POWER<BR>If Val(POWER) < 10 ^ 6 Then POWER = Val(POWER)<BR>FIRSTNUM = Left(Replace(10 ^ (TEMP), ".", ""), 10)<BR>Debug.Print Right(Space(9) & num, 9) & "! : 用时 " & Right(Space(8) & Format(Timer - STIME, "0.00000"), 8) & " 秒, 结果 " & Right(Space(10) & POWER, 10) & " 位,前10位为 " & 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> |
|