Hướng dẫn what is factorial formula in python? - công thức giai thừa trong python là gì?

Chương trình Python để tìm giai thừa của một số

Factorial là gì?

Factorial là một số nguyên không âm. Đây là sản phẩm của tất cả các số nguyên dương nhỏ hơn hoặc bằng con số mà bạn yêu cầu. Nó được biểu thị bằng một dấu chấm than (!).

Example:

Giá trị giai thừa của 4 là 24.

Lưu ý: Giá trị giai thừa là 0 là 1 luôn luôn. (Vi phạm quy tắc)

Thí dụ -

Output:

Enter a number: 10
The factorial of 10 is 3628800

Giải trình -

Trong ví dụ trên, chúng tôi đã khai báo một biến số lấy số nguyên làm đầu vào từ người dùng. Chúng tôi đã khai báo một giai thừa biến và được gán 1. Sau đó, chúng tôi đã kiểm tra xem người dùng có nhập số ít hơn một không, thì nó trả về giai thừa không tồn tại cho một số âm. Nếu nó trả về sai, thì chúng tôi kiểm tra Num bằng 0, nó sẽ trả lại sai, việc chuyển điều khiển cho câu lệnh khác và in giai đoạn của một số nhất định.num variable that takes an integer as an input from the user. We declared a variable factorial and assigned 1. Then, we checked if the user enters the number less than one, then it returns the factorial does not exist for a negative number. If it returns false, then we check num is equal to zero, it returns false the control transfers to the else statement and prints the factorial of a given number.

Sử dụng đệ quy

Recursion Python là một phương pháp tự gọi. Hãy hiểu ví dụ sau.

Thí dụ -

Output:

Giải trình -

Trong ví dụ trên, chúng tôi đã khai báo một biến số lấy số nguyên làm đầu vào từ người dùng. Chúng tôi đã khai báo một giai thừa biến và được gán 1. Sau đó, chúng tôi đã kiểm tra xem người dùng có nhập số ít hơn một không, thì nó trả về giai thừa không tồn tại cho một số âm. Nếu nó trả về sai, thì chúng tôi kiểm tra Num bằng 0, nó sẽ trả lại sai, việc chuyển điều khiển cho câu lệnh khác và in giai đoạn của một số nhất định.fact(num) function, which returns one if the entered value is 1 or 0 otherwise until we get the factorial of a given number.

Sử dụng đệ quy

Recursion Python là một phương pháp tự gọi. Hãy hiểu ví dụ sau.factorial() method. Let's understand the following example.

Thí dụ -

Output:

Enter the number: 6
Factorial of 6 is 720

Giải trình -factorial() function. It takes an integer number to calculate the factorial. We don't need to use logic.


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

    n ____10

    Enter the number: 6
    Factorial of 6 is 720
    
    1

    Enter the number: 6
    Factorial of 6 is 720
    
    2
    Enter the number: 6
    Factorial of 6 is 720
    
    0
    Enter the number: 6
    Factorial of 6 is 720
    
    4

    Enter the number: 6
    Factorial of 6 is 720
    
    5
    Enter the number: 6
    Factorial of 6 is 720
    
    6
    Enter the number: 6
    Factorial of 6 is 720
    
    7
    Enter the number: 6
    Factorial of 6 is 720
    
    8
    Enter the number: 6
    Factorial of 6 is 720
    
    9
    Enter the number: 6
    Factorial of 6 is 720
    
    4
    The factorial of 23 is : 25852016738884976640000
    
    1
    The factorial of 23 is : 25852016738884976640000
    
    2222214
    The factorial of 23 is : 25852016738884976640000
    
    4

    The factorial of 23 is : 25852016738884976640000
    
    5
    Enter the number: 6
    Factorial of 6 is 720
    
    2
    Enter the number: 6
    Factorial of 6 is 720
    
    0
    Enter the number: 6
    Factorial of 6 is 720
    
    2
    The factorial of 23 is : 25852016738884976640000
    
    9
    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

    Đầu ra:

    The factorial of 23 is : 25852016738884976640000
    

    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
    Enter the number: 6
    Factorial of 6 is 720
    
    9
    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.
    
    4
    Enter the number: 6
    Factorial of 6 is 720
    
    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.
    
    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.
    
    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.
    
    8math” 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.

    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

    Đầu ra:

    The factorial of 23 is : 25852016738884976640000
    

    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
    Enter the number: 6
    Factorial of 6 is 720
    
    9
    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 23 is : 25852016738884976640000
    
    44__10

    • 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 23 is : 25852016738884976640000
      
      8
      Enter the number: 6
      Factorial of 6 is 720
      
      1
      The factorial of -5 is : 
      
      0

      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.

      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

      Đầu ra:

      The factorial of -5 is : 
      

      Lỗi runtime :

      Traceback (most recent call last):
        File "/home/f29a45b132fac802d76b5817dfaeb137.py", line 9, in 
          print (math.factorial(-5))
      ValueError: factorial() not defined for negative values
      
    • Bài viết này được đóng góp bởi Manjeet Singh. Nếu bạn thích GeekSforGeeks và muốn đóng góp, bạn cũng có thể viết một bài viết bằng cách sử dụng PROPTENT.GeekSforGeeks.org hoặc gửi bài viết của bạn đến. Xem bài viết của bạn xuất hiện trên trang chính của GeekSforGeek và giúp các chuyên viên máy tính khác.

      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.

      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

      Đầu ra:

      The factorial of 5.6 is : 
      

      Lỗi runtime :

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

    Bài viết này được đóng góp bởi Manjeet Singh. Nếu bạn thích GeekSforGeeks và muốn đóng góp, bạn cũng có thể viết một bài viết bằng cách sử dụng PROPTENT.GeekSforGeeks.org hoặc gửi bài viết của bạn đến. Xem bài viết của bạn xuất hiện trên trang chính của GeekSforGeek và giúp các chuyên viên máy tính khác.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.

    Vui lòng viết nhận xét nếu bạn tìm thấy bất cứ điều gì không chính xác, hoặc bạn muốn chia sẻ thêm thông tin về chủ đề được thảo luận ở trên.