Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,53 @@
using Mpir.NET;
using System;
namespace Bernoulli
{
class Program
{
private static void bernoulli(mpq_t rop, uint n)
{
mpq_t[] a = new mpq_t[n + 1];
for (uint i = 0; i < n + 1; i++)
{
a[i] = new mpq_t();
}
for (uint m = 0; m <= n; ++m)
{
mpir.mpq_set_ui(a[m], 1, m + 1);
for (uint j = m; j > 0; --j)
{
mpir.mpq_sub(a[j - 1], a[j], a[j - 1]);
mpir.mpq_set_ui(rop, j, 1);
mpir.mpq_mul(a[j - 1], a[j - 1], rop);
}
mpir.mpq_set(rop, a[0]);
}
}
static void Main(string[] args)
{
mpq_t rop = new mpq_t();
mpz_t n = new mpz_t();
mpz_t d = new mpz_t();
for (uint i = 0; i <= 60; ++i)
{
bernoulli(rop, i);
if (mpir.mpq_cmp_ui(rop, 0, 1) != 0)
{
mpir.mpq_get_num(n, rop);
mpir.mpq_get_den(d, rop);
Console.WriteLine(string.Format("B({0, 2}) = {1, 44} / {2}", i, n, d));
}
}
Console.ReadKey();
}
}
}

View file

@ -0,0 +1,51 @@
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);
}
}
}

View file

@ -0,0 +1,58 @@
using System;
using System.Numerics;
using System.Collections.Generic;
namespace bern
{
class Program
{
struct BerNum { public int index; public BigInteger Numer, Denomin; };
static int w1 = 1, w2 = 1; // widths for formatting output
static int max = 60; // default maximum, can override on command line
// returns nth Bernoulli number
static BerNum CalcBernoulli(int n)
{
BerNum res;
BigInteger f;
BigInteger[] nu = new BigInteger[n + 1],
de = new BigInteger[n + 1];
for (int m = 0; m <= n; m++)
{
nu[m] = 1; de[m] = m + 1;
for (int j = m; j > 0; j--)
if ((f = BigInteger.GreatestCommonDivisor(
nu[j - 1] = j * (de[j] * nu[j - 1] - de[j - 1] * nu[j]),
de[j - 1] *= de[j])) != BigInteger.One)
{ nu[j - 1] /= f; de[j - 1] /= f; }
}
res.index = n; res.Numer = nu[0]; res.Denomin = de[0];
w1 = Math.Max(n.ToString().Length, w1); // ratchet up widths
w2 = Math.Max(res.Numer.ToString().Length, w2);
if (max > 50) Console.Write("."); // progress dots appear for larger values
return res;
}
static void Main(string[] args)
{
List<BerNum> BNumbList = new List<BerNum>();
// defaults to 60 when no (or invalid) command line parameter is present
if (args.Length > 0) {
int.TryParse(args[0], out max);
if (max < 1 || max > Int16.MaxValue) max = 60;
if (args[0] == "0") max = 0;
}
for (int i = 0; i <= max; i++) // fill list with values
{
BerNum BNumb = CalcBernoulli(i);
if (BNumb.Numer != BigInteger.Zero) BNumbList.Add(BNumb);
}
if (max > 50) Console.WriteLine();
string strFmt = "B({0, " + w1.ToString() + "}) = {1, " + w2.ToString() + "} / {2}";
// display formatted list
foreach (BerNum bn in BNumbList)
Console.WriteLine(strFmt , bn.index, bn.Numer, bn.Denomin);
if (System.Diagnostics.Debugger.IsAttached) Console.Read();
}
}
}