1 文本格式
/// <summary>
/// 阶乘的非递归算法
/// </summary>
/// <param name="a"></param>
/// <returns></returns>
private int Factorial_Original(int a)
{
int r = 1;
for (int i = a; i > 1; i--)
{
r = r * i;
}
return r;
}
/// <summary>
/// 阶乘的递归算法
/// 递归简单理解就是函数调用自己(当然参数不同哈!)
/// </summary>
/// <param name="a"></param>
/// <returns></returns>
private int Factorial(int a)
{
if (a > 1) return a * Factorial(a - 1);
else return 1;
}
/// <summary>
/// 《小白学程序》第十一课:阶乘(Factorial)的计算方法与代码
/// 阶乘是基斯顿·卡曼(Christian Kramp,1760~1826)于 1808 年发明的运算符号,是数学术语。
/// 一个正整数的阶乘(factorial)是所有小于及等于该数的正整数的积,并且0的阶乘为1。自然数n的阶乘写作n!。
/// 1808年,基斯顿·卡曼引进这个表示法。亦即 n! = 1×2×3×...×(n-1)×n。
/// 阶乘亦可以递归方式定义:
/// 0! = 1
/// n! = (n-1)! × n
///
/// 本节课接触了函数(阶乘函数)。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button11_Click(object sender, EventArgs e)
{
int n = 4;
StringBuilder sb = new StringBuilder();
sb.AppendLine("非递归算法:" + n + "! = " + Factorial_Original(n) + "<br>");
sb.AppendLine("递归算法:" + n + "! = " + Factorial(n) + "<br>");
webBrowser1.DocumentText = sb.ToString();
}
2 代码格式
/// <summary>
/// 阶乘的非递归算法
/// </summary>
/// <param name="a"></param>
/// <returns></returns>
private int Factorial_Original(int a)
{
int r = 1;
for (int i = a; i > 1; i--)
{
r = r * i;
}
return r;
}
/// <summary>
/// 阶乘的递归算法
/// 递归简单理解就是函数调用自己(当然参数不同哈!)
/// </summary>
/// <param name="a"></param>
/// <returns></returns>
private int Factorial(int a)
{
if (a > 1) return a * Factorial(a - 1);
else return 1;
}
/// <summary>
/// 《小白学程序》第十一课:阶乘(Factorial)的计算方法与代码
/// 阶乘是基斯顿·卡曼(Christian Kramp,1760~1826)于 1808 年发明的运算符号,是数学术语。
/// 一个正整数的阶乘(factorial)是所有小于及等于该数的正整数的积,并且0的阶乘为1。自然数n的阶乘写作n!。
/// 1808年,基斯顿·卡曼引进这个表示法。亦即 n! = 1×2×3×...×(n-1)×n。
/// 阶乘亦可以递归方式定义:
/// 0! = 1
/// n! = (n-1)! × n
///
/// 本节课接触了函数(阶乘函数)。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button11_Click(object sender, EventArgs e)
{
int n = 4;
StringBuilder sb = new StringBuilder();
sb.AppendLine("非递归算法:" + n + "! = " + Factorial_Original(n) + "<br>");
sb.AppendLine("递归算法:" + n + "! = " + Factorial(n) + "<br>");
webBrowser1.DocumentText = sb.ToString();
}
3 局限性
咱们尝试着计算其他数据的阶乘:
16! = 2004189184
17! = -288522240
可见,上面的算法无法计算超过 16 的阶乘!!!!
将数据类型改为 long 可以计算更大的阶乘。
private long Factorial(long a)
{
if (a > 1) return a * Factorial(a - 1);
else return 1;
}
20! = 2432902008176640000
21! = -4249290049419214848
超过 20 又不行了!
怎么办?
后面学习 大数的乘法,可计算很大数的阶乘。
4 512 阶乘
512! = 347728979313260536328304591754560471199225065564351457034247483155161041206635254347320985033950225364432243311021394545295001702070069013264153113260937941358711864044716186861040899557497361427588282356254968425012480396855239725120562512065555822121708786443620799246550959187232026838081415178588172535280020786313470076859739980965720873849904291373826841584712798618430387338042329771801724767691095019545758986942732515033551529595009876999279553931070378592917099002397061907147143424113252117585950817850896618433994140232823316432187410356341262386332496954319973130407342567282027398579382543048456876800862349928140411905431276197435674603281842530744177527365885721629512253872386613118821540847897493107398381956081763695236422795880296204301770808809477147632428639299038833046264585834888158847387737841843413664892833586209196366979775748895821826924040057845140287522238675082137570315954526727437094904914796782641000740777897919134093393530422760955140211387173650047358347353379234387609261306673773281412893026941927424000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
5 1024阶乘
1024! = 541852879605885728307692194468385473800155396353801344448287027068321061207337660373314098413621458671907918845708980753931994165770187368260454133333721939108367528012764993769768292516937891165755680659663747947314518404886677672556125188694335251213677274521963430770133713205796248433128870088436171654690237518390452944732277808402932158722061853806162806063925435310822186848239287130261690914211362251144684713888587881629252104046295315949943900357882410243934315037444113890806181406210863953275235375885018598451582229599654558541242789130902486944298610923153307579131675745146436304024890820442907734561827369030502252796926553072967370990758747793127635104702469889667961462133026237158973227857814631807156427767644064591085076564783456324457736853810336981776080498707767046394272605341416779125697733374568037475186676265961665615884681450263337042522664141862157046825684773360944326737493676674915098953768112945831626643856479027816385730291542667725665642276826058264393884514911976419675509290208592713156362983290989441052732125187249527501314071676405516936190781821236701912295767363117054126589929916482008515781751955466910902838729232224509906388638147771255227782631322385756948819393658889908993670874516860653098411020299853816281564334981847105777839534742531499622103488807584513705769839763993103929665046046121166651345131149513657400869056334867859885025601787284982567787314407216524272262997319791568603629406624740101482697559533155736658800562921274680657285201570401940692285557800611429055755324549794008939849146812639860750085263298820224719585505344773711590656682821041417265040658600683844945104354998812886801316551551714673388323340851763819713591312372548673734783537316341517369387565212899726597964903241208727348690699802996369265070088758384854547542272771024255049902319275830918157448205196421072837204937293516175341957775422453152442280391372407717891661203061040255830055033886790052116025408740454620938384367637886658769912790922323717371343176067483352513629123362885893627132294183565884010418727869354439077085278288558308427090461075019007184933139915558212752392329879780649639075333845719173822840501869570463626600235265587502335595489311637509380219119860471335771652403999403296360245577257963673286654348957325740999710567131623272345766761937651408103999193633908286420510098577454524068106897392493138287362226257920000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000