RosettaCodeData/Task/Bernoulli-numbers/C-sharp/bernoulli-numbers-2.cs
2023-07-01 13:44:08 -04:00

51 lines
1.4 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Console;
using System.Linq;
using MathNet.Numerics;
namespace Rosettacode.Rational.CS
{
class Program
{
private static readonly Func<int, BigRational> = BigRational.FromInt;
private static BigRational CalculateBernoulli(int n)
{
var a = InitializeArray(n);
foreach(var m in Enumerable.Range(1,n))
{
a[m] = (1) / ((m) + (1));
for (var j = m; j >= 1; j--)
{
a[j-1] = (j) * (a[j-1] - a[j]);
}
}
return a[0];
}
private static BigRational[] InitializeArray(int n)
{
var a = new BigRational[n + 1];
for (var x = 0; x < a.Length; x++)
{
a[x] = (x + 1);
}
return a;
}
static void Main()
{
Enumerable.Range(0, 61) // the second parameter is the number of range elements, and is not the final item of the range.
.Select(n => new {N = n, BernoulliNumber = CalculateBernoulli(n)})
.Where(b => !b.BernoulliNumber.Numerator.IsZero)
.Select(b => string.Format("B({0, 2}) = {1, 44} / {2}", b.N, b.BernoulliNumber.Numerator, b.BernoulliNumber.Denominator))
.ToList()
.ForEach(WriteLine);
}
}
}