再瞧一瞧这个链接,希望对你有帮助。
http://tutorialsto.com/index.php ... arge-numbers-d.html
The calculation of factorial of large numbers (d)
Print View by: iSee Total views: 45 Word Count: 553 Date: Fri, 22 May 2009 Time: 2:06 PM
Read northwolves article the calculation of factorial of large numbers (c)
http://dev.csdn.net/article/28/28432.shtm very enlightening, reminiscent of the past I have written a similar function, methods and its almost all is a dynamic array by way of digital preservation. Efficient than northwolves poor brother to some, about a difference of 50%; thought factorial algorithm is the fastest, so this article is to see is admirable!
However, I think that this algorithm can also optimize the room, so, once again done something new to try, do not want a big success, efficiency than the calculation of factorial of large numbers (c) once again increased more than 5 times, and algorithm is simple and amazingly, they did not dareimmediately posted to share with you.
- 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 & "!: When"; Timer - starttime & "seconds, the result of" & numlen & "bit"
- 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
复制代码Output: (My machine is running "calculation of (c)" when, instead of the result of considerable, it should be with the northwolves brother, with the level of the machine)
100! : 0 seconds when the results 158
200! : 1.171875E-02 Time seconds, the results of 375
300! : 3.515625E-02 Time seconds, the results of 615
400! : 4.296875E-02 Time seconds, the results of 869
500! : 8.203125E-02 Time seconds, the results of 1135
600! : .1054688 Seconds when a result, 1409
700! : .15625 Seconds when a result, 1690
800! : .1992188 Seconds when a result, 1977
900! : .2617188 Seconds when a result, 2270
1000! : .3632813 Seconds when a result, 2568
2000! : 1.53125 seconds with the time, the results of 5736
3000! : Used 3.75 seconds, resulting in 9131
4000! : 7.171875 seconds when a result, 12,674
5000! : 11.45313 seconds when a result, 16,326
6000! : 16.41016 seconds when a result, 20,066
7000! : 22.83984 seconds when a result, 23,878
8000! : 30.375 seconds used, resulting in 27,753
9000! : Used 41.75 seconds, resulting in 31,682
10000! : 54.57813 seconds when a result, 35,660
The output have been a bit slow because I am the first test in the 10000! Only spent 47 seconds. I wonder if there will be a faster algorithm.