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à 1*2*3*4*5*6 = 720
. Nấp không được xác định cho các số âm và giai thừa của số 0 là một, ____10.
Đơ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
1. To test the program for a different number, change the value of
The factorial of 7 is 50401.
Ở đây, số lượng mà giai thừa sẽ được tìm thấy được lưu trữ trong
The factorial of 7 is 50401 và chúng tôi kiểm tra xem số đó là âm, bằng không hoặc dương bằng câu lệnh
The factorial of 7 is 50403. Nếu số là dương, chúng tôi sử dụng chức năng
The factorial of 7 is 50404 và
The factorial of 7 is 50405 để 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
# 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 50406 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
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
python3
Đơn vị của một số nguyên không âm, là phép nhân của tất cả các số nguyên nhỏ hơn hoặc bằng n. Ví dụ: giai thừa 6 là 6*5*4*3*2*1 là 720.
1. Phương pháp tiếp cận: & NBSP;
The factorial of 7 is 50407
The factorial of 7 is 50408
The factorial of 7 is 50409
# 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]
0 # 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]
1 # 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]
2 # 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]
3# 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]
4# 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]
4__Factorial of 5 is 1200
Output:
Factorial of 5 is 120
Factorial of 5 is 120
0
4 # 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]
Factorial of 5 is 120
2Factorial of 5 is 120
3O[n]
Auxiliary Space: O[n]
Factorial of 5 is 120
4Factorial of 5 is 120
5Factorial of 5 is 120
6Factorial of 5 is 120
7Factorial of 5 is 120
8Factorial of 5 is 120
9
Method1:
python3
Đơn vị của một số nguyên không âm, là phép nhân của tất cả các số nguyên nhỏ hơn hoặc bằng n. Ví dụ: giai thừa 6 là 6*5*4*3*2*1 là 720.
1. Phương pháp tiếp cận: & NBSP;
The factorial of 7 is 50407
The factorial of 7 is 50408
The factorial of 7 is 50409
# 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]
0 # 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]
1 # 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]
2 # 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]
3# 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]
4# 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]
4__
Factorial of 5 is 1200
# 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]
4 Factorial of 5 is 1202
Factorial of 5 is 1203
The factorial of 7 is 50409
Factorial of 5 is 1203
Factorial of 5 is 1207
Factorial of 5 is 1204
Factorial of 5 is 1205
Factorial of 5 is 1206
Factorial of 5 is 1207
Factorial of 5 is 1208
Factorial of 5 is 1209
Factorial of 5 is 1208
1204
1205
# 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]
11207
Độ phức tạp về thời gian: O [N] Không gian phụ trợ: O [N]
2. Cách tiếp cận lặp:
The factorial of 7 is 50409
# 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]
2 Factorial of 5 is 1205
Factorial of 5 is 1201
Factorial of 5 is 1207
The factorial of 7 is 50407
The factorial of 7 is 50408
Factorial of 5 is 1204
Factorial of 5 is 1205
Factorial of 5 is 1206
Factorial of 5 is 1207
Factorial of 5 is 1208
Factorial of 5 is 1209
Factorial of 5 is 1200
Output:
Factorial of 5 is 120
The factorial of 7 is 5040
9
0 # 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]
1 # 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]
2 # 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]
3# 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]
4# 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]
4__ O[n]# 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]
Auxiliary
Space: O[1]
Method2:
Python3
Đơn vị của một số nguyên không âm, là phép nhân của tất cả các số nguyên nhỏ hơn hoặc bằng n. Ví dụ: giai thừa 6 là 6*5*4*3*2*1 là 720.
1. Phương pháp tiếp cận: & NBSP;
The factorial of 7 is 50407
The factorial of 7 is 50408
The factorial of 7 is 50409
# 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]
0 # 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]
1 # 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]
2 # 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]
3# 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]
4# 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]
4__Factorial of 5 is 1200
# 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]
4 Factorial of 5 is 1202
Factorial of 5 is 1203
The factorial of 7 is 50407
The factorial of 7 is 50408
Factorial of 5 is 1204
Factorial of 5 is 1205
Factorial of 5 is 1206
The factorial of 7 is 504044
Factorial of 5 is 1208
Factorial of 5 is 1209
Factorial of 5 is 1200
The factorial of 7 is 5040
9
0 # 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]
1 # 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]
2 # 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]
3# 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]
4# 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]
4__
# 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]
Factorial of 5 is 120
The factorial of 7 is 5040
9
0 # 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]
1 # 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]
2 # 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]
3# 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]
4# 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]
4__O[n]# 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]
Auxiliary Space: O[1]
Factorial of 5 is 120
0# 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]
4 Factorial of 5 is 120
2Factorial of 5 is 120
3
# 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]
Python3
Đơn vị của một số nguyên không âm, là phép nhân của tất cả các số nguyên nhỏ hơn hoặc bằng n. Ví dụ: giai thừa 6 là 6*5*4*3*2*1 là 720.
1. Phương pháp tiếp cận: & NBSP;
The factorial of 7 is 50407
The factorial of 7 is 50408
The factorial of 7 is 50409
# 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]
0 # 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]
1 # 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]
2 # 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]
3# 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]
4# 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]
4__The factorial of 7 is 504080
Factorial of 5 is 1200
Output:
Factorial of 5 is 120
Factorial of 5 is 120
0
4 # 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]
Factorial of 5 is 120
2Factorial of 5 is 120
3O[n]
Auxiliary Space: O[n]
Factorial of 5 is 1204
Factorial of 5 is 1205
Factorial of 5 is 1206
Factorial of 5 is 1207
Factorial of 5 is 1208
Factorial of 5 is 1209
Độ phức tạp về thời gian: O [N] Không gian phụ trợ: O [N]
2. Cách tiếp cận lặp:math.factorial[] function returns the factorial of desired number.
The factorial of 7 is 5040
9
2 # 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]
Factorial of 5 is 120
5Factorial of 5 is 120
1Factorial of 5 is 120
7 math.factorial[x]
Factorial of 5 is 120
8
0 # 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]
Factorial of 5 is 120
1
x: This is a numeric expression.
The factorial of 7 is 5040
9Factorial of 5 is 120
2 Factorial of 5 is 120
4
4# 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]
4 # 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]
Factorial of 5 is 120
1
7 factorial of desired number.# 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]
Python3
Factorial of 5 is 1208
# 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]
0 # 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]
1Đơn vị của một số nguyên không âm, là phép nhân của tất cả các số nguyên nhỏ hơn hoặc bằng n. Ví dụ: giai thừa 6 là 6*5*4*3*2*1 là 720.
1. Phương pháp tiếp cận: & NBSP;
The factorial of 7 is 50407
The factorial of 7 is 50408
Factorial of 5 is 1204
Factorial of 5 is 1205
Factorial of 5 is 1206
The factorial of 7 is 504044
Factorial of 5 is 1208
Factorial of 5 is 1209
The factorial of 7 is 504080
Factorial of 5 is 1200
Output:
Factorial of 5 is 120
The factorial of 7 is 5040
9# 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]
0 # 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]
1 # 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]
2 # 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]
3# 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]
4# 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]
4__
# 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]
# 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]
# 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]
# 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]
# 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]
# 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]
Python3
Factorial of 5 is 1200
# 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]
4 Factorial of 5 is 1202
Factorial of 5 is 1203
# 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]
8# 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]
4Factorial of 5 is 1202
Factorial of 5 is 1204
Factorial of 5 is 1205
Factorial of 5 is 1206
Factorial of 5 is 1207
Factorial of 5 is 1208
Factorial of 5 is 1209
Factorial of 5 is 1204
# 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]
19Output
120
The factorial of 7 is 5040
9
0 # 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]
1 # 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]
2 # 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]
3# 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]
4# 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]
4__O[n]# 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]
Auxiliary Space: O[1]