forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTetranacciNumbersSequence.cs
More file actions
33 lines (31 loc) · 888 Bytes
/
Copy pathTetranacciNumbersSequence.cs
File metadata and controls
33 lines (31 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace Algorithms.Sequences;
/// <summary>
/// <para>
/// Tetranacci numbers: a(n) = a(n-1) + a(n-2) + a(n-3) + a(n-4) with a(0) = a(1) = a(2) = a(3) = 1.
/// </para>
/// <para>
/// OEIS: https://oeis.org/A000288.
/// </para>
/// </summary>
public class TetranacciNumbersSequence : ISequence
{
public IEnumerable<BigInteger> Sequence
{
get
{
var buffer = Enumerable.Repeat(BigInteger.One, 4).ToArray();
while (true)
{
yield return buffer[0];
var next = buffer[0] + buffer[1] + buffer[2] + buffer[3];
buffer[0] = buffer[1];
buffer[1] = buffer[2];
buffer[2] = buffer[3];
buffer[3] = next;
}
}
}
}