Pagina's

2013/08/14

Stirling numbers of the second kind

/*

   k                                                                                  1       1      1    1   1 1
 n 0 1     2       3        4         5         6         7         8        9        0       1      2    3   4 5
 0 1 0     0       0        0         0         0         0         0        0        0       0      0    0   0 0
 1 0 1     0       0        0         0         0         0         0        0        0       0      0    0   0 0
 2 0 1     1       0        0         0         0         0         0        0        0       0      0    0   0 0
 3 0 1     3       1        0         0         0         0         0        0        0       0      0    0   0 0
 4 0 1     7       6        1         0         0         0         0        0        0       0      0    0   0 0
 5 0 1    15      25       10         1         0         0         0        0        0       0      0    0   0 0
 6 0 1    31      90       65        15         1         0         0        0        0       0      0    0   0 0
 7 0 1    63     301      350       140        21         1         0        0        0       0      0    0   0 0
 8 0 1   127     966     1701      1050       266        28         1        0        0       0      0    0   0 0
 9 0 1   255    3025     7770      6951      2646       462        36        1        0       0      0    0   0 0
10 0 1   511    9330    34105     42525     22827      5880       750       45        1       0      0    0   0 0
11 0 1  1023   28501   145750    246730    179487     63987     11880     1155       55       1      0    0   0 0
12 0 1  2047   86526   611501   1379400   1323652    627396    159027    22275     1705      66      1    0   0 0
13 0 1  4095  261625  2532530   7508501   9321312   5715424   1899612   359502    39325    2431     78    1   0 0
14 0 1  8191  788970 10391745  40075035  63436373  49329280  20912320  5135130   752752   66066   3367   91   1 0
15 0 1 16383 2375101 42355950 210766920 420693273 408741333 216627840 67128490 12662650 1479478 106470 4550 105 1
 
                              coincidence?
Two formula's:
                       k
    S2(n,k) = 1/k! * SIGMA[(-1)^(k-j) * C(k,j) * j^n ]
                      j=0
  
    S2(n,k) = k * S2(n-1,k) + S2(n-1,k-1) 
  
    Initial value's: S2(j,j) = 1 , S2(j,0) = 0 , S2(0,j) = 0
 
The first formula: * powers, * / factorials, + - (large) numbers .
The second one leads to a recursive solution. It's going top-down,
a larger number depends on two smaller ones. Bottom-up seems to be
an easier, faster, iterative way.
  
Top-down:
 
    S2(5,3) = 3 * S2(4,3) + S2(4,2)
                  S2(4,3) = 3 * S2(3,3) + S2(3,2)
                            S2(4,2) = 2 * S2(3,2) + S2(2,2)
                                          S2(3,2) = 2 * S2(2,2) + S2(2 ...
                                                            ...complicated...
Bottom-up:
 
     Rearrange table, remove empty (zero) items in colums:
      _             _            _             _
     | 1             |          | 1   1   1   1 |
     | 0   1         |  =====>  | 0   1   3   6 |
     | 0   1   1     |          |_0   1   7  25_|
     | .   1   3   1 |          
     | .   .   7   6 |
     |_.   .   .  25_|
     
       1   1   1 + 2 * 0 = 1   1 + 3 * 0 =  1
       0   1   1 + 2 * 1 = 3   3 + 3 * 1 =  6
       0   1   1 + 2 * 3 = 7   7 + 3 * 6 = 25 = S2(5,3)  ...less complicated...


          |---------------------------------------------------------------|
          |                             S2(n,k)                           |
          |------|------|-----------------------------------------|-------|
          |    n |    k |     Time in ms (Athlon X4, XP, 2GB)     |  bits |
          |------|------|---------|---------|----------|----------|-------|
          |  100 |   10 |    0,25 |         |          |          |   311 |
          |  100 |   25 |         |    0,61 |          |          |   381 |
          |  100 |   50 |         |         |     0,81 |          |   338 |
          |  100 |   89 |         |         |          |     0,27 |   108 |
          |      |      |         |         |          |          |       |
          |  200 |   20 |    1,5  |         |          |          |   804 |
          |  200 |   43 |         |    3,1  |          |          |   910 |
          |  200 |   86 |         |         |     4,4  |          |   838 |
          |  200 |  172 |         |         |          |     1,6  |   295 |
          |      |      |         |         |          |          |       |
          |  400 |   40 |    9,4  |         |          |          |  1970 |
          |  400 |   77 |         |   18    |          |          |  2131 |
          |  400 |  154 |         |         |    27    |          |  1986 |
          |  400 |  308 |         |         |          |    14    |   982 |
          |      |      |         |         |          |          |       |
          |  800 |   80 |   75    |         |          |          |  4663 |
          |  800 |  138 |         |  124    |          |          |  4900 |
          |  800 |  276 |         |         |   183    |          |  4617 |
          |  800 |  552 |         |         |          |   119    |  2744 |
          |      |      |         |         |          |          |       |
          | 1600 |  160 |  672    |         |          |          | 10770 |
          | 1600 |  250 |         | 1010    |          |          | 11109 |
          | 1600 |  500 |         |         |  1620    |          | 10546 |
          | 1600 | 1000 |         |         |          |  1120    |  6974 |
          |      |      |         |         |          |          |       |
          | 3200 |  320 | 7230    |         |          |          | 24424 |
          | 3200 |  456 |         | 9930    |          |          | 24889 |
          | 3200 |  912 |         |         | 15300    |          | 23765 |
          | 3200 | 1824 |         |         |          | 11900    | 16881 |
          |------|------|---------|---------|----------|----------|-------|
   
*/

