楼主: landry.zhao
2973 3

[问答] 阶乘 [推广有奖]

  • 1关注
  • 0粉丝

大专生

78%

还不是VIP/贵宾

-

威望
0
论坛币
14 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
1834 点
帖子
70
精华
0
在线时间
56 小时
注册时间
2011-3-21
最后登录
2018-5-13

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
请问怎么在Excel里做大于300的阶乘呀?谢谢啦!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:EXCEL xcel exce cel 阶乘 EXCEL 大于 300

沙发
ryusukekenji 发表于 2011-5-3 14:19:34 |只看作者 |坛友微信交流群
瞧一瞧
http://amulee.bokee.com/6960395.html

如何计算大数阶乘的精确值?
关键词: 大数    阶乘    VBA                                          

在Excel VBA中,虽然可以计算大数的阶乘,但是其精度仅10多位。对于大数的阶乘精确值却无能为力。其实,我们可以利用数组存放每一位的数字,来模拟乘法的运算计算大数的阶乘。代码如下:
  1. Sub MyFact()
  2.     Dim ArrJG, ArrTemp
  3.     Dim SumA As Double
  4.     Dim i&, j&
  5.     Dim DNumber '目标阶乘
  6.     Dim strJG$  '结果输出
  7.     DNumber = 100   '要计算的阶乘
  8.     '计算阶乘长度
  9.     SumA = 1
  10.     For i = 1 To DNumber
  11.         SumA = Log(i) / Log(10) + SumA
  12.     Next i
  13.     ReDim ArrJG(Int(SumA) - 1)
  14.     ReDim ArrTemp(UBound(ArrJG))
  15.     '初始化结果数组
  16.     ArrJG(0) = 1
  17.     '计算阶乘结果
  18.     For i = 2 To DNumber
  19.         For j = 0 To UBound(ArrJG)
  20.             ArrTemp(j) = ArrJG(j) * i
  21.         Next j
  22.         j = 0
  23.         Do While j <= UBound(ArrJG)
  24.             ArrJG(j) = ArrTemp(j) Mod 10
  25.             If ArrTemp(j) >= 10 Then ArrTemp(j + 1) = ArrTemp(j + 1) + ArrTemp(j) \ 10
  26.             j = j + 1
  27.         Loop
  28.     Next i
  29.     '结果输出
  30.     For i = UBound(ArrJG) To 0 Step -1
  31.         strJG = strJG & ArrJG(i)
  32.     Next i
  33.     MsgBox DNumber & "的阶乘为:" & strJG
  34. End Sub
复制代码

使用道具

藤椅
ryusukekenji 发表于 2011-5-3 14:21:59 |只看作者 |坛友微信交流群
再瞧一瞧这个链接,希望对你有帮助。
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.
  1. Option Explicit
  2. Private Function cacl (num As Long) As String
  3. Dim numlen As Long, last As Long, x As Long
  4. Dim i As Long, m As Long, n As Long
  5. Dim result () As Long, starttime As Single
  6. numlen = 1
  7. starttime = Timer
  8. ReDim result (1 To numlen)
  9. result (1) = 1
  10. x = 1
  11. Do While x <= num
  12. last = 0
  13. For i = 1 To numlen
  14. m = result (i) * x + last
  15. result (i) = m Mod 10
  16. last = m \ 10
  17. Next
  18. If last> 0 Then
  19. n = Len (CStr (last))
  20. ReDim Preserve result (1 To numlen + n)
  21. For i = 1 To n
  22. result (numlen + i) = last Mod 10
  23. last = last \ 10
  24. Next
  25. numlen = UBound (result)
  26. End If
  27. x = x + 1
  28. Loop
  29. ReDim s (1 To numlen)
  30. For i = 1 To numlen
  31. s (i) = result (numlen + 1 - i)
  32. Next
  33. cacl = Join (s, "")
  34. Debug.Print num & "!: When"; Timer - starttime & "seconds, the result of" & numlen & "bit"
  35. End Function

  36. Private Sub Command1_Click ()
  37. Dim i As Long
  38. For i = 1 To 10
  39. cacl i * 100
  40. Next
  41. ''''For i = 1 To 10
  42. ''''cacl i * 1000
  43. ''''Next
  44. 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.

使用道具

板凳
landry.zhao 发表于 2011-5-5 09:21:58 |只看作者 |坛友微信交流群
谢谢R大人!原来这么复杂呢呀。。。

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-1 14:58