博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
零基础学算法->阶乘
阅读量:5882 次
发布时间:2019-06-19

本文共 3256 字,大约阅读时间需要 10 分钟。

hot3.png

阶乘

在数学中,正整数的阶乘(英语:factorial)是所有小于及等于该数的正整数的积,计为n!

例如5的阶乘为5!,其值=120; 5!=5x4x3x2x1=120

用递归计算阶乘

一般数字的阶乘可以用递归的方法进行计算。

以下用递归计算1~12阶乘值;阶乘的值是以指数进行增长。

cd529ef68b572769b05aeef2475ffacc729.jpg

Sub Factorial_Main()Dim num%, m%Dim FactorNum&With Sheet9For num = 1 To 12    m = 5 + num    'if num>13 program will overflow    FactorNum = Factorial_Recursion(num)    .Cells(m, 1) = num    .Cells(m, 2) = FactorNumNext numEnd WithDebug.Print TimerEnd Sub'Factorial by Recursion MethodFunction Factorial_Recursion(ByVal n)If n <= 1 Then    Factorial_Recursion = 1    Exit FunctionElse    Factorial_Recursion = n * Factorial_Recursion(n - 1)End IfEnd Function

大数阶乘

一般阶乘的值都比较大,这时递归的方法不可行。此时需要用数组保存阶乘结果来进行计算。

思路: 

 1. 当前值乘以数组中的值
 2. 将结果保存到数组中
 3. 判断每个元素是否需要进位
 4. 使用一个数组来保存阶乘每一位结果

cefb3de79a9bab435dc29418b0bf66f207a.jpg

3ba69b3e4a45f8252420b3ab9d558ad97e9.jpg

以下是1~100的阶乘

39112ec251f0b8b6132fc251604e1d0add8.jpg

具体代码如下:

'log10:https://baike.baidu.com/item/%E5%AF%B9%E6%95%B0%E5%87%BD%E6%95%B0/6013318?fr=aladdin'Calculate the CarryFunction Carry_Bit(ByRef Bit, ByVal Pos)Dim i&Dim CarryCarry = 0                       'InitialFor i = 1 To Pos                'Check 1~Pos whether need carry bit    Bit(i) = Bit(i) + Carry    'Current value+Carry value    If Bit(i) <= 9 Then         '<9 no need carray bit        Carry = 0    ElseIf Bit(i) > 9 And i < Pos Then   '>9 but 
9 And i >= Pos Then '>9 and highest digit Do While Bit(i) > 9 Carry = Int(Bit(i) / 10) 'Save carry Bit(i) = Bit(i) Mod 10 'one digit i = i + 1 Bit(i) = Carry 'Save the carry Loop End IfNext iCarry_Bit = Bit 'Return the ArrayEnd Function'Calculate multiple number factor'Loop mSub Factorial_BigNumber_Multiple()Dim numDim Digit, sumDim i, jDim Fact()Dim Count, Str$Dim m, nWith Sheet10For m = 10 To 43 sum = 0: Digit = 0 num = .Cells(m, 10).Value 'Input Value For i = 1 To num 'Calculate Result digit sum = Log10(i) + sum Next i Digit = Int(sum) + 1 'Length of Data ReDim Fact(1 To Digit) 'Allocate Array For i = 1 To Digit 'Initial Array Fact(i) = 0 Next i Fact(1) = 1 'Setup 1st digit For i = 1 To num 'Loop fact num Pos = Highest_Digit(Fact, Digit) 'Record highest digit For j = 1 To Pos Fact(j) = Fact(j) * i 'fact(n-1)xN Next j Fact = Carry_Bit(Fact, Pos) 'process carry bit Next i Pos = Highest_Digit(Fact, Digit) 'Record highest digit' m = 10 n = 12 'Initial i = Pos Str = "'" Count = 0 Do While i <= Pos And i >= 1 Count = Count + 1 Str = Str & Fact(i) If Count Mod 5 = 0 Then .Cells(m, n) = Str Str = "'" n = n + 1 End If If i = 1 And Str <> "" Then .Cells(m, n) = Str i = i - 1 Loop .Cells(m, 11) = CountNext mEnd WithDebug.Print TimerEnd Sub'Find Hightest digitFunction Highest_Digit(Arr, n)Dim iFor i = n To 1 Step -1 If Arr(i) <> 0 Then Highest_Digit = i Exit Function End IfNext iEnd Function'Function for Log10Function Log10(ByVal x) As DoubleDim nLog10nLog10 = Log(x) / Log(10)Log10 = nLog10End Function

 

转载于:https://my.oschina.net/tedzheng/blog/3019815

你可能感兴趣的文章
一种Web服务的go语言实现
查看>>
转载-- 魔兽哈希算法封装和测试
查看>>
下载文件
查看>>
(算法)排序
查看>>
Ajax传值原理.aspx文档
查看>>
一个微软的DDD架构图
查看>>
使用git时出现Please make sure you have the correct access rights and the repository exists.问题已解决。...
查看>>
分析称微软不应再为WP弱势地位推脱 借口已失效
查看>>
Spring核心实现篇
查看>>
css动态样式
查看>>
Sql Server查询性能优化之走出索引的误区
查看>>
回忆下高中的数学归纳法
查看>>
CentOS7 'Username' is not in the sudoers file. This incident will be reported
查看>>
HashMap,HashTable,HashSet区别(转)
查看>>
关于php上传文件
查看>>
360浏览器内核控制标签meta说明
查看>>
android AsyncTask
查看>>
poj 3225 【线段树】
查看>>
序列化
查看>>
Linux 查询命令
查看>>