Hướng dẫn how do you calculate factorial in python? - làm thế nào để bạn 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:
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 50400. 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 50401. Đơn vị của một số sử dụng vòng lặp
Đầ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 50402. Ở đây, số lượng mà giai thừa sẽ được tìm thấy được lưu trữ trong The factorial of 7 is 50402 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 50404. Nếu số là dương, chúng tôi sử dụng chức năng The factorial of 7 is 50405 và The factorial of 7 is 50406 để tính toán giai thừa.
Đơn vị của một số sử dụng đệ quy
Trong ví dụ trên, The factorial of 7 is 50407 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 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 50408 The factorial of 7 is 50409 0 1The factorial of 7 is 50409 3Is The factorial of 23 is : 258520167388849766400004 1The factorial of 7 is 50409 1The factorial of 23 is : 258520167388849766400008 The factorial of 23 is : 258520167388849766400009 math.factorial(x) Parameters : x : The number whose factorial has to be computed. Return value : Returns the factorial of desired number. Exceptions : Raises Value error if number is negative or non-integral.0 8math.factorial(x) Parameters : x : The number whose factorial has to be computed. Return value : Returns the factorial of desired number. Exceptions : Raises Value error if number is negative or non-integral.2 math.factorial(x) Parameters : x : The number whose factorial has to be computed. Return value : Returns the factorial of desired number. Exceptions : Raises Value error if number is negative or non-integral.3 The factorial of 7 is 50409 math.factorial(x) Parameters : x : The number whose factorial has to be computed. Return value : Returns the factorial of desired number. Exceptions : Raises Value error if number is negative or non-integral.5 The factorial of 23 is : 25852016738884976640000
Đầu ra:math” module of python. Because it has C type internal implementation, it is fast. math.factorial(x) Parameters : x : The number whose factorial has to be computed. Return value : Returns the factorial of desired number. Exceptions : Raises Value error if number is negative or non-integral. Sử dụng math.factorial () Phương pháp này được định nghĩa trong mô -đun Math Math của Python. Bởi vì nó có loại C thực hiện nội bộ, nó là nhanh. math.factorial(x) Parameters : x : The number whose factorial has to be computed. Return value : Returns the factorial of desired number. Exceptions : Raises Value error if number is negative or non-integral.8 math.factorial(x) Parameters : x : The number whose factorial has to be computed. Return value : Returns the factorial of desired number. Exceptions : Raises Value error if number is negative or non-integral.9 math.factorial(x) Parameters : x : The number whose factorial has to be computed. Return value : Returns the factorial of desired number. Exceptions : Raises Value error if number is negative or non-integral.0 8math.factorial(x) Parameters : x : The number whose factorial has to be computed. Return value : Returns the factorial of desired number. Exceptions : Raises Value error if number is negative or non-integral.2 math.factorial(x) Parameters : x : The number whose factorial has to be computed. Return value : Returns the factorial of desired number. Exceptions : Raises Value error if number is negative or non-integral.3 The factorial of 7 is 50409 math.factorial(x) Parameters : x : The number whose factorial has to be computed. Return value : Returns the factorial of desired number. Exceptions : Raises Value error if number is negative or non-integral.5 The factorial of 23 is : 25852016738884976640000
Đầ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. Vui lòng viết nhận xét nếu bạn tìm thấy bất cứ điều gì không chính xác, hoặc bạn muốn chia sẻ thêm thông tin về chủ đề được thảo luận ở trên. |