pythonプログラミングはnの階乗を求めます_Pythonでプログラミングした階乗


pythonプログラミングはnの階乗を求めます
Before we start implementing factorial using Python, let us first discuss what factorial of a number implies.
Pythonを用いて階乗を実現する前に,まずデジタル階乗の意味について議論する.
Theoretically, the factorial of a number is defined as the product of all positive integers less than or equal to the number. Certainly, ‘n!’ represents the factorial of an integer ‘n’. As an example, let us look at the factorial of the number 6,
理論的には、数値の乗算は、その数値より小さいまたは等しいすべての正の整数の積として定義される.もちろん、「n!」整数'n'を表す階乗.たとえば、数字6の階乗を見てみましょう
6! = 6 * 5 * 4 * 3 * 2 * 1
6! = 6 * 5 * 4 * 3 * 2 * 1
The following techniques could be followed to determine the factorial of an integer.
整数の乗算は、以下の技術に従って決定することができる.
  • Using Loop使用サイクル
  • Using Recursive function call再帰関数呼び出し
  • Using predefined function'factorial()'from the math module数学モジュールの事前定義関数'factorial()'
  • を使用
    Pythonでの使用サイクル(Using Loop in Python)
    The below-mentioned code illustrates how we can calculate the factorial of a given number using for loop in Python programming.
    以下のコードは、Pythonプログラミングでforループを使用して、所与の数値の乗算を計算する方法を説明する.
    
    n=9
    fact=1
    for i in range(2,n+1):
        fact=fact*i
    print("The factorial of ",n," is: ",fact)
    

    Output:
    出力:
    
    The factorial of 9 is: 362880
    

    Pythonでの再帰関数呼び出しの使用(Using Recursion function call in Python)
    Similarly, we can also calculate the factorial of a given number using a Recursive function. Let us see how
    同様に,再帰関数を用いて所与の数値の階乗を計算することもできる.見てみましょう
    
    n=9
    def fact(n):
        if(n==1 or n==0):
            return 1
        else:
            return n*fact(n-1)
    
    print("The factorial of ",n," is: ",fact(n))
    

    Output
    しゅつりょくりょう
    
    The factorial of 9 is: 362880
    

    For a clear understanding of functions and recursion, one can refer to
    関数と再帰を明確に理解するには、
    Python Function and ArgumentsPython Recursion Function
    Python関数とパラメータPython再帰関数
    PythonでMathモジュールのfactorial()メソッド(Using the factorial()method from the math module in Python)を使用する
    The math module provides a simple way to calculate the factorial of any positive integer. Certainly, the module comes with a pre-defined method ‘factorial()’ which takes in the integer as an argument and returns the factorial of the number. Let’s take a look at how we can use the pre-defined method and consequently find the factorial. The code given below depicts how the method ‘factorial()‘ can be used
    数学モジュールは、任意の正の整数の乗算を計算するための簡単な方法を提供する.もちろん、このモジュールには、整数をパラメータとして数値の乗算を返す事前定義されたメソッド'factorial()があります.事前定義されたメソッドを使用して乗算を見つける方法を見てみましょう.次のコードでは、メソッド'factorial()'の使用方法について説明します.
    
    import math
    n=9
    print("The factorial of ",n," is: ",math.factorial(n))
    

    Output:
    出力:
    
    The factorial of 9 is: 362880
    

    Furthermore, in the case of all of the above-mentioned techniques, we have used a pre-defined value of the integer ‘n’. Also making ‘n’ a user input is possible. This could be easily achieved by substituting the line ‘n=9’ with:
    また,上述したすべての技術の場合,整数「n」の事前定義値を用いた.ユーザーに「n」と入力させることもできます.行n=9を次のように置き換えます.
    
    n=int(input("Enter the number for calculating factorial"))
    

    The Python input function is covered in further detail in one of our previous articles.
    前の記事ではPython入力関数についてさらに詳しく説明しました.
    References:
    参考文献:
    https://stackoverflow.com/questions/5136447/function-for-factorial-in-python
    https://stackoverflow.com/questions/5136447/function-for-factorial-in-python
    https://stackoverflow.com/questions/20604185/find-the-best-way-for-factorial-in-python
    https://stackoverflow.com/questions/20604185/find-the-best-way-for-factorial-in-python
    翻訳:https://www.journaldev.com/34688/factorial-using-python-programming
    pythonプログラミングはnの階乗を求めます