Pagina's

2013/07/18

Pascal's triangle



// Horizontal row in Pascal's triangle

using System;
class Pascal_Triangle_Horizontal_Row
{
    private static int[] fr(int n)            // full row
    {                                         // 0=>{1} 1=>{1,1} 2=>{1,2,1} 3=>{1,3,3,1} ...
        int i = 0, j = n / 2, k = 1;
        int[] c = new int[n + 1];
        c[0] = 1;
        for (; i < j; i++, k++)
            c[k] = c[i] * (n - i) / k;
        for (i = 0; n > i; i++, n--)
            c[n] = c[i];
        return c;
    }

    private static int[] pfr(int[] d)         // previous full row
    {                                         // ...=>{1,3,3,1}=>{1,2,1}=>{1,1}=>{1}=>{}=>{} 
        int n = d.Length - 2;
        if (n < 0)
            return new int[] { };
        int i = 1, m = n / 2;
        int[] c = new int[n + 1];
        c[0] = 1;
        for (; i <= m; i++)
            c[i] = d[i] - c[i - 1];
        for (i = 0; n > i; i++, n--)
            c[n] = c[i];
        return c;
    }

    private static int[] nfr(int[] b)         // next full row
    {                                         // {}=>{1}=>{1,1}=>{1,2,1}=>{1,3,3,1}=>...
        int i = 1, n = b.Length, m = n / 2;
        int[] c = new int[n + 1];
        c[0] = 1;
        for (; i <= m; i++)
            c[i] = b[i - 1] + b[i];
        for (i = 0; n > i; i++, n--)
            c[n] = c[i];
        return c;
    }

    private static int[] hr(int n)            // half row
    {                                         // 0=>{1} 1=>{1} 2=>{1,2} 3=>{1,3} 4=>{1,4,6} ...
        int i = 0, j = n / 2, k = 1;
        int[] c = new int[j + 1];
        c[0] = 1;
        for (; i < j; i++, k++, n--)
            c[k] = c[i] * n / k;
        return c;
    }

    private static int[] phr(int[] d)         // previous half row
    {                                         // ...=>{1,4,6}=>{1,3}=>{1,2}=>{1}=>{}=>{}
        if (d.Length < 2)
            return new int[] { };
        int i = 1, n = (d[1] + 1) / 2;
        int[] c = new int[n];
        c[0] = 1;
        for (; i < n; i++)
            c[i] = d[i] - c[i - 1];
        return c;
    }

    private static int[] nhr(int[] b)         // next half row
    {                                         // {}=>{1}=>{1,2}=>{1,3}=>{1,4,6}=>...
        switch (b.Length)
        {
            case 0: return new int[] { 1 };
            case 1: return new int[] { 1, 2 };
            default:
                int i = 1, b1 = b[1], n = (b1 + 3) / 2;
                b1 &= 1;
                int[] c = new int[n--];
                c[0] = 1;
                for (; i < n; i++)
                    c[i] = b[i] + b[i - 1];
                c[i] = (1 - b1) * b[i - 1] + (1 + b1) * b[i - b1];
                return c;
        }
    }

    static void Main()
    {
        int[] c = new int[] { };              // 1
        for (int i = 0; i < 9; i++)           // 1 1
        {                                     // 1 2 1
            c = nfr(c);                       // 1 3 3 1
            foreach (int j in c)              // 1 4 6 4 1
                Console.Write(j + " ");       // 1 5 10 10 5 1
            Console.WriteLine();              // 1 6 15 20 15 6 1      
        }                                     // 1 7 21 35 35 21 7 1   
        Console.ReadLine();                   // 1 8 28 56 70 56 28 8 1
    }
}

No comments:

Post a Comment