C#, Lesson 13 of "Xiaobai Learning Program": Calculation method and code of factorial (Factorial)

1 text format


/// <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>
/// Factorial recursive algorithm
/// The simple understanding of recursion is that the function calls itself (of course the parameters are different!)
/// </summary>
/// <param name="a"></ param>
/// <returns></returns>
private int Factorial(int a)
{     if (a > 1) return a * Factorial(a - 1);     else return 1; }


/// <summary>
/// Lesson 11 of "Xiaobai Learning Program": Calculation method and code of Factorial
/// Factorial was created by Christian Kramp (1760~1826) in 1808 The operation symbol invented in 1999 is a mathematical term.
/// The factorial of a positive integer is the product of all positive integers less than or equal to the number, and the factorial of 0 is 1. The factorial of a natural number n is written as n!.
/// In 1808, Christian Carman introduced this notation. That is n! = 1×2×3×...×(n-1)×n.
/// Factorial can also be defined recursively:
/// 0! = 1
/// n! = (n-1)! × n
/// 
/// This lesson has touched on functions (factorial functions).
/// </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("Non-recursive algorithm: " + n + "! = " + Factorial_Original(n) + "<br>");
    sb.AppendLine("Recursive algorithm: " + n + "! = " + Factorial(n) + "<br>");
    webBrowser1. DocumentText = sb. ToString();
}
 

2 code format


/// <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 Limitations

Let's try to calculate the factorial of other data:

16! = 2004189184

17! = -288522240

It can be seen that the above algorithm cannot calculate factorials over 16! ! ! !

Changing the data type to long allows larger factorials to be computed.

private long Factorial(long a)
{
   if (a > 1) return a * Factorial(a - 1);
   else return 1;
}

20! = 2432902008176640000

21! = -4249290049419214848

No more than 20!

what to do?

After learning the multiplication of large numbers, you can calculate the factorial of large numbers.

4 512 factorial 

512! = 347728979313260536328304591754560471199225065564351457034247483155161041206635254347320985033950225364432243311021394545295001702070069013264153113260937941358711864044716186861040899557497361427588282356254968425012480396855239725120562512065555822121708786443620799246550959187232026838081415178588172535280020786313470076859739980965720873849904291373826841584712798618430387338042329771801724767691095019545758986942732515033551529595009876999279553931070378592917099002397061907147143424113252117585950817850896618433994140232823316432187410356341262386332496954319973130407342567282027398579382543048456876800862349928140411905431276197435674603281842530744177527365885721629512253872386613118821540847897493107398381956081763695236422795880296204301770808809477147632428639299038833046264585834888158847387737841843413664892833586209196366979775748895821826924040057845140287522238675082137570315954526727437094904914796782641000740777897919134093393530422760955140211387173650047358347353379234387609261306673773281412893026941927424000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

5 1024 factorial

1024! = 541852879605885728307692194468385473800155396353801344448287027068321061207337660373314098413621458671907918845708980753931994165770187368260454133333721939108367528012764993769768292516937891165755680659663747947314518404886677672556125188694335251213677274521963430770133713205796248433128870088436171654690237518390452944732277808402932158722061853806162806063925435310822186848239287130261690914211362251144684713888587881629252104046295315949943900357882410243934315037444113890806181406210863953275235375885018598451582229599654558541242789130902486944298610923153307579131675745146436304024890820442907734561827369030502252796926553072967370990758747793127635104702469889667961462133026237158973227857814631807156427767644064591085076564783456324457736853810336981776080498707767046394272605341416779125697733374568037475186676265961665615884681450263337042522664141862157046825684773360944326737493676674915098953768112945831626643856479027816385730291542667725665642276826058264393884514911976419675509290208592713156362983290989441052732125187249527501314071676405516936190781821236701912295767363117054126589929916482008515781751955466910902838729232224509906388638147771255227782631322385756948819393658889908993670874516860653098411020299853816281564334981847105777839534742531499622103488807584513705769839763993103929665046046121166651345131149513657400869056334867859885025601787284982567787314407216524272262997319791568603629406624740101482697559533155736658800562921274680657285201570401940692285557800611429055755324549794008939849146812639860750085263298820224719585505344773711590656682821041417265040658600683844945104354998812886801316551551714673388323340851763819713591312372548673734783537316341517369387565212899726597964903241208727348690699802996369265070088758384854547542272771024255049902319275830918157448205196421072837204937293516175341957775422453152442280391372407717891661203061040255830055033886790052116025408740454620938384367637886658769912790922323717371343176067483352513629123362885893627132294183565884010418727869354439077085278288558308427090461075019007184933139915558212752392329879780649639075333845719173822840501869570463626600235265587502335595489311637509380219119860471335771652403999403296360245577257963673286654348957325740999710567131623272345766761937651408103999193633908286420510098577454524068106897392493138287362226257920000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Guess you like

Origin blog.csdn.net/beijinghorn/article/details/132591263