using System;
using System.Diagnostics;
using Xint = System.Numerics.BigInteger;

class Stirling_2nd_kind
{
    private static Xint S2(uint n, uint k)
    {
        if (k >= n) return k == n ? 1 : 0;
        if (k <= 2) return k == 0 ? 0 : k == 1 ?
            1 : (Xint.One << (int)(n - 1)) - 1;
        if (k + 1 == n) return (Xint)(n) * k / 2;

        n -= k - 1;
        uint i = 0, j = 3;
        Xint[] A = new Xint[n];
        for (; i < n; i++)
            A[i] = 1;
        Xint[] B = new Xint[n];
        for (i = 0; i < n; i++) 
            B[i] = (Xint.One << (int)(i + 1)) - 1;

        do
        {
            for (i = 1; i < n; i++)
                A[i] = A[i - 1] * j + B[i];
            j++;

            if (j > k) return A[n - 1];

            for (i = 1; i < n; i++)
                B[i] = B[i - 1] * j + A[i];
            j++;
        }
        while (j <= k);
        return B[n - 1];
    }

    // to do: - use uints/ulongs: - for smaller value's / 
    //                            - as long as possible 
    //        - return row (array)
    //        - return previous/next row from row
    //        - k close to n , go from right to left in table ?
    //        - same/similar trick for: - other sequences ?
    //                                  - sequences using S2(n,k) ?

    private static Stopwatch sw = new Stopwatch();
    static void Main()
    {
        Console.WriteLine("S2(50,17) = " + S2(50, 17));
        Console.WriteLine("     bits : " + bL(S2(50, 17)));
        sw.Restart();
        for (int i = 0; i < 1000; i++) S2(50, 17);
        sw.Stop();
        Console.WriteLine("       ms : 0," + sw.ElapsedMilliseconds);
        Console.ReadLine();
        // S2(50,17) = 37645241791600906804871080818625037726247519045
        //      bits : 155
        //        ms : 0,134
    }

    private static int bL(Xint U)
    {
        byte[] bytes = (U.Sign * U).ToByteArray();
        int i = bytes.Length - 1;
        return i * 8 | bitLengthMostSignificantByte(bytes[i]);
    }
    private static int bitLengthMostSignificantByte(byte b)
    {
        return b < 08 ? b < 02 ? b < 01 ? 0 : 1 :
                                 b < 04 ? 2 : 3 :
                        b < 32 ? b < 16 ? 4 : 5 :
                                 b < 64 ? 6 : 7;
    }
}

2013/08/09

Cube Root

