RosettaCodeData/Task/Fibonacci-sequence/Elena/fibonacci-sequence-2.elena

41 lines
669 B
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
import extensions;
2025-08-11 18:05:26 -07:00
singleton FibonacciEnumerable : Enumerable
2023-07-01 11:58:00 -04:00
{
2025-08-11 18:05:26 -07:00
Enumerator enumerator()
= FibonacciEnumerable.infinitEnumerator();
yield Enumerator infinitEnumerator()
{
2023-07-01 11:58:00 -04:00
long n_2 := 1l;
long n_1 := 1l;
2025-08-11 18:05:26 -07:00
:yield n_2;
:yield n_1;
2023-07-01 11:58:00 -04:00
while(true)
{
long n := n_2 + n_1;
2025-08-11 18:05:26 -07:00
:yield n;
2023-07-01 11:58:00 -04:00
n_2 := n_1;
n_1 := n
}
2025-08-11 18:05:26 -07:00
}
2023-07-01 11:58:00 -04:00
}
2026-02-01 16:33:20 -08:00
public Program()
2023-07-01 11:58:00 -04:00
{
2025-08-11 18:05:26 -07:00
auto e := FibonacciEnumerable.enumerator();
if(!e.next())
InvalidOperationException.raise();
2023-07-01 11:58:00 -04:00
2025-08-11 18:05:26 -07:00
for(int i := 0; i < 10 && e.next(); i += 1) {
Console.printLine(*e)
2023-07-01 11:58:00 -04:00
};
2025-08-11 18:05:26 -07:00
Console.readChar()
2023-07-01 11:58:00 -04:00
}