Hướng dẫn function of factorial in python - chức năng của giai thừa trong python

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận

    Không nhiều người biết, nhưng Python cung cấp một chức năng trực tiếp có thể tính toán giai thừa của một số mà không viết toàn bộ mã để tính toán.

    Phương pháp ngây thơ để tính toán giai thừa

    math.factorial(x)
    Parameters :
    x : The number whose factorial has to be computed.
    Return value :
    Returns the factorial of desired number.
    Exceptions : 
    Raises Value error if number is negative or non-integral.
    
    0____11
    math.factorial(x)
    Parameters :
    x : The number whose factorial has to be computed.
    Return value :
    Returns the factorial of desired number.
    Exceptions : 
    Raises Value error if number is negative or non-integral.
    
    2

    math.factorial(x)
    Parameters :
    x : The number whose factorial has to be computed.
    Return value :
    Returns the factorial of desired number.
    Exceptions : 
    Raises Value error if number is negative or non-integral.
    
    3
    math.factorial(x)
    Parameters :
    x : The number whose factorial has to be computed.
    Return value :
    Returns the factorial of desired number.
    Exceptions : 
    Raises Value error if number is negative or non-integral.
    
    1
    math.factorial(x)
    Parameters :
    x : The number whose factorial has to be computed.
    Return value :
    Returns the factorial of desired number.
    Exceptions : 
    Raises Value error if number is negative or non-integral.
    
    5

    math.factorial(x)
    Parameters :
    x : The number whose factorial has to be computed.
    Return value :
    Returns the factorial of desired number.
    Exceptions : 
    Raises Value error if number is negative or non-integral.
    
    6
    math.factorial(x)
    Parameters :
    x : The number whose factorial has to be computed.
    Return value :
    Returns the factorial of desired number.
    Exceptions : 
    Raises Value error if number is negative or non-integral.
    
    7
    math.factorial(x)
    Parameters :
    x : The number whose factorial has to be computed.
    Return value :
    Returns the factorial of desired number.
    Exceptions : 
    Raises Value error if number is negative or non-integral.
    
    8
    math.factorial(x)
    Parameters :
    x : The number whose factorial has to be computed.
    Return value :
    Returns the factorial of desired number.
    Exceptions : 
    Raises Value error if number is negative or non-integral.
    
    9
    The factorial of 23 is : 25852016738884976640000
    
    0
    math.factorial(x)
    Parameters :
    x : The number whose factorial has to be computed.
    Return value :
    Returns the factorial of desired number.
    Exceptions : 
    Raises Value error if number is negative or non-integral.
    
    5
    The factorial of 23 is : 25852016738884976640000
    
    223____
    math.factorial(x)
    Parameters :
    x : The number whose factorial has to be computed.
    Return value :
    Returns the factorial of desired number.
    Exceptions : 
    Raises Value error if number is negative or non-integral.
    
    5
    The factorial of 23 is : 25852016738884976640000
    
    5

    The factorial of 23 is : 25852016738884976640000
    
    6
    math.factorial(x)
    Parameters :
    x : The number whose factorial has to be computed.
    Return value :
    Returns the factorial of desired number.
    Exceptions : 
    Raises Value error if number is negative or non-integral.
    
    3
    math.factorial(x)
    Parameters :
    x : The number whose factorial has to be computed.
    Return value :
    Returns the factorial of desired number.
    Exceptions : 
    Raises Value error if number is negative or non-integral.
    
    1
    math.factorial(x)
    Parameters :
    x : The number whose factorial has to be computed.
    Return value :
    Returns the factorial of desired number.
    Exceptions : 
    Raises Value error if number is negative or non-integral.
    
    3
    The factorial of -5 is : 
    
    0
    The factorial of -5 is : 
    
    1

    The factorial of -5 is : 
    
    2
    The factorial of 23 is : 25852016738884976640000
    
    0
    The factorial of -5 is : 
    
    4
    The factorial of -5 is : 
    
    5
    math.factorial(x)
    Parameters :
    x : The number whose factorial has to be computed.
    Return value :
    Returns the factorial of desired number.
    Exceptions : 
    Raises Value error if number is negative or non-integral.
    
    1
    The factorial of -5 is : 
    
    7

    The factorial of 23 is : 25852016738884976640000
    

    The factorial of -5 is : 
    
    2
    The factorial of -5 is : 
    
    9

    Đầu ra:math” module of python. Because it has C type internal implementation, it is fast.

    math.factorial(x)
    Parameters :
    x : The number whose factorial has to be computed.
    Return value :
    Returns the factorial of desired number.
    Exceptions : 
    Raises Value error if number is negative or non-integral.
    

    Sử dụng math.factorial ()

    Phương pháp này được định nghĩa trong mô -đun Math Math của Python. Bởi vì nó có loại C thực hiện nội bộ, nó là nhanh.

    Traceback (most recent call last):
      File "/home/f29a45b132fac802d76b5817dfaeb137.py", line 9, in 
        print (math.factorial(-5))
    ValueError: factorial() not defined for negative values
    
    0
    Traceback (most recent call last):
      File "/home/f29a45b132fac802d76b5817dfaeb137.py", line 9, in 
        print (math.factorial(-5))
    ValueError: factorial() not defined for negative values
    
    1

    The factorial of -5 is : 
    
    2
    The factorial of 23 is : 25852016738884976640000
    
    0
    The factorial of -5 is : 
    
    4
    The factorial of -5 is : 
    
    5
    math.factorial(x)
    Parameters :
    x : The number whose factorial has to be computed.
    Return value :
    Returns the factorial of desired number.
    Exceptions : 
    Raises Value error if number is negative or non-integral.
    
    1
    The factorial of -5 is : 
    
    7

    The factorial of 23 is : 25852016738884976640000
    

    The factorial of -5 is : 
    
    2
    The factorial of -5 is : 
    
    9

    • Đầu ra:

      Sử dụng math.factorial ()

      Phương pháp này được định nghĩa trong mô -đun Math Math của Python. Bởi vì nó có loại C thực hiện nội bộ, nó là nhanh.

      Traceback (most recent call last):
        File "/home/f29a45b132fac802d76b5817dfaeb137.py", line 9, in 
          print (math.factorial(-5))
      ValueError: factorial() not defined for negative values
      
      0
      Traceback (most recent call last):
        File "/home/f29a45b132fac802d76b5817dfaeb137.py", line 9, in 
          print (math.factorial(-5))
      ValueError: factorial() not defined for negative values
      
      1

      The factorial of -5 is : 
      
      2
      The factorial of 23 is : 25852016738884976640000
      
      0
      The factorial of -5 is : 
      
      4
      The factorial of -5 is : 
      
      5
      math.factorial(x)
      Parameters :
      x : The number whose factorial has to be computed.
      Return value :
      Returns the factorial of desired number.
      Exceptions : 
      Raises Value error if number is negative or non-integral.
      
      1
      The factorial of -5 is : 
      
      7

      The factorial of -5 is : 
      

      The factorial of -5 is : 
      
      2
      The factorial of -5 is : 
      
      9

      Traceback (most recent call last):
        File "/home/f29a45b132fac802d76b5817dfaeb137.py", line 9, in 
          print (math.factorial(-5))
      ValueError: factorial() not defined for negative values
      
    • Đầu ra:

      Sử dụng math.factorial ()

      Phương pháp này được định nghĩa trong mô -đun Math Math của Python. Bởi vì nó có loại C thực hiện nội bộ, nó là nhanh.

      Traceback (most recent call last):
        File "/home/f29a45b132fac802d76b5817dfaeb137.py", line 9, in 
          print (math.factorial(-5))
      ValueError: factorial() not defined for negative values
      
      0
      Traceback (most recent call last):
        File "/home/f29a45b132fac802d76b5817dfaeb137.py", line 9, in 
          print (math.factorial(-5))
      ValueError: factorial() not defined for negative values
      
      1

      The factorial of -5 is : 
      
      2
      The factorial of 23 is : 25852016738884976640000
      
      0
      The factorial of -5 is : 
      
      4
      The factorial of -5 is : 
      
      5
      math.factorial(x)
      Parameters :
      x : The number whose factorial has to be computed.
      Return value :
      Returns the factorial of desired number.
      Exceptions : 
      Raises Value error if number is negative or non-integral.
      
      1
      The factorial of -5 is : 
      
      7

      The factorial of 5.6 is : 
      

      The factorial of -5 is : 
      
      2
      The factorial of -5 is : 
      
      9

      Traceback (most recent call last):
        File "/home/3987966b8ca9cbde2904ad47dfdec124.py", line 9, in 
          print (math.factorial(5.6))
      ValueError: factorial() only accepts integral values
      

    Đầu ra:Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Sử dụng math.factorial ()


    Trong bài viết này, bạn sẽ học cách tìm thấy giai thừa của một số và hiển thị nó.

    Để hiểu ví dụ này, bạn nên có kiến ​​thức về các chủ đề lập trình Python sau:

    • Python nếu ... tuyên bố khác
    • Python cho vòng lặp
    • Đệ quy Python

    Nấp của một số là sản phẩm của tất cả các số nguyên từ 1 đến số đó.

    Ví dụ, giai thừa của 6 là

    # Python program to find the factorial of a number provided by the user.
    
    # change the value for a different result
    num = 7
    
    # To take input from the user
    #num = int(input("Enter a number: "))
    
    factorial = 1
    
    # check if the number is negative, positive or zero
    if num < 0:
       print("Sorry, factorial does not exist for negative numbers")
    elif num == 0:
       print("The factorial of 0 is 1")
    else:
       for i in range(1,num + 1):
           factorial = factorial*i
       print("The factorial of",num,"is",factorial)
    
    7. Sự kiện không được định nghĩa cho các số âm và giai thừa của số 0 là một,
    # Python program to find the factorial of a number provided by the user.
    
    # change the value for a different result
    num = 7
    
    # To take input from the user
    #num = int(input("Enter a number: "))
    
    factorial = 1
    
    # check if the number is negative, positive or zero
    if num < 0:
       print("Sorry, factorial does not exist for negative numbers")
    elif num == 0:
       print("The factorial of 0 is 1")
    else:
       for i in range(1,num + 1):
           factorial = factorial*i
       print("The factorial of",num,"is",factorial)
    
    8.

    Đơn vị của một số sử dụng vòng lặp

    # Python program to find the factorial of a number provided by the user.
    
    # change the value for a different result
    num = 7
    
    # To take input from the user
    #num = int(input("Enter a number: "))
    
    factorial = 1
    
    # check if the number is negative, positive or zero
    if num < 0:
       print("Sorry, factorial does not exist for negative numbers")
    elif num == 0:
       print("The factorial of 0 is 1")
    else:
       for i in range(1,num + 1):
           factorial = factorial*i
       print("The factorial of",num,"is",factorial)
    

    Đầu ra

    The factorial of 7 is 5040
    

    Lưu ý: Để kiểm tra chương trình cho một số khác, thay đổi giá trị của

    # Python program to find the factorial of a number provided by the user.
    
    # change the value for a different result
    num = 7
    
    # To take input from the user
    #num = int(input("Enter a number: "))
    
    factorial = 1
    
    # check if the number is negative, positive or zero
    if num < 0:
       print("Sorry, factorial does not exist for negative numbers")
    elif num == 0:
       print("The factorial of 0 is 1")
    else:
       for i in range(1,num + 1):
           factorial = factorial*i
       print("The factorial of",num,"is",factorial)
    
    9. To test the program for a different number, change the value of
    # Python program to find the factorial of a number provided by the user.
    
    # change the value for a different result
    num = 7
    
    # To take input from the user
    #num = int(input("Enter a number: "))
    
    factorial = 1
    
    # check if the number is negative, positive or zero
    if num < 0:
       print("Sorry, factorial does not exist for negative numbers")
    elif num == 0:
       print("The factorial of 0 is 1")
    else:
       for i in range(1,num + 1):
           factorial = factorial*i
       print("The factorial of",num,"is",factorial)
    
    9.

    Ở đây, số lượng có được tìm thấy giai đoạn được lưu trữ trong

    # Python program to find the factorial of a number provided by the user.
    
    # change the value for a different result
    num = 7
    
    # To take input from the user
    #num = int(input("Enter a number: "))
    
    factorial = 1
    
    # check if the number is negative, positive or zero
    if num < 0:
       print("Sorry, factorial does not exist for negative numbers")
    elif num == 0:
       print("The factorial of 0 is 1")
    else:
       for i in range(1,num + 1):
           factorial = factorial*i
       print("The factorial of",num,"is",factorial)
    
    9 và chúng tôi kiểm tra xem số đó là âm, 0 hoặc dương bằng câu lệnh
    The factorial of 7 is 5040
    
    1. Nếu số là dương, chúng tôi sử dụng chức năng
    math.factorial(x)
    Parameters :
    x : The number whose factorial has to be computed.
    Return value :
    Returns the factorial of desired number.
    Exceptions : 
    Raises Value error if number is negative or non-integral.
    
    6 và
    The factorial of 7 is 5040
    
    3 để tính toán giai thừa.

    Lặp đi lặp lại Factorial*i (giá trị trả về)
    i = 11 * 1 = 1
    i = 21 * 2 = 2
    i = 32 * 3 = 6
    i = 46 * 4 = 24
    i = 524 * 5 = 120
    i = 6120 * 6 = 720
    i = 7720 * 7 = 5040

    Đơn vị của một số sử dụng đệ quy

    # Python program to find the factorial of a number provided by the user
    # using recursion
    
    def factorial(x):
        """This is a recursive function
        to find the factorial of an integer"""
    
        if x == 1:
            return 1
        else:
            # recursive call to the function
            return (x * factorial(x-1))
    
    
    # change the value for a different result
    num = 7
    
    # to take input from the user
    # num = int(input("Enter a number: "))
    
    # call the factorial function
    result = factorial(num)
    print("The factorial of", num, "is", result)

    Trong ví dụ trên,

    The factorial of 7 is 5040
    
    4 là một hàm đệ quy tự gọi. Ở đây, chức năng sẽ tự gọi mình bằng cách giảm giá trị của x.

    Để tìm hiểu về hoạt động của đệ quy, hãy truy cập đệ quy Python.