- Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathVanEcksSequence.cs
43 lines (39 loc) · 1.21 KB
/
VanEcksSequence.cs
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
34
35
36
37
38
39
40
41
42
43
usingSystem.Collections.Generic;
usingSystem.Numerics;
namespaceAlgorithms.Sequences;
/// <summary>
/// <para>
/// Van Eck's sequence. For n >= 1, if there exists an m < n such that a(m) = a(n), take the largest such m and set a(n+1) = n-m; otherwise a(n+1) = 0. Start with a(1)=0.
/// </para>
/// <para>
/// OEIS: http://oeis.org/A181391.
/// </para>
/// </summary>
publicclassVanEcksSequence:ISequence
{
/// <summary>
/// Gets Van Eck's sequence.
/// </summary>
publicIEnumerable<BigInteger>Sequence
{
get
{
yieldreturn0;
vardictionary=newDictionary<BigInteger,BigInteger>();
BigIntegerprevious=0;
BigIntegercurrentIndex=2;// 1-based index
while(true)
{
BigIntegerelement=0;
if(dictionary.TryGetValue(previous,outvarpreviousIndex))
{
element=currentIndex-previousIndex;
}
yieldreturnelement;
dictionary[previous]=currentIndex;
previous=element;
currentIndex++;
}
}
}
}