/*
___ \3 / Input: integer x >= 0 \/ x = y Output: integer y, such that y^3 <= x < (y+1)^3 Cube Roots are computed three to five times faster with the CR function, compared to the Nth Root function.
For small values it's obvious to use: " y = (uint)Math.Pow(x, 1d / 3)) ". It takes ~125 ns, but for example with x = 4503569204744003 (a 52 bits number), it returns 165139, wrong, it should be 165140. So errors have to be corrected. Another option: the "Integer Cube Root" algorithm from "Hacker's Delight" (see Refs). For a uint it takes ~25 ns, not too bad for a C# version, for a ulong ~330 ns. Up to ulong.MaxValue there are ~2.5 million perfect cubes, they can be generated like below. 3th powers: |-----|------|------|------|------| dy=growth of y, ddy=growth of dy, ... | x | y=x^3| dy | ddy | dddy | |-----|------|------|------|------| y=x^3 , y'=3*x^2 , y''=6*x , y'''=6 | 0 | 0 | | 0 | | | | | 1 | | 6 | y=x^n ,,,,,,,,,,,,,, y''''...=n! A000142 | 1 | 1 | | 6 | | | | | 7 | | 6 | y: The cubes A000578 | 2 | 8 | | 12 | | dy: Central hexagonal numbers A003215 | | | 19 | | 6 | ddy: Multiples of 6 A008588 | 3 | 27 | | 18 | | dddy: The six sequence A010722 | | | 37 | | 6 | ddddy: The zero sequence A000004 | 4 | 64 | | 24 | | |-----|------|------|------|------| y=x^4 : A000583 , A010863 , A101103 , A005914 , A005917 private static void abc() { uint n = 0, a = 0, b = 1, c = 6; Console.WriteLine("// {0,2}{1,4}{2,4}{3,3}", n, a, b, c); // 0 0 1 6 while (n < 7) // 1 1 7 12 { // 2 8 19 18 n++; // 3 27 37 24 a += b; // 4 64 61 30 b += c; // 5 125 91 36 c += 6; // 6 216 127 42 Console.WriteLine("// {0,2}{1,4}{2,4}{3,3}", n, a, b, c); // 7 343 169 48 } Console.ReadLine(); } 4th powers: private static void abcd() { uint n = 0; uint a = 0, b = 1, c = 14, d = 36; Console.WriteLine("// {0,2}{1,5}{2,5}{3,4}{4,4}", n, a, b, c, d); // 0 0 1 14 36 while (n < 8) // 1 1 15 50 60 { // 2 16 65 110 84 n++; // 3 81 175 194 108 a += b; // 4 256 369 302 132 b += c; // 5 625 671 434 156 c += d; // 6 1296 1105 590 180 d += 24; // 7 2401 1695 770 204 Console.WriteLine("// {0,2}{1,5}{2,5}{3,4}{4,4}", n, a, b, c, d); // 8 4096 2465 974 228 } Console.ReadLine(); }
The initialisation value's: "a = 0, b = 1, c = 14, d = 36, 24" : A019538
It's getting a bit off topic, hence another topic: Power Addition Only.
Above leads to cro12 / CR12, "Hacker's Delight" hardware algorithms, and
a new "cro12", while writing ... this. It's ~1 ns faster than the one used.
 
    private static uint cro12new(uint x)     //   |------|----|
    {                                        //   |    x | ns |
        uint y = 0, a = 0, b = 1, c = 0;     //   |------|----|
        while (a < x)                        //   |    0 |  4 |
        {                                    //   |    1 |  4 |
            y++;                             //   |    7 |  5 |
            b += c;                          //   |   63 |  7 |
            a += b;                          //   |  255 | 10 |
            c += 6;                          //   | 1000 | 13 |
        }                                    //   | 1023 | 15 |
        if (a != x) y--;                     //   | 4095 | 24 |
        return y;                            //   |------|----|
    }
                                       

    |----------------------------------|
    | Times in ns (Athlon X4, XP, 2GB) |
    |----------|---------|-------------|
    |          |         | output type |
    | function |  input  |------|------|
    |          |         | uint | Xint |
    |----------|---------|------|------|
    |   cro12  |  < 4096 | <=25 |  <60 |  
    |   cro32  |  <= ~0u |  ~25 |  ~80 |  
    |   cro64  | <= ~0uL | ~330 | ~360 |
    |          |         |      |      |
    |    CR12  |  < 4096 | <=35 | <130 |
    |    CR32  |  <= ~0u |  ~35 | ~135 |
    |    CR64  | <= ~0uL | ~340 | ~460 | 
    |----------------------------------|
 
  
Abbreviations: 
  ref  reference
   CR  Cube Root & Remainder CR(9)={2,1} 
  CRO  Cube Root Only       CRO(9)= 2
  RND  Random number
    ~  bitwise not operator, or depending 
       on context, approximately
  ~0u  uint.MaxValue
 ~0uL  ulong.MaxValue
   ns  nanosecond  (10^-9 s)
   us  microsecond (10^-6 s)
   ms  millisecond (10^-3 s)
  
Times:  
    |------------------------------------------------------------------------|
    |                             Athlon X4, XP, 2GB                         |
    |--------|--------|--------||----------|---------||------------|---------|               
    |        |   ns   |   ns   ||          |   us    ||            |   ms    |
    |      X | CRO(X) |  CR(X) || bits RND | CR(RND) ||  bits RND  | CR(RND) |
    |--------|--------|--------||----------|---------||------------|---------|
    |      0 |    35  |    97  ||      65  |      4  ||    100.000 |     13  | 
    |      1 |    36  |    98  ||     100  |      8  ||    200.000 |     31  | 
    |    100 |    42  |   103  ||     200  |     14  ||    500.000 |     92  | 
    |   1000 |    47  |   112  ||     500  |     27  ||  1.000.000 |    237  | 
    |   4095 |    59  |   124  ||    1000  |     38  ||  2.000.000 |    620  | 
    |   4096 |    66  |   129  ||    2000  |     59  ||  5.000.000 |   2330  | 
    |  ~0u   |    83  |   142  ||    5000  |    134  || 10.000.000 |   6250  | 
    |  ~0u+1 |   329  |   395  ||   10000  |    341  || 20.000.000 |  16500  | 
    | ~0uL   |   382  |   474  ||   20000  |   1060  ||            |         |
    | ~0uL+1 |  3870  |  3820  ||   50000  |   4360  ||            |         |
    |        |        |        ||  100000  |  12400  ||            |         |
    |--------|--------|--------||----------|---------||------------|---------|

Refs: Hacker's Delight   
      Henry S. Warren, Jr.   
      ISBN 0-201-91465-4   
      8th Printing February 2008   
      http://www.hackersdelight.org   
      (11-2 Integer Cube Root)
 
      Modern Computer Arithmetic    
      Richard Brent and Paul Zimmermann   
      version 0.1, October 2006   
      http://www.loria.fr/~zimmerma/mca/mca-0.1.pdf
      (1.5.2 k-th Root (Cube Root))   
 
*/
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Xint = System.Numerics.BigInteger;

