1 Star 0 Fork 63

雁秋 / C-Sharp

forked from 编程语言算法集 / C-Sharp 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
BinomialSequence.cs 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
Andrii Siriak 提交于 2021-05-08 21:42 . Cleanup minor things (#221)
using System.Collections.Generic;
using System.Numerics;
namespace Algorithms.Sequences
{
/// <summary>
/// <para>
/// Sequence of binomial coefficients.
/// </para>
/// <para>
/// Wikipedia: https://en.wikipedia.org/wiki/Binomial_coefficient.
/// </para>
/// <para>
/// OEIS: http://oeis.org/A007318.
/// </para>
/// </summary>
public class BinomialSequence : ISequence
{
/// <summary>
/// Gets sequence of binomial coefficients.
/// </summary>
public IEnumerable<BigInteger> Sequence
{
get
{
var i = 0;
while (true)
{
var row = GenerateRow(i);
foreach (var coefficient in row)
{
yield return coefficient;
}
i++;
}
}
}
private static BigInteger BinomialCoefficient(long n, long k)
{
if (k == 0 || k == n)
{
return new BigInteger(1);
}
if (n < 0)
{
return new BigInteger(0);
}
return BinomialCoefficient(n - 1, k) + BinomialCoefficient(n - 1, k - 1);
}
private static IEnumerable<BigInteger> GenerateRow(long n)
{
long k = 0;
while (k <= n)
{
yield return BinomialCoefficient(n, k);
k++;
}
}
}
}
C#
1
https://gitee.com/xyesterday/C-Sharp.git
git@gitee.com:xyesterday/C-Sharp.git
xyesterday
C-Sharp
C-Sharp
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891