Code ATLAS

Factorial Calculator

Visualize how factorial is calculated using iterative and recursive approaches

Factorial Visualization
Calculation Method:
Iterative
Calculating:
5! = 120
Current Step
Completed
Pending
Mathematical Definition:
n! = n × (n-1) × (n-2) × ... × 2 × 1
Special case: 0! = 1
Input Controls

Current input: 5 (calculating 5!)

Uses a loop to calculate factorial step by step

Calculation Controls
Algorithm Analysis

Time Complexity

Iterative:
O(n)
Recursive:
O(n)

Space Complexity

Iterative:
O(1)
Recursive:
O(n)
Current Statistics
Input:
5
Method:
iterative
Total Steps:
0
Current Step:
0
Result:
Calculating...
Algorithm Properties

Iterative Approach:

  • Space Efficient: Uses constant O(1) space
  • Simple: Easy to understand and implement
  • No Stack Overflow: No risk of call stack overflow
  • Predictable: Linear execution pattern

Recursive Approach:

  • Elegant: Matches mathematical definition
  • Intuitive: Natural recursive structure
  • Stack Usage: Uses O(n) call stack space
  • Risk: Can cause stack overflow for large n
How Factorial Works

Mathematical Definition: n! = n × (n-1) × (n-2) × ... × 2 × 1

Base Case: 0! = 1 and 1! = 1

Iterative: Use a loop to multiply numbers from 1 to n

Recursive: n! = n × (n-1)! with base case n ≤ 1

Performance Comparison

Iterative:

  • • Time: O(n)
  • • Space: O(1)
  • • No stack overflow
  • • More memory efficient

Recursive:

  • • Time: O(n)
  • • Space: O(n)
  • • Risk of stack overflow
  • • More elegant code
Real-World Applications
  • Combinatorics: Counting permutations and combinations
  • Probability: Calculating probabilities in statistics
  • Algorithms: Dynamic programming and recursive algorithms
  • Mathematics: Taylor series and mathematical analysis
  • Computer Science: Analysis of algorithms and complexity
Common Factorial Values
0! =1
1! =1
2! =2
3! =6
4! =24
5! =120
6! =720
7! =5,040