Authors:
Release Date: January 9, 2022
License: MIT License
To execute this implementation, ensure you have Python 3.x installed and follow these steps:
python FactorialSequence.pyThe Factorial Sequence is a series of numbers where each term
The factorial of a non-negative integer
Equivalently, it is the product of all positive integers from 1 up to
For large
-
Iterative vs Recursive: While factorials are often used to teach recursion, this implementation uses an iterative generator to avoid stack overflow for large
$n$ and minimize memory overhead. - Large Integer Handling: Python natively supports arbitrary-precision integers, allowing the calculation of large factorial values that would overflow a standard 64-bit integer.
-
Complexity:
-
Time Complexity:
$O(n^2)$ total for the sequence up to$n$ , as each multiplication takes$O(\text{bits})$ time and the number of bits grows. -
Space Complexity:
$O(1)$ auxiliary space (excluding the output), as the generator yields values one by one.
-
Time Complexity:
- Memory-Efficient Generation: Employs the
yieldkeyword to provide values lazily, which is critical when processing the rapidly growing terms of the sequence. - Input Validation: Strictly enforces non-negative integer constraints to ensure mathematical correctness.
flowchart TD
A["Start: n"] --> B["Initialize acc = 1"]
B --> C["Yield acc (0!)"]
C --> D["For i from 1 to n"]
D --> E["acc = acc * i"]
E --> F["Yield acc (i!)"]
F --> G["i < n?"]
G -- Yes --> D
G -- No --> H["End"]