class Cube_Root
{
    private static Xint[] CR(Xint D)
    {
        if (D <= ~0uL) return D < 4096 ? CR12((uint)D) : D <= ~0u ? CR32((uint)D) : CR64((ulong)D);
        int n = bL(D) / 6, m = 2 * n;
        Xint BA = D & ((Xint.One << m) - 1);                                  //  |-------D------|
        D >>= m;                                                              //  |              |
        Xint C = D & ((Xint.One << n) - 1);                                   //  |-D-|-C-|--BA--|
        D >>= n;
        Xint[] R = CR(D);                                                     //  R[0] = CubeRoot
        Xint R0R0 = SQ(R[0]);
        Xint[] Q = DQR(C + (R[1] << n), 3 * R0R0);                            //  Q[0] = Quotient
        Xint Q0Q0 = SQ(Q[0]);
        R[1] = BA + (Q[1] << m) - MTP(Q0Q0, Q[0] + ((3 * R[0]) << n));
        if (R[1] < 0)
        {
            R0R0 <<= m;
            R0R0 += Q0Q0 + (MTP(Q[0], R[0]) << (n + 1));
            R[0] <<= n;
            R[0] += Q[0];
            R[1] += 1 + 3 * (R0R0 - R[0]);
            R[0] -= 1;
            while (R[1] < 0)
            {
                R0R0 -= 1 + 2 * R[0];
                R[1] += 1 + 3 * (R0R0 - R[0]);
                R[0] -= 1;
            }
        }
        else
        {
            R[0] <<= n;
            R[0] += Q[0];
        }
        return R;
    }
    private static Xint[] CR12(uint x)
    {
        uint y = 0, z = 0, r = 0, s = 0;
        while (r < x)
        {
            s = r;
            z += y * 6;
            r += z + 1;
            y += 1;
        }
        return r == x ?
            new Xint[] { y, 0 } :          // no need to use s, replace "x - s" by "x - (r + z + 1)",
            new Xint[] { y - 1, x - s };   // but it's much slower, why?
    }
    private static Xint[] CR32(uint x)
    {
        uint y = 0, z = 0, b = 0;
        int s = 30;
        while (s >= 0)
        {
            y *= 2;
            z *= 4;
            b = 3 * y + 3 * z + 1 << s;
            s -= 3;
            if (x >= b)
            {
                x -= b;
                z += 2 * y + 1;
                y += 1;
            }
        }
        return new Xint[] { y, x };
    }
    private static Xint[] CR64(ulong x)
    {
        uint y = 0;
        ulong z = 0, b = 0, bs = 0;
        int s = 63;
        while (s >= 0)
        {
            y *= 2;
            z *= 4;
            b = 3 * y + 3 * z + 1;
            bs = b << s;
            if (x >= bs && b == bs >> s)
            {
                x -= bs;
                z += 2 * y + 1;
                y += 1;
            }
            s -= 3;
        }
        return new Xint[] { y, x };
    }
    private static Xint CRO(Xint X)
    {
        if (X <= ~0ul) return X < 4096 ? cro12((uint)X) : X <= ~0u ? cro32((uint)X) : cro64((ulong)X);
        Xint[] R = CR(X);
        return R[0];
    }
    private static uint cro12(uint x)
    {
        uint y = 0, z = 0, r = 0;
        while (r < x)
        {
            z += y * 6;
            r += z + 1;
            y += 1;                    //  y > 1625 causes overflow of r
        }
        return r == x ? y : y - 1;
    }
    private static uint cro32(uint x)
    {
        uint y = 0, z = 0, b = 0;
        int s = 30;
        while (s >= 0)
        {
            y *= 2;
            z *= 4;
            b = 3 * y + 3 * z + 1 << s;
            s -= 3;
            if (x >= b)
            {
                x -= b;
                z += 2 * y + 1;
                y += 1;
            }
        }
        return y;
    }
    private static uint cro64(ulong x)
    {
        uint y = 0;
        ulong z = 0, b = 0, bs = 0;
        int s = 63;
        while (s >= 0)
        {
            y *= 2;
            z *= 4;
            b = 3 * y + 3 * z + 1;
            bs = b << s;
            if (x >= bs && b == bs >> s)
            {
                x -= bs;
                z += 2 * y + 1;
                y += 1;
            }
            s -= 3;
        }
        return y;
    }

