Advertisement
pacho_the_python

Factorial Generator

Apr 20th, 2024
946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.44 KB | None | 0 0
  1. def nat():
  2.     num = 0
  3.     while True:
  4.         yield num
  5.         num += 1
  6.  
  7.  
  8. def factorial_new(n):
  9.     """
  10.    Python generator function to generate factorials.
  11.    """
  12.     natural_nums = nat()
  13.     my_nums = [next(natural_nums) for _ in range(n + 1)][1:]
  14.     result = 1
  15.     if n == 0:
  16.         return result
  17.  
  18.     for i in my_nums:
  19.         if i > n:
  20.             break
  21.         result *= i
  22.     return result
  23.  
  24.  
  25. print(factorial_new(6))
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement