Infinite fibonacci sequence, lazy take first 42 values

Salih Dincer salihdb at hotmail.com
Thu Apr 21 08:00:07 UTC 2022


On Thursday, 21 April 2022 at 05:00:53 UTC, Alain De Vos wrote:
>
> This example limits the maximum value returned by the fibonacci 
> function. f(n) < limit
> But it does not allow to return the n-th element of a fibonacci 
> function.

You are free to use ```take():```

```d
struct FibonacciRange(long l) { /*...*/ }

enum limit = long.max;

void main()
{
   FibonacciRange!limit fibs;
   auto fibFirst90 = fibs.take(90);
   auto total = fibFirst90.sum;
        total.writeln; // "7540113804746346428" == fib(92)-1

   auto fibInfinite = recurrence!("a[n-1] + a[n-2]")(1L, 1L);
   assert(fibInfinite.take(92).array[$-1]-1 == total);
}
```
SDB at 79


More information about the Digitalmars-d-learn mailing list