Thuật toán tìm giai thừa của một số trong python

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ố

  1. Nhận đầu vào số nguyên dương [n] từ người dùng
  2. 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]
  3. Sử dụng công thức dưới đây, tính giai thừa của một sốf = f*i
  4. 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 120
0

Factorial of 5 is 120
0
Factorial of 5 is 120
2

Factorial of 5 is 120
0
Factorial of 5 is 120
4
Factorial of 5 is 120
5
Factorial of 5 is 120
6
Factorial of 5 is 120
7
Factorial of 5 is 120
8
Factorial of 5 is 120
8
Factorial of 5 is 120
5
Factorial of 5 is 120
1
Factorial of 5 is 120
2
Factorial of 5 is 120
8
Factorial of 5 is 120
8
Factorial of 5 is 120
5
Factorial of 5 is 120
6
Factorial of 5 is 120
7
Factorial of 5 is 120
2
Factorial of 5 is 120
9
Factorial of 5 is 120
0
Factorial of 5 is 120
1
Factorial of 5 is 120
5
Factorial of 5 is 120
3

 

Factorial of 5 is 120
4

Factorial of 5 is 120
5
Factorial of 5 is 120
8
Factorial of 5 is 120
7_______68

Factorial of 5 is 120
9
Factorial of 5 is 120
0
Factorial of 5 is 120
1
Factorial of 5 is 120
2
Factorial of 5 is 120
3
Factorial of 5 is 120
4

Factorial of 5 is 120
5

 

Factorial of 5 is 120
6

đầ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 120
7

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 120
0
Factorial of 5 is 120
6
# 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 120
5
# 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 120
4
Factorial of 5 is 120
5

Factorial of 5 is 120
0
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 120
2_______68
Factorial of 5 is 120
8
Factorial of 5 is 120
5
Factorial of 5 is 120
1
Factorial of 5 is 120
2
Factorial of 5 is 120
8
Factorial of 5 is 120
8
Factorial of 5 is 120
5
# 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 120
4
Factorial of 5 is 120
5

Factorial of 5 is 120
0
Factorial of 5 is 120
7
# 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 120
8
Factorial of 5 is 120
5

# 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 120
5
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 120
9
Factorial of 5 is 120
8
Factorial of 5 is 120
2

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 120
2
Factorial of 5 is 120
1
Factorial of 5 is 120
8
Factorial of 5 is 120
5

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

Factorial of 5 is 120
5
Factorial of 5 is 120
8
Factorial of 5 is 120
7_______68

Factorial of 5 is 120
9
Factorial of 5 is 120
0
Factorial of 5 is 120
1
Factorial of 5 is 120
2
Factorial of 5 is 120
3
Factorial of 5 is 120
4

Factorial of 5 is 120
5

 

Factorial of 5 is 120
11

đầ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 120
14

Factorial of 5 is 120
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]
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 120
18

Factorial of 5 is 120
0
Factorial of 5 is 120
20
Factorial of 5 is 120
8
Factorial of 5 is 120
5

Factorial of 5 is 120
23

Factorial of 5 is 120
0
Factorial of 5 is 120
25
Factorial of 5 is 120
26
Factorial of 5 is 120
27
Factorial of 5 is 120
28
Factorial of 5 is 120
0
Factorial of 5 is 120
30
Factorial of 5 is 120
31
Factorial of 5 is 120
32
Factorial of 5 is 120
5
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 120
20____69
Factorial of 5 is 120
8
Factorial of 5 is 120
26

Factorial of 5 is 120
0
Factorial of 5 is 120
4
Factorial of 5 is 120
20

Factorial of 5 is 120
14
Factorial of 5 is 120
4

Factorial of 5 is 120
5
Factorial of 5 is 120
8
Factorial of 5 is 120
7_______68

Factorial of 5 is 120
9
Factorial of 5 is 120
0
Factorial of 5 is 120
1
Factorial of 5 is 120
52
Factorial of 5 is 120
3
Factorial of 5 is 120
4

Factorial of 5 is 120
5

Đầ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 120
0
Factorial of 5 is 120
2

Factorial of 5 is 120
0
Factorial of 5 is 120
4
Factorial of 5 is 120
5
Factorial of 5 is 120
6
Factorial of 5 is 120
7
Factorial of 5 is 120
8
Factorial of 5 is 120
8
Factorial of 5 is 120
5
Factorial of 5 is 120
1
Factorial of 5 is 120
2
Factorial of 5 is 120
8
Factorial of 5 is 120
8
Factorial of 5 is 120
5
Factorial of 5 is 120
6
Factorial of 5 is 120
7
Factorial of 5 is 120
2
Factorial of 5 is 120
9
Factorial of 5 is 120
0
Factorial of 5 is 120
1
Factorial of 5 is 120
5
Factorial of 5 is 120
6

 

 

Factorial of 5 is 120
4

Factorial of 5 is 120
5
Factorial of 5 is 120
8
Factorial of 5 is 120
7

Factorial of 5 is 120
9
Factorial of 5 is 120
0_______61
Factorial of 5 is 120
2
Factorial of 5 is 120
3
Factorial of 5 is 120
4

Factorial of 5 is 120
18
Factorial of 5 is 120
5

 

Factorial of 5 is 120
95

Factorial of 5 is 120
96

đầ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 120
99
Factorial of 5 is 120
00

 

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 120
0
Factorial of 5 is 120
4
Factorial of 5 is 120
05

 

 

Factorial of 5 is 120
4

Factorial of 5 is 120
5
Factorial of 5 is 120
8
Factorial of 5 is 120
7

Factorial of 5 is 120
9
Factorial of 5 is 120
0
Factorial of 5 is 120
1
Factorial of 5 is 120
52
Factorial of 5 is 120
3
Factorial of 5 is 120
4

Factorial of 5 is 120
18
Factorial of 5 is 120
5

 

Factorial of 5 is 120
18

đầu ra

Factorial of 5 is 120

Phương pháp. sử dụng numpy. sản xuất

Python3




Factorial of 5 is 120
99
Factorial of 5 is 120
20

Factorial of 5 is 120
2
Factorial of 5 is 120
8
Factorial of 5 is 120
7

Factorial of 5 is 120
24
Factorial of 5 is 120
8
Factorial of 5 is 120
26
Factorial of 5 is 120
25
Factorial of 5 is 120
26
Factorial of 5 is 120
27
Factorial of 5 is 120
28
Factorial of 5 is 120
0
Factorial of 5 is 120
5
Factorial of 5 is 120
33
Factorial of 5 is 120
32
Factorial of 5 is 120
5
Factorial of 5 is 120
36

Thuật toán tìm giai thừa của một số là gì?

Do đó, n. được ký hiệu là Giai thừa của n. Giai thừa được ký hiệu là ". ". Vì vậy, giả sử bạn muốn tìm giai thừa của số n, thì n. = n * [n-1] * [n-2] * [n-3]

Làm cách nào để tìm giai thừa của một số trong Python bằng vòng lặp for?

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ố đó. Ví dụ, giai thừa của 6 là 1*2*3*4*5*6 = 720. . Giai thừa của một số bằng vòng lặp

Có phương pháp giai thừa nào trong Python không?

factorial[] trong Python . python offers a direct function that can compute the factorial of a number without writing the whole code for computing factorial.

Chương trình giai thừa trong Python là gì?

# Chương trình Python 3 để tìm. # giai thừa của số đã cho. giai thừa xác định [n]. # một dòng để tìm giai thừa. trả về 1 nếu [n = = 1 hoặc n = = 0 ] khác n * giai thừa[n - 1 ];

Chủ Đề