Fibonacci Sequence Generator
Memory-efficient generator for Fibonacci numbers. Produces infinite sequence without storing all values in memory.
Python7/16/2025
#algorithms#generators#math
Python
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b def fibonacci_nth(n): """Get the nth Fibonacci number (0-indexed)""" if n <= 1: return n a, b = 0, 1 for _ in range(2, n + 1): a, b = b, a + b return b def fibonacci_range(start, end): """Generate Fibonacci numbers in a range""" gen = fibonacci() result = [] for num in gen: if num > end: break if num >= start: result.append(num) return result
...
Loading comments...