Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Factorial Function in Python

Last Updated on February 25, 2023 by Prepbytes

In mathematics, the factorial of a non-negative integer n, denoted by n! is the product of all positive integers less than or equal to n. For example, the factorial of 5 (written as 5!) is 5 x 4 x 3 x 2 x 1 = 120. In Python, the math module provides a built-in function called factorial() to calculate the factorial of a number. In this article we will discuss the factorial function in python, the Parameter Values of the factorial function in python and some examples of the factorial functions in python with different arguments.

Factorial Function in Python

The factorial() function uses a recursive approach to calculate the factorial of a number. It starts by checking if the input number is 0 or 1. If the input is 0 or 1, the function returns 1, since the factorial of 0 and 1 is defined to be 1. If the input is greater than 1, the function recursively calls itself with the argument reduced by 1, until it reaches the base case of 0. In Python, you can use the math module to calculate the factorial of a number using math.factorial() function. This function takes a single argument, which is the number whose factorial is to be calculated and returns the factorial of that number.

Time Complexity of Factorial Function in Python

The time complexity of the factorial() function is O(n), where n is the input integer. This is because the function must iterate over all integers from 1 to n in order to calculate the product. As the value of n increases, the time required to calculate the factorial also increases, making the factorial() function computationally expensive for large inputs.

Return Type of Factorial Function in Python

The return type of the factorial() function is always an integer. This is because the factorial of a non-negative integer is always a whole number. If the input to the factorial() function is negative, the function raises a ValueError because the factorial of a negative integer is not defined.

Parameter Values of Factorial Function in Python

The factorial function in Python can take one parameter, which is the non-negative integer whose factorial is to be calculated. If no parameter is provided, the function raises a TypeError indicating that the parameter is missing. Here are some details about the parameter values for the factorial function in python:

Valid Values:

  • Any non-negative integer (i.e., 0 or a positive integer).
  • The maximum input value for the factorial() function in Python depends on the size of the integer type used by your system. For example, on a 64-bit system, the largest integer that can be represented is 2^63 – 1, which is much larger than the largest factorial that is commonly used in practice.

Invalid Values:

  • Any value that is not an integer, such as a float or a string.
  • Any negative integer.
  • Any value that exceeds the maximum integer value for your system.
  • If the factorial() function is called with an invalid parameter value, it will raise an exception. For example, if a negative integer is passed as the parameter, a ValueError exception will be raised with the message "factorial() not defined for negative values". Similarly, if a value that exceeds the maximum integer value for your system is passed, an OverflowError exception will be raised with the message "integer division result too large for a float".

Example 1 of Factorial Function in Python

In this example we have taken positive integer value

import math
x = 6
print ("The factorial of 6 is : ", end ="")
print (math.factorial(x))

Output

The factorial of 6 is :  720

Explanation
In the above python program we have taken positive integer value as an argument and here we have taken 6, so the factorial of 6 comes on the output screen. This clearly shows factorial function accepts positive(non-negative) integer value as an input.

Example 2 of Factorial Function in Python

In this example we have taken float value

Code

import math 
x = 17.2
print ("The factorial of 17.2 is : ", end ="")
print (math.factorial(x))

Output

ValueError: factorial() only accepts integral values

Explanation
In the above python program we have taken float value as an argument and if we observe on output screen it gives value error which shows the factorial function in python not allows float value as an input.

Example 3 of Factorial Function in Python

In this example we have taken negative integer value

Code

import math
x = -17
print ("The factorial of -17 is : ", end ="")
print (math.factorial(x))

Output

ValueError: factorial() not defined for negative values

Explanation
In the above python program we have taken negative integer value as an argument and if we observe on output screen it gives value error which shows the factorial function in python not allows negative value as an input.

Summary
The factorial function in Python is a useful tool for calculating the factorial of a non-negative integer. It has a time complexity of O(n), returns an integer, and takes a single parameter which must be a non-negative integer. While the factorial() function is not efficient for large inputs, it is still a valuable tool for mathematical and computational applications. The factorial function in Python has a wide range of applications in mathematics, statistics, and computer science. It is a powerful tool for counting and arranging objects, calculating probabilities, and analyzing the time complexity of algorithms.

Frequently Asked Questions (FAQ):

Here are some frequently asked questions about the factorial function in Python:

Q1 What is the factorial function in Python?
Ans: The factorial() function in Python is a function in the math module that takes a non-negative integer as its input and returns the factorial of that integer as an integer.

Q2 What is the time complexity of the factorial() function in Python?
Ans: The time complexity of the factorial() function is O(n), where n is the input integer. This is because the function must iterate over all integers from 1 to n in order to calculate the product.

Q3 What is the return type of the factorial() function in Python?
Ans: The return type of the factorial() function is always an integer. This is because the factorial of a non-negative integer is always a whole number.

Q4 What happens if the input to the factorial() function is negative?
Ans: If the input to the factorial() function is negative, the function raises a ValueError indicating that the input is not a non-negative integer.

Q5 What are some common applications of the factorial() function in Python?
Ans: The factorial() function in Python is commonly used in combinatorics to calculate the number of permutations and combinations of a set of objects. It is also used in probability theory to calculate the number of ways that a set of events can occur.

Q6 Are there any alternatives to the factorial() function in Python?
Ans: Yes, there are alternative functions and libraries that can be used to calculate the factorial of an integer in Python. For example, math.gamma() function can be used to calculate the gamma function, which is a generalization of the factorial function. There are also libraries like numpy and scipy that provide more advanced mathematical functions.

Leave a Reply

Your email address will not be published. Required fields are marked *