    // Use the faster versions from: Nth Root ////////////////////////////////////////////////////
    private static Xint SQ(Xint U) { return U * U; }                                            //
    private static Xint MTP(Xint U, Xint V) { return U * V; }                                   //
    private static Xint[] DQR(Xint U, Xint V)                                                   //
    {                                                                                           //
        Xint[] QR = new Xint[2];                                                                //
        QR[0] = Xint.DivRem(U, V, out QR[1]);                                                   //
        return QR;                                                                              //
    }                                                                                           //
    // Use the faster versions: http://www.bigintegers.blogspot.com/2013/07/nth-root-power.html //

    private static Stopwatch sw = new Stopwatch();
    static void Main()
    {
        for (int i = 1; i < 100; i++)
        {
            Xint X = RND(8 * i);
            //X = Xint.Pow(X, 3);
            //X--;
            //X++;
            sw.Start(); Xint[] R = CR(X); sw.Stop();
            if (Xint.Pow(R[0], 3) > X) Console.WriteLine("WRONG1");
            if (Xint.Pow(R[0] + 1, 3) <= X) Console.WriteLine("WRONG2");
            if (Xint.Pow(R[0], 3) + R[1] != X) Console.WriteLine("WRONG3");
        }
        Console.WriteLine(sw.ElapsedMilliseconds + " ms");
        Console.ReadLine();
    }

