Trong bài viết này, chúng ta sẽ nghiên cứu bài toán nổi tiếng python chương trình tìm giai thừa của một số. Giai thừa của một số là tích của tất cả các số nguyên từ 1 đến số đó. Nhiều công ty như Wipro, Accenture, TCS và nhiều công ty khác đã hỏi những loại câu hỏi này trong các cuộc phỏng vấn kỹ thuật của họ để kiểm tra kiến thức cơ bản của ứng viên. Hãy thảo luận về các phương pháp khác nhau để tìm chương trình python để tìm giai thừa của một số
Ví dụ.
Nhập số. 5
Đầu ra.
Giai thừa của 5 là 120
Giải thích.
5. Sẽ là 5 x 4 x 3 x 2 x 1, tức là 120
Thuật toán tìm kiếm chương trình python tìm giai thừa của một số
- Nhận đầu vào số nguyên dương [n] từ người dùng
- Lặp lại từ 1 đến n bằng vòng lặp for [vòng lặp for được sử dụng để tăng số lên đến đầu vào đã cho]
- Sử dụng công thức dưới đây, tính giai thừa của một sốf = f*i
- In đầu ra tôi. e giai thừa được tính toán
Phương pháp 1. chương trình python để tìm giai thừa của một số bằng hàm giai thừa []
Triển khai mã
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]
Output: The factorial of 5 is 120
Phương pháp 2. Chương trình Python tìm giai thừa của một số bằng vòng lặp for
Triển khai mã
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]
Output: The factorial of 5 is 120
Phương pháp 3. Chương trình Python để tìm giai thừa của một số bằng cách sử dụng đệ quy
Triển khai mã
# Factorial of a number using recursion def recur_factorial[n]: if n == 1: return n else: return n*recur_factorial[n-1] num = 5 # check if the number is negative if num < 0: print["Sorry, factorial does not exist for negative numbers"] elif num == 0: print["The factorial of 0 is 1"] else: print["The factorial of", num, "is", recur_factorial[num]]
Output: The factorial of 5 is 120
Phần kết luận
Trong blog này, chúng tôi đã thảo luận về các cách tiếp cận khác nhau để tìm chương trình python để tìm giai thừa của một số. Chúng tôi hy vọng bài viết này sẽ giúp bạn xóa tan mọi nghi ngờ và xây dựng sự tự tin của bạn. PrepBytes cung cấp cho bạn nội dung tốt nhất, bạn cũng có thể kiểm tra nền tảng MYCODE của chúng tôi để kiểm tra vị trí thực sự của bạn vì những câu hỏi này được chuẩn bị bởi các chuyên gia cố vấn của chúng tôi
Giai thừa 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 của 6 là 6*5*4*3*2*1 là 720
1. phương pháp đệ quy.
trăn3
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]13
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]14
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]15
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]16
Factorial of 5 is 1200
Factorial of 5 is 1200
Factorial of 5 is 1202
Factorial of 5 is 1200
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 1208
Factorial of 5 is 1205
Factorial of 5 is 1201
Factorial of 5 is 1202
Factorial of 5 is 1208
Factorial of 5 is 1208
Factorial of 5 is 1205
Factorial of 5 is 1206
Factorial of 5 is 1207
Factorial of 5 is 1202
Factorial of 5 is 1209
Factorial of 5 is 1200
Factorial of 5 is 1201
Factorial of 5 is 1205
Factorial of 5 is 1203
Factorial of 5 is 1204
Factorial of 5 is 1205
Factorial of 5 is 1208
Factorial of 5 is 1207_______68
Factorial of 5 is 1209
Factorial of 5 is 1200
Factorial of 5 is 1201
Factorial of 5 is 1202
Factorial of 5 is 1203
Factorial of 5 is 1204
Factorial of 5 is 1205
Factorial of 5 is 1206
đầu ra
Factorial of 5 is 120
Độ phức tạp về thời gian. O[n]
Không gian phụ. O[n]
2. cách tiếp cận lặp đi lặp lại
Phương pháp1
trăn3
Factorial of 5 is 1207
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]14
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]15
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]16
Factorial of 5 is 1200
Factorial of 5 is 1206
# Factorial of a number using recursion def recur_factorial[n]: if n == 1: return n else: return n*recur_factorial[n-1] num = 5 # check if the number is negative if num < 0: print["Sorry, factorial does not exist for negative numbers"] elif num == 0: print["The factorial of 0 is 1"] else: print["The factorial of", num, "is", recur_factorial[num]]73
Factorial of 5 is 1205
# Factorial of a number using recursion def recur_factorial[n]: if n == 1: return n else: return n*recur_factorial[n-1] num = 5 # check if the number is negative if num < 0: print["Sorry, factorial does not exist for negative numbers"] elif num == 0: print["The factorial of 0 is 1"] else: print["The factorial of", num, "is", recur_factorial[num]]75
# Factorial of a number using recursion def recur_factorial[n]: if n == 1: return n else: return n*recur_factorial[n-1] num = 5 # check if the number is negative if num < 0: print["Sorry, factorial does not exist for negative numbers"] elif num == 0: print["The factorial of 0 is 1"] else: print["The factorial of", num, "is", recur_factorial[num]]76
Factorial of 5 is 1204
Factorial of 5 is 1205
Factorial of 5 is 1200
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]130
Factorial of 5 is 1202_______68
Factorial of 5 is 1208
Factorial of 5 is 1205
Factorial of 5 is 1201
Factorial of 5 is 1202
Factorial of 5 is 1208
Factorial of 5 is 1208
Factorial of 5 is 1205
# Factorial of a number using recursion def recur_factorial[n]: if n == 1: return n else: return n*recur_factorial[n-1] num = 5 # check if the number is negative if num < 0: print["Sorry, factorial does not exist for negative numbers"] elif num == 0: print["The factorial of 0 is 1"] else: print["The factorial of", num, "is", recur_factorial[num]]75
# Factorial of a number using recursion def recur_factorial[n]: if n == 1: return n else: return n*recur_factorial[n-1] num = 5 # check if the number is negative if num < 0: print["Sorry, factorial does not exist for negative numbers"] elif num == 0: print["The factorial of 0 is 1"] else: print["The factorial of", num, "is", recur_factorial[num]]76
Factorial of 5 is 1204
Factorial of 5 is 1205
Factorial of 5 is 1200
Factorial of 5 is 1207
# Factorial of a number using recursion def recur_factorial[n]: if n == 1: return n else: return n*recur_factorial[n-1] num = 5 # check if the number is negative if num < 0: print["Sorry, factorial does not exist for negative numbers"] elif num == 0: print["The factorial of 0 is 1"] else: print["The factorial of", num, "is", recur_factorial[num]]75
# Factorial of a number using recursion def recur_factorial[n]: if n == 1: return n else: return n*recur_factorial[n-1] num = 5 # check if the number is negative if num < 0: print["Sorry, factorial does not exist for negative numbers"] elif num == 0: print["The factorial of 0 is 1"] else: print["The factorial of", num, "is", recur_factorial[num]]76
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]148
Factorial of 5 is 1208
Factorial of 5 is 1205
# Factorial of a number using recursion def recur_factorial[n]: if n == 1: return n else: return n*recur_factorial[n-1] num = 5 # check if the number is negative if num < 0: print["Sorry, factorial does not exist for negative numbers"] elif num == 0: print["The factorial of 0 is 1"] else: print["The factorial of", num, "is", recur_factorial[num]]76
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]152
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]153
Factorial of 5 is 1205
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]155
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]156
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]148
Factorial of 5 is 1209
Factorial of 5 is 1208
Factorial of 5 is 1202
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]156
Factorial of 5 is 1202
Factorial of 5 is 1201
Factorial of 5 is 1208
Factorial of 5 is 1205
# Factorial of a number using recursion def recur_factorial[n]: if n == 1: return n else: return n*recur_factorial[n-1] num = 5 # check if the number is negative if num < 0: print["Sorry, factorial does not exist for negative numbers"] elif num == 0: print["The factorial of 0 is 1"] else: print["The factorial of", num, "is", recur_factorial[num]]76
Factorial of 5 is 1204
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]148
Factorial of 5 is 1204
Factorial of 5 is 1205
Factorial of 5 is 1208
Factorial of 5 is 1207_______68
Factorial of 5 is 1209
Factorial of 5 is 1200
Factorial of 5 is 1201
Factorial of 5 is 1202
Factorial of 5 is 1203
Factorial of 5 is 1204
Factorial of 5 is 1205
Factorial of 5 is 12011
đầu ra
Factorial of 5 is 120
Độ phức tạp về thời gian. O[n]
Không gian phụ. Ô[1]
Phương pháp2.
Python3
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]13
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]14
Factorial of 5 is 12014
Factorial of 5 is 12015
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]15
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]16
Factorial of 5 is 12018
Factorial of 5 is 1200
Factorial of 5 is 12020
Factorial of 5 is 1208
Factorial of 5 is 1205
Factorial of 5 is 12023
Factorial of 5 is 1200
Factorial of 5 is 12025
Factorial of 5 is 12026
Factorial of 5 is 12027
Factorial of 5 is 12028
Factorial of 5 is 1200
Factorial of 5 is 12030
Factorial of 5 is 12031
Factorial of 5 is 12032
Factorial of 5 is 1205
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]155
# Factorial of a number using recursion def recur_factorial[n]: if n == 1: return n else: return n*recur_factorial[n-1] num = 5 # check if the number is negative if num < 0: print["Sorry, factorial does not exist for negative numbers"] elif num == 0: print["The factorial of 0 is 1"] else: print["The factorial of", num, "is", recur_factorial[num]]76
Factorial of 5 is 12020____69
Factorial of 5 is 1208
Factorial of 5 is 12026
Factorial of 5 is 1200
Factorial of 5 is 1204
Factorial of 5 is 12020
Factorial of 5 is 12014
Factorial of 5 is 1204
Factorial of 5 is 1205
Factorial of 5 is 1208
Factorial of 5 is 1207_______68
Factorial of 5 is 1209
Factorial of 5 is 1200
Factorial of 5 is 1201
Factorial of 5 is 12052
Factorial of 5 is 1203
Factorial of 5 is 1204
Factorial of 5 is 1205
Đầu ra
Factorial of 5 is 120
Độ phức tạp về thời gian. O[n]
Không gian phụ. Ô[1]
3. Giải pháp một dòng [Sử dụng toán tử bậc ba].
Python3
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]13
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]14
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]15
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]16
Factorial of 5 is 1200
Factorial of 5 is 1202
Factorial of 5 is 1200
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 1208
Factorial of 5 is 1205
Factorial of 5 is 1201
Factorial of 5 is 1202
Factorial of 5 is 1208
Factorial of 5 is 1208
Factorial of 5 is 1205
Factorial of 5 is 1206
Factorial of 5 is 1207
Factorial of 5 is 1202
Factorial of 5 is 1209
Factorial of 5 is 1200
Factorial of 5 is 1201
Factorial of 5 is 1205
Factorial of 5 is 1206
Factorial of 5 is 1204
Factorial of 5 is 1205
Factorial of 5 is 1208
Factorial of 5 is 1207
Factorial of 5 is 1209
Factorial of 5 is 1200_______61
Factorial of 5 is 1202
Factorial of 5 is 1203
Factorial of 5 is 1204
Factorial of 5 is 12018
Factorial of 5 is 1205
Factorial of 5 is 12095
Factorial of 5 is 12096
đầu ra
Factorial of 5 is 120
Độ phức tạp về thời gian. O[n]
Không gian phụ. O[n]
Vui lòng tham khảo toàn bộ bài viết về Chương trình giai thừa của một số để biết thêm chi tiết
4. Bằng cách sử dụng chức năng dựng sẵn.
Trong Python, mô-đun toán học chứa một số phép toán, có thể thực hiện dễ dàng bằng mô-đun. môn Toán. hàm giai thừa [] trả về giai thừa của số mong muốn
cú pháp. môn Toán. giai thừa[x]
Tham số.
x. Đây là một biểu thức số.
trả lại. giai thừa của số mong muốn
Python3
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]13
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]14
Factorial of 5 is 12099
Factorial of 5 is 12000
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]15
n = int [input ["Enter a number: "]] factorial = 1 if n >= 1: for i in range [1, n+1]: factorial=factorial *i print["Factorial of the given number is: ", factorial]16
Factorial of 5 is 1200
Factorial of 5 is 1204
Factorial of 5 is 12005
Factorial of 5 is 1204
Factorial of 5 is 1205
Factorial of 5 is 1208
Factorial of 5 is 1207
Factorial of 5 is 1209
Factorial of 5 is 1200
Factorial of 5 is 1201
Factorial of 5 is 12052
Factorial of 5 is 1203
Factorial of 5 is 1204
Factorial of 5 is 12018
Factorial of 5 is 1205
Factorial of 5 is 12018
đầu ra
Factorial of 5 is 120
Phương pháp. sử dụng numpy. sản xuất
Python3
Factorial of 5 is 12099
Factorial of 5 is 12020
Factorial of 5 is 1202
Factorial of 5 is 1208
Factorial of 5 is 1207
Factorial of 5 is 12024
Factorial of 5 is 1208
Factorial of 5 is 12026
Factorial of 5 is 12025
Factorial of 5 is 12026
Factorial of 5 is 12027
Factorial of 5 is 12028
Factorial of 5 is 1200
Factorial of 5 is 1205
Factorial of 5 is 12033
Factorial of 5 is 12032
Factorial of 5 is 1205
Factorial of 5 is 12036