1044. Factorial Generator

Write a generator function that takes an integer n as an argument and returns a generator object which yields the factorial sequence.

The factorial sequence is defined by the relation n! = n * (n-1) * (n-2) * ... * 2 * 1​​​.

The factorial of 0 is defined as 1.

Example 1:

Example 1:

Input: n = 5
Output: [1,2,6,24,120]
Explanation: const gen = factorial(5) gen.next().value // 1 gen.next().value // 2 gen.next().value // 6 gen.next().value // 24 gen.next().value // 120

Example 2:

Example 2:

Input: n = 2
Output: [1,2]
Explanation: const gen = factorial(2) gen.next().value // 1 gen.next().value // 2

Now Your Turn!

Pick the correct output for the given input
Input: n = 0

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n <= 18

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
// Input: n (integer) - number of factorial values to generate
// Output: generator that yields factorial values 1! , 2! , 3! ... n! one at a time
class Solution {
factorial(n) {
// User code goes here
}
}
Test Case

Input:

N