    private static int bL(Xint U)
    {
        byte[] bytes = (U.Sign * U).ToByteArray();
        int i = bytes.Length - 1;
        return i * 8 | bitLengthMostSignificantByte(bytes[i]);
    }
    private static int bitLengthMostSignificantByte(byte b)
    {
        return b < 08 ? b < 02 ? b < 01 ? 0 : 1 :
                                 b < 04 ? 2 : 3 :
                        b < 32 ? b < 16 ? 4 : 5 :
                                 b < 64 ? 6 : 7;
    }
    private static int seed;
    private static Xint RND(int n)
    {
        if (n < 2) return n;
        if (seed == int.MaxValue) seed = 0; else seed++;
        Random rand = new Random(seed);
        byte[] bytes = new byte[(n + 15) >> 3];
        rand.NextBytes(bytes);
        int i = bytes.Length - 1;
        bytes[i] = 0;
        n = (i << 3) - n;
        i--;
        bytes[i] >>= n;
        bytes[i] |= (byte)(128 >> n);
        return new Xint(bytes);
    }
    private static Xint[] CRold(Xint D)
    {
        if (D < 64)
        {
            int d = (int)D;
            if (d >= 27) return new Xint[] { 3, d - 27 };
            if (d >= 08) return new Xint[] { 2, d - 08 };
            if (d >= 01) return new Xint[] { 1, d - 01 };
            return new Xint[] { 0, 0 };
        }
        int n = bL(D) / 6;
        Xint Mask = (Xint.One << n) - 1;
        Xint A = D & Mask; D >>= n;
        Xint B = D & Mask; D >>= n;
        Xint C = D & Mask; D >>= n;
        Xint[] R = CRold(D);                                  // R[0] = CubeRoot, R[1] = Remainder
        Xint R0R0 = SQ(R[0]);
        Xint[] Q = DQR(C + (R[1] << n), 3 * R0R0);            // Q[0] = Quotient, Q[1] = Remainder
        Xint Q0Q0 = SQ(Q[0]);
        R[1] = A + ((B + (Q[1] << n)) << n) - MTP(Q0Q0, Q[0] + ((3 * R[0]) << n));
        if (R[1] < 0)
        {
            R0R0 <<= n * 2;
            R0R0 += Q0Q0 + (MTP(Q[0], R[0]) << (n + 1));
            R[0] <<= n;
            R[0] += Q[0];
            R[1] += 1 + 3 * (R0R0 - R[0]);
            R[0] -= 1;
            while (R[1] < 0)
            {
                R0R0 -= 1 + 2 * R[0];
                R[1] += 1 + 3 * (R0R0 - R[0]);
                R[0] -= 1;
            }
        }
        else
        {
            R[0] <<= n;
            R[0] += Q[0];
        }
        return R;
    }
}

2013/08/08

Power Addition Only

