Python Encyclopedia for Academics
  • Course Outline
  • Artificial Intelligence
    • Data Science Foundation
      • Python Programming
        • Introduction and Basics
          • Variables
          • Print Function
          • Input From User
          • Data Types
          • Type Conversion
        • Operators
          • Arithmetic Operators
          • Relational Operators
          • Bitwise Operators
          • Logical Operators
          • Assignment Operators
          • Compound Operators
          • Membership Operators
          • Identity Operators
      • Numpy
        • Vectors, Matrix
        • Operations on Matrix
        • Mean, Variance, and Standard Deviation
        • Reshaping Arrays
        • Transpose and Determinant of Matrix
      • Pandas
        • Series and DataFrames
        • Slicing, Rows, and Columns
        • Operations on DataFrames
        • Different wayes to creat DataFrame
        • Read, Write Operations with CSV files
      • Matplotlib
        • Graph Basics
        • Format Strings in Plots
        • Label Parameters, Legend
        • Bar Chart, Pie Chart, Histogram, and Scatter Plot
  • Machine Learning Algorithms
    • Regression Analysis In ML
      • Regression Analysis in Machine Learning
      • Proof of Linear Regression Formulas
      • Simple Linear Regression Implementation
      • Multiple Linear Regression
      • Advertising Dataset Example
      • Bike Sharing Dataset
      • Wine Quality Dataset
      • Auto MPG Dataset
    • Classification Algorithms in ML
      • Proof of Logistic Regression
      • Simplified Mathematical Proof of SVM
      • Iris Dataset
  • Machine Learning Laboratory
    • Lab 1: Titanic Dataset
      • Predicting Survival on the Titanic with Machine Learning
    • Lab 2: Dow Jones Index Dataset
      • Dow Jones Index Predictions Using Machine Learning
    • Lab 3: Diabetes Dataset
      • Numpy
      • Pandas
      • Matplotlib
      • Simple Linear Regression
      • Simple Non-linear Regression
      • Performance Matrix
      • Preprocessing
      • Naive Bayes Classification
      • K-Nearest Neighbors (KNN) Classification
      • Decision Tree & Random Forest
      • SVM Classifier
      • Logistic Regression
      • Artificial Neural Network
      • K means Clustering
    • Lab 4: MAGIC Gamma Telescope Dataset
      • Classification in ML-MAGIC Gamma Telescope Dataset
    • Lab 5: Seoul Bike Sharing Demand Dataset
      • Regression in ML-Seoul Bike Sharing Demand Dataset
    • Lab 6: Medical Cost Personal Datasets
      • Predict Insurance Costs with Linear Regression in Python
    • Lab 6: Predict The S&P 500 Index With Machine Learning And Python
      • Predict The S&P 500 Index With Machine Learning And Python
  • Artificial Neural Networks
    • Biological Inspiration vs. Artificial Neurons
    • Review linear algebra and calculus essentials for ANNs
    • Activation Function
  • Mathematics
    • Pre-Calculus
      • Factorials
      • Roots of Polynomials
      • Complex Numbers
      • Polar Coordinates
      • Graph of a Function
    • Calculus 1
      • Limit of a Function
      • Derivative of Function
      • Critical Points
      • Indefinite Integrals
  • Calculus 2
    • 3D Coordinates and Vectors
    • Vectors and Vector Operations
    • Lines and Planes in Space (3D)
    • Partial Derivatives
    • Optimization Problems (Maxima/Minima) in Multivariable Functions
    • Gradient Vectors
  • Engineering Mathematics
    • Laplace Transform
  • Electrical & electronics Eng
    • Resistor
      • Series Resistors
      • Parallel Resistors
    • Nodal Analysis
      • Example 1
      • Example 2
    • Transient State
      • RC Circuit Equations in the s-Domain
      • RL Circuit Equations in the s-Domain
      • LC Circuit Equations in the s-Domain
      • Series RLC Circuit with DC Source
  • Computer Networking
    • Fundamental
      • IPv4 Addressing
      • Network Diagnostics
  • Cybersecurity
    • Classical Ciphers
      • Caesar Cipher
      • Affine Cipher
      • Atbash Cipher
      • Vigenère Cipher
      • Gronsfeld Cipher
      • Alberti Cipher
      • Hill Cipher
Powered by GitBook
On this page
  • What is a Factorial?
  • Examples:
  • Step-by-Step: How Factorial Works
  • Factorial in Python (3 Ways)
  • Visualizing Factorial Growth (with matplotlib)
  • Keywords
  1. Mathematics
  2. Pre-Calculus

Factorials

Nerd Cafe

What is a Factorial?

In mathematics, the factorial of a non-negative integer n is the product of all positive integers less than or equal to n.

Mathematical Definition:

n!=n×(n−1)×(n−2)×...×3×2×1n!=n\times (n-1)\times (n-2) \times ...\times 3\times 2\times 1n!=n×(n−1)×(n−2)×...×3×2×1

Base Case:

0!=10!=10!=1

This is defined (not derived) so that formulas like permutations and combinations work correctly.

Examples:

n
n!
Explanation

0

1

Defined as 1

1

1

1

2

2

2 × 1

3

6

3 × 2 × 1

4

24

4 × 3 × 2 × 1

5

120

5 × 4 × 3 × 2 × 1

Step-by-Step: How Factorial Works

Let’s walk through how 4! is calculated step-by-step:

Step 1: Start with 4
Step 2: Multiply by 3 → 4 × 3 = 12
Step 3: Multiply by 2 → 12 × 2 = 24
Step 4: Multiply by 1 → 24 × 1 = 24

So:

4!=4×3×2×1=24

Factorial in Python (3 Ways)

1. Iterative Approach (Using a Loop)

def factorial_iterative(n):
    result = 1
    for i in range(1, n + 1):
        result *= i
    return result

print(factorial_iterative(5))  # Output: 120

Output

120

2. Recursive Approach (Mathematical Style)

def factorial_recursive(n):
    if n == 0 or n == 1:
        return 1
    return n * factorial_recursive(n - 1)

print(factorial_recursive(5))  # Output: 120

3. Using Python’s Built-in math.factorial()

import math

print(math.factorial(5))  # Output: 120

Visualizing Factorial Growth (with matplotlib)

import matplotlib.pyplot as plt
import math

x = list(range(10))  # 0! to 9!
y = [math.factorial(i) for i in x]

plt.plot(x, y, marker='o')
plt.title('Factorial Growth')
plt.xlabel('n')
plt.ylabel('n!')
plt.grid(True)
plt.show()

Output

Keywords

factorial,math,python,factorial function,recursive factorial,iterative factorial,math.factorial,combinatorics,permutations,recursion,loop,series,exponential growth,factorial table,mathematics,factorial formula,factorial examples,visualizing factorials,python math,algorithm, nerd cafe

PreviousPre-CalculusNextRoots of Polynomials

Last updated 1 month ago