Beispiel #1
0
        static void Faulhaber(int p)
        {
            Console.Write("{0} : ", p);
            Frac q    = new Frac(1, p + 1);
            int  sign = -1;

            for (int j = 0; j <= p; j++)
            {
                sign *= -1;
                Frac coeff = q * new Frac(sign, 1) * new Frac(Binomial(p + 1, j), 1) * Bernoulli(j);
                if (Frac.ZERO == coeff)
                {
                    continue;
                }
                if (j == 0)
                {
                    if (Frac.ONE != coeff)
                    {
                        if (-Frac.ONE == coeff)
                        {
                            Console.Write("-");
                        }
                        else
                        {
                            Console.Write(coeff);
                        }
                    }
                }
                else
                {
                    if (Frac.ONE == coeff)
                    {
                        Console.Write(" + ");
                    }
                    else if (-Frac.ONE == coeff)
                    {
                        Console.Write(" - ");
                    }
                    else if (Frac.ZERO < coeff)
                    {
                        Console.Write(" + {0}", coeff);
                    }
                    else
                    {
                        Console.Write(" - {0}", -coeff);
                    }
                }
                int pwr = p + 1 - j;
                if (pwr > 1)
                {
                    Console.Write("n^{0}", pwr);
                }
                else
                {
                    Console.Write("n");
                }
            }
            Console.WriteLine();
        }
Beispiel #2
0
 static Frac Bernoulli(int n)
 {
     if (n < 0)
     {
         throw new ArgumentException("n may not be negative or zero");
     }
     Frac[] a = new Frac[n + 1];
     for (int m = 0; m <= n; m++)
     {
         a[m] = new Frac(1, m + 1);
         for (int j = m; j >= 1; j--)
         {
             a[j - 1] = (a[j - 1] - a[j]) * new Frac(j, 1);
         }
     }
     // returns 'first' Bernoulli number
     if (n != 1)
     {
         return(a[0]);
     }
     return(-a[0]);
 }