计算大数阶乘--VB Script 版
生活随笔
收集整理的這篇文章主要介紹了
计算大数阶乘--VB Script 版
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
和Basic版相比,這個(gè)版本有以下幾處不同點(diǎn)
這次采用的不是靜態(tài)數(shù)組,而是動(dòng)態(tài)數(shù)組,先定義一個(gè)數(shù)組“Dim buff()”,在運(yùn)行過(guò)程中再根據(jù)需要擴(kuò)展大小。
計(jì)算結(jié)果沒(méi)有直接輸出,而是寫到文件,因?yàn)楫?dāng)字符串太長(zhǎng)時(shí),MsgBox的會(huì)丟失部分內(nèi)容
難點(diǎn):確定n!的位數(shù)是難點(diǎn),ln(n!) 可用 斯特林逼近 來(lái)計(jì)算,ln(n!)= (n+0.5)ln(n)- n + 0.5*ln(2*pi)
請(qǐng)參考 http://mathworld.wolfram.com/StirlingsApproximation.html
下面為源代碼
Dim n,i,j,c,t,len1 Dim buff() Dim numstrConst RAD=10000 Const PI=3.1415926535897932384626433832795numstr= InputBox( "please input a numer n(n>1)", "calculate the n!", 1)if numstr > "" and IsNumeric( numstr ) Thenn=CLng(numstr)log_facn=(n+0.5)*Log(n)-n+0.5*Log(2*PI) 'log_facn=ln(n!)ReDim buff(log_facn/Log(RAD)+1) 'Expand array size for store n!buff(1)=1len1=1For i=1 to nc=CLng(0)For j= 1 to len1t=CLng(buff(j))* CLng(i)+cbuff(j)=t MOD RADc=t \ RADNextIf c>0 Thenlen1=len1+1buff(len1)=c End If Next'Caculation is completed, the following code for write the result to filefilename=".\" & CStr(n) & ".txt"Set fs = CreateObject("Scripting.FileSystemObject")Set a = fs.CreateTextFile(filename, True)numstr=CStr(buff(len1))For j=len1-1 to 1 step -1t=CStr(buff(j))If Len(t)<4 Thennumstr=numstr & String(4-Len(t),"0") End Ifnumstr=numstr & tIf Len(numstr) > 76 Thena.WriteLine(numstr)numstr=""End if NextIf (Len(numstr)>0) Thena.WriteLine(numstr)End Ifa.closeMsgBox "Result have been stored to file " & filename,0,"calculate n!" ElseMsgBox "Invalid input" End if
參考學(xué)習(xí)資料
1. VBScript 教程,可從VBScript http://download.pchome.net/development/reference/detail-11108.html
2. VB script 腳本語(yǔ)言.doc
總結(jié)
以上是生活随笔為你收集整理的计算大数阶乘--VB Script 版的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python去噪音_python中的噪声
- 下一篇: 【SPSS】SPSS之主成分分析及因子分