/*


Finding Powers using only additions?

    Multiplication is repeated addition: 
    3*2 = 3+3 or 2+2+2
    
    Powering is repeated multiplication: 
    3^3 = 3*3*3
        = 3*(3+3+3)
        = (3+3+3)+(3+3+3)+(3+3+3)
        
But here's something different. It came along while I wrote Cube Root code,
it went off topic, thus another topic, this one.

      x        x^4
      0            0       1     14     36   24   0
      1            1     15     50     60   24   0
      2          16     65   110     84   24   0
      3          81   175   194   108   24   0
      4        256   369   302   132   24   0


Above each black number is the sum of the numbers above and right above it,
because: "see Cube Root".
     _                     _       _               _ 
    |    0   1  14  36  24  |     |  A  B  C  D  E  |      F=A+B , G=B+C , ...
    |    1  15  50  60  24  |     |  F  G  H  I  .  |      
    |   16  65 110  84  24  |  =  |  J  K  L  .  .  |      
    |   81 175 194 108  24  |     |  M  N  .  .  .  |      
    |_ 256 369 302 132  24 _|     |_ P  .  .  .  . _|      
     
Each row depends on the row above it, depends on the first (magic?) row.
The other way round:     How to get the first row: " 0,1,14,36,24,0 ",
                       from the first colum (x^4): " 0,1,16,81,256  ".

    C=G-B G=J-F C=1J-2F+1A 
          B=F-A
          
    D=H-C H=K-G D=1K-2G+1B K=M-J D=M-J-2J+2F+F-A D=1M-3J+3F-1A 
          C=G-B            G=J-F   
                           B=F-A 
                           
    E=I-D I=L-H E=1L-2H+1C L=N-K E=N-K-2K+2G+G-B E=N-3K+3G-B N=P-M E=P-M-3M+3J+3J-3F-F+A E=1P-4M+6J-4F+1A 
          D=H-C            H=K-G                             K=M-J
                           C=G-B                             G=J-F
                                                             B=F-A
    A=      1.0      A=1.0^4=0                                                         A=           +2A+1.0
    B=    1F-1A      B=1.1^4-1.0^4                                                     B=         1F-2A+1A
    C=   1J-2F+1A    C=1.2^4-2.1^4+1.0^4             C=1J-2F+1A+1B-(1F-1A)             C=      1J-3F+2A+1B
    D=  1M-3J+3F-1A  D=1.3^4-3.2^4+3.1^4-1.0^4       D=1M-3J+3F-1A+1C-(1J-2F+1A)       D=   1M-4J+5F-2A+1C
    E=1P-4M+6J-4F+1A E=1.4^4-4.3^4+6.2^4-4.1^4+1.0^4 E=1P-4M+6J-4F+1A+1D-(1M-3J+3F-1A) E=1P-5M+9J-7F+2A+1D
     
    Pascal's Triangle

The first row is part of A131689 (~A019538): Triangle of numbers: T(n,k)=k!*Stirling2(n,k)
Next step: Use Pascal's triangle, use Binomial Coefficients ( C(.,.) ).

                                                 C(1,1).1^1 =  1   \
                                                                    \
                                                 C(1,1).1^2 =  1     \
                                    C(2,2).2^2 - C(2,1).1^2 =  2      \ 
                                                                       \
                                                 C(1,1).1^3 =  1        \
                                    C(2,2).2^3 - C(2,1).1^3 =  6         A019538
                       C(3,3).3^3 - C(3,2).2^3 + C(3,1).1^3 =  6        /
                                                                       /
                                                 C(1,1).1^4 =  1      /
                                    C(2,2).2^4 - C(2,1).1^4 = 14     /
                       C(3,3).3^4 - C(3,2).2^4 + C(3,1).1^4 = 36    /
          C(4,4).4^4 - C(4,3).3^4 + C(4,2).2^4 - C(4,1).1^4 = 24   /

*/
using System;
class Power_Addition_Only
{
    private static uint T(uint n, uint k)   // OEIS: A019538 =============================>>  //  1
    {                                                                                         //  1
        return F(k) * S2(n, k);                                                               //  2
    }                                                                                         //  1
    private static uint S2(uint n, uint k)  // Stirling numbers                               //  6
    {                                       //    2nd kind                                    //  6
        if (n == k) return 1;                                                                 //  1
        if (n == 0 || k == 0) return 0;                                                       //  14
        n--;                                                                                  //  36
        return k * S2(n, k--) + S2(n, k);                                                     //  24
    }                                                                                         //  1
    private static uint F(uint k)           // Factorial                                      //  30
    {                                                                                         //  150
        uint f = 1;                                                                           //  240
        while (k > 1) f *= k--;                                                               //  120
        return f;                                                                             //  1
    }                                                                                         //  62
                                                                                              //  540
    static void Main()                                                                        //  1560
    {                                                                                         //  1800
        Console.WriteLine();                                                                  //  720
        powers(4, 4);                                                                         //  1
        powers(3, 1);                                                                         //  126
        powers(21, 6);                                                                        //  1806
        powers(15, 7);                                                                        //  8400
        powers(9, 9);                                                                         //  16800
        Console.ReadLine();                                                                   //  15120
    }                                                                                         //  5040
    private static void powers(uint x, uint p) // up to x^p                                   //  1
    {                                                                                         //  254
        Console.WindowWidth = 105;                                                            //  5796
        Console.WindowHeight = Console.LargestWindowHeight - 10;                              //  40824
        Console.WriteLine("       x^" + p);                                                   //  126000
        Console.WriteLine();                                                                  //  191520
        int i = 0, j;                                                                         //  141120
        uint[] v = Init(p);                                                                   //  40320
        int vL = v.Length - 1;                                                                //  1
        for (j = 0; j <= vL; j++) Console.Write("{0,10}", v[j]);                              //  510
        Console.WriteLine();                                                                  //  18150
        for (; i < x; i++)                                                                    //  186480
        {                                                                                     //  834120
            for (j = 0; j < vL; ) v[j++] += v[j];                                             //  1905120
            for (j = 0; j <= vL; j++) Console.Write("{0,10}", v[j]);                          //  2328480
            Console.WriteLine();                                                              //  1451520
        }                                                                                     //  362880
        Console.WriteLine();                                                                  //  1
        Console.WriteLine();                                                                  //  1022
    }                                                                                         //  55980
    private static uint[] Init(uint i)      // get Initial value's                            //  818520
    {                                                                                         //  5103000
        uint[] v = new uint[i + 1];                                                           //  16435440
        v[0] = 0;                                                                             //  29635200
        for (uint j = 1; j <= i; j++)                                                         //  30240000
            v[j] = T(i, j);                                                                   //  16329600
        return v;                                                                             //  3628800
    }                                                                                         //  1      
}