Using C++20 co_yield to produce lazy numeric sequences:
#include <generator>
std::generator<uint64_t> Fibonacci()
{
uint64_t a = 0, b = 1;
while (true) {
co_yield a;
auto next = a + b;
a = b;
b = next;
}
}
// Usage: lazily consumes only what is requested!
for (uint64_t val : Fibonacci() | std::views::take(10)) {
std::cout << val << " ";
}Generates Fibonacci numbers on demand in $O(1)$ memory without creating large vectors!