18 Star 133 Fork 63

编程语言算法集 / C-Sharp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
MakeChangeSequence.cs 1.64 KB
一键复制 编辑 原始数据 按行查看 历史
Gerson Jr 提交于 2024-01-03 22:51 . Switch to file-scoped namespaces (#432)
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;
}
}
}
}
C#
1
https://gitee.com/TheAlgorithms/C-Sharp.git
git@gitee.com:TheAlgorithms/C-Sharp.git
TheAlgorithms
C-Sharp
C-Sharp
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891