Hướng dẫn calculate factorial in python - tính giai thừa trong python

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à

The factorial of 7 is 5040
0. Nấp không được định nghĩa cho các số âm và giai thừa của số 0 là một,
The factorial of 7 is 5040
1.

Đơ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

The factorial of 7 is 5040
2. To test the program for a different number, change the value of 2. To test the program for a different number, change the value of

The factorial of 7 is 5040
2.

Ở đây, số lượng mà giai thừa sẽ được tìm thấy được lưu trữ trong

The factorial of 7 is 5040
2 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
4. Nếu số là dương, chúng tôi sử dụng chức năng
The factorial of 7 is 5040
5 và
The factorial of 7 is 5040
6 để tính toán giai thừa.
Lặp đi lặp lại Factorial*i (giá trị trả về)
i = 1 1 * 1 = 1
i = 2 1 * 2 = 2
i = 3 2 * 3 = 6
i = 4 6 * 4 = 24
i = 5 24 * 5 = 120
i = 6 120 * 6 = 720
i = 7 720 * 7 = 5040

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

The factorial of 7 is 5040
0

Trong ví dụ trên,

The factorial of 7 is 5040
7 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.

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

    The factorial of 7 is 5040
    
    8
    The factorial of 7 is 5040
    
    9
    The factorial of 7 is 5040
    
    00
    The factorial of 7 is 5040
    
    01
    The factorial of 7 is 5040
    
    9
    The factorial of 7 is 5040
    
    03

    Is

    The factorial of 7 is 5040
    
    84
    The factorial of 7 is 5040
    
    01
    The factorial of 7 is 5040
    
    9
    The factorial of 7 is 5040
    
    01
    The factorial of 7 is 5040
    
    88
    The factorial of 7 is 5040
    
    89
    # 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)
    
    40
    The factorial of 7 is 5040
    
    08
    # 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)
    
    42
    # 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)
    
    43
    The factorial of 7 is 5040
    
    9
    # 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)
    
    45
    The factorial of 7 is 5040
    
    8

    The factorial of 7 is 5040
    
    10
    # 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)
    
    470
    # 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)
    
    47

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

    # 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)
    
    4

    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.

    # 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)
    
    48
    # 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)
    
    49
    # 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)
    
    40
    The factorial of 7 is 5040
    
    08
    # 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)
    
    42
    # 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)
    
    43
    The factorial of 7 is 5040
    
    9
    # 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)
    
    45
    The factorial of 7 is 5040
    
    8

    The factorial of 7 is 5040
    
    10
    # 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)
    
    470
    # 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)
    
    47

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

      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.

      # 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)
      
      48
      # 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)
      
      49
      # 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)
      
      40
      The factorial of 7 is 5040
      
      08
      # 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)
      
      42
      # 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)
      
      43
      The factorial of 7 is 5040
      
      9
      # 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)
      
      45
      The factorial of 7 is 5040
      
      8
    • Đầu ra:math” module of python. Because it has C type internal implementation, it is fast.

      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.

      # 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)
      
      48
      # 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)
      
      49
      # 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)
      
      40
      The factorial of 7 is 5040
      
      08
      # 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)
      
      42
      # 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)
      
      43
      The factorial of 7 is 5040
      
      9
      # 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)
      
      45
      The factorial of 7 is 5040
      
      8

    Đầ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.

    # 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)
    
    48
    # 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)
    
    49
    # 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)
    
    40
    The factorial of 7 is 5040
    
    08
    # 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)
    
    42
    # 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)
    
    43
    The factorial of 7 is 5040
    
    9
    # 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)
    
    45
    The factorial of 7 is 5040
    
    3
    # 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)
    
    40
    # 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)
    
    47
    The factorial of 7 is 5040
    
    6