Basics of Python - Factorial Calculator

by tanish satpal in Teachers > 6

923 Views, 9 Favorites, 0 Comments

Basics of Python - Factorial Calculator

Image 30-08-24 at 6.53 PM.jpeg

What are Factorials?


Factorials are a mathematical concept used to calculate the product of all positive integers less than or equal to a given number. The factorial of a number n is denoted by n! and is defined as:


n! = n * (n-1) * (n-2) * … * 2 * 1


For example:


• 5! = 5 * 4 * 3 * 2 * 1 = 120

• 4! = 4 * 3 * 2 * 1 = 24


Factorials are widely used in combinatorics, probability, and algebra. They help in calculating permutations, combinations, and are also involved in the expansions of functions.


There is a special case in factorials:


• 0! = 1 by definition. This is a standard convention in mathematics and is essential for various mathematical operations.


Why the Code Needs to Be There


The Python code I'll provide is designed to compute the factorial of a non-negative integer input by the user. This will help you understand its intricacies and formulas.

Supplies

You will need a software that can run Python like Pycharm ot Google Colab.

User Input

Image 30-08-24 at 6.30 PM.jpeg

• The script starts by asking the user to input a non-negative integer.

• input() is a built-in function that takes input from the user as a string. int() converts that string into an integer.

• The user’s input is stored in the variable num.

Why it’s needed: The code needs to know the number for which the factorial should be calculated. This line captures the user’s input and converts it to an integer.

Code:

num = int(input("Enter a non-negative integer: "))

Initialize the Factorial Variable:

Image 30-08-24 at 6.30 PM (1).jpeg

• A variable named factorial is initialized to 1. This will hold the result of the factorial calculation as the program progresses.

Why it’s needed: Factorials involve multiplication, and since multiplying by 1 doesn’t change the value, starting with 1 is a logical initial value. This variable will store the running product of numbers as the factorial is calculated.

Code:

factorial = 1

Handling Negative Input:

Image 30-08-24 at 6.30 PM (2).jpeg

• The script checks if the input number is negative by using an if statement.

• If num is less than 0, it prints a message saying that the factorial is not defined for negative numbers. This is because the factorial function is only defined for non-negative integers.

Why it’s needed: Factorials are only defined for non-negative integers. This condition ensures that the user doesn’t input an invalid number, which would lead to an incorrect or undefined result. If the number is negative, the code stops further processing and informs the user of the error.

Code:

if num < 0:

print("Factorial is not defined for negative numbers.")

Handling the Special Case of Zero:

Image 30-08-24 at 6.30 PM (3).jpeg

• If the input number is 0, the script prints that the factorial of 0 is 1.

• This is because by definition, 0! = 1 .

Why it’s needed: By definition, the factorial of 0 is 1. This line of code handles this special case directly without entering the loop. It ensures the correct result is immediately returned if the user inputs 0.

Code:

elif num == 0:

print("The factorial of 0 is 1.")

Calculating the Factorial for Positive Numbers:

Image 30-08-24 at 6.30 PM (4).jpeg

• If num is a positive integer, the script enters a for loop.

• range(1, num + 1) generates a sequence of numbers from 1 to num.

• For each number i in this sequence, factorial is multiplied by i (factorial *= i). This operation accumulates the product of all integers from 1 to num.

• After the loop completes, the script prints the calculated factorial.

Why it’s needed: For any positive integer n, the factorial is calculated by multiplying all integers from 1 up to n. The for loop iterates through each number in this range, multiplying it with the factorial variable, which accumulates the result. This process is essential to compute the factorial of any given positive number.


Code:

else:

for i in range(1, num + 1):

factorial *= i

print("The factorial of", num, "is", factorial)

Summary

Image 30-08-24 at 6.31 PM.jpeg

The code structure is designed to handle different cases—negative input, zero, and positive integers—to ensure that the correct factorial is calculated for any non-negative integer the user inputs. Each part of the code serves a specific purpose in guiding the user and accurately computing the factorial.


Example Execution


Input: 5

Output: “The factorial of 5 is 120”

Input: 0

Output: “The factorial of 0 is 1”

Input: -3

Output: “Factorial is not defined for negative numbers.”


And, as usual, I will share the Google Colab link for your preference: https://colab.research.google.com/drive/1JRapgvN5cfsyea6Zu8M3TeRaNvxIVjO5?usp=sharing