forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MakeChangeSequence.cs
55 lines (51 loc) · 1.64 KB
/
MakeChangeSequence.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
44
45
46
47
48
49
50
51
52
53
54
55
using System.Collections.Generic;
using System.Numerics;
namespace Algorithms.Sequences;
/// <summary>
/// <para>
/// Number of ways of making change for n cents using coins of 1, 2, 5, 10 cents.
/// </para>
/// <para>
/// OEIS: https://oeis.org/A000008.
/// </para>
/// </summary>
public class MakeChangeSequence : ISequence
{
/// <summary>
/// <para>
/// Gets sequence of number of ways of making change for n cents
/// using coins of 1, 2, 5, 10 cents.
/// </para>
/// <para>
/// Uses formula from OEIS page by Michael Somos
/// along with first 17 values to prevent index issues.
/// </para>
/// <para>
/// Formula:
/// a(n) = a(n-2) +a(n-5) - a(n-7) + a(n-10) - a(n-12) - a(n-15) + a(n-17) + 1.
/// </para>
/// </summary>
public IEnumerable<BigInteger> Sequence
{
get
{
var seed = new List<BigInteger>
{
1, 1, 2, 2, 3, 4, 5, 6, 7, 8,
11, 12, 15, 16, 19, 22, 25,
};
foreach (var value in seed)
{
yield return value;
}
for(var index = 17; ; index++)
{
BigInteger newValue = seed[index - 2] + seed[index - 5] - seed[index - 7]
+ seed[index - 10] - seed[index - 12] - seed[index - 15]
+ seed[index - 17] + 1;
seed.Add(newValue);
yield return newValue;
}
}
}
}