Pascal Triangle


What is the purpose?

A triangular array of numbers in which those at the ends of the rows are 1 and each of the others is the sum of the nearest two numbers in the row above (the apex, 1, being at the top).
The rows of Pascal's triangle are conventionally enumerated starting with row n = 0 at the top (the 0th row). The entries in each row are numbered from the left beginning with k = 0 and are usually staggered relative to the numbers in the adjacent rows. The triangle may be constructed in the following manner: In row 0 (the topmost row), there is a unique nonzero entry 1. Each entry of each subsequent row is constructed by adding the number above and to the left with the number above and to the right, treating blank entries as 0. For example, the initial number in the first (or any other) row is 1 (the sum of 0 and 1), whereas the numbers 1 and 3 in the third row are added to produce the number 4 in the fourth row.



How to do?
package com.demo;
public class PascalTriangle {
public static void main(String[] args) {
PascalTriangle pt = new PascalTriangle();
int n = 6;
System.out.println("Simple procedure Pascal Triangle Display");
pt.pascaleTriangleSimple(n);
System.out.println();
System.out.println("Binomial procedure Pascal Triangle Display");
pt.pascaleTriangleBinomial(n);
System.out.println();
System.out.println("Recursive procedure Pascal Triangle Display");
pt.pascaleTriangleRecursive(n);
}
private void pascaleTriangleSimple(int n) {
int formatSpace = 4;
for (int i = 0; i < n; i++) {
int number = 1;
System.out.format("%" + (n - i) * formatSpace + "s", "");
for (int j = 0; j <= i; j++) {
System.out.format("%" + formatSpace * 2 + "d", number);
number = number * (i - j) / (j + 1);
}
System.out.println();
}
}
public void pascaleTriangleBinomial(int n) {
int formatSpace = 4;
for (int i = 0; i < n; i++) {
System.out.format("%" + (n - i) * formatSpace + "s", "");
for (int j = 0; j <= i; j++) {
System.out.format("%" + formatSpace * 2 + "d", nCk(i, j));
}
System.out.println();
}
}
/**
 * nCk =!n/!n*!n-k
 *
 * @param n
 * @param k
 * @return
 */
public long nCk(int n, int k) {
long numerator = fact(n);
long denominator = fact(k) * fact(n - k);
long result = (long) (numerator / denominator);
return result;
}
public long fact(long num) {
if (num <= 1)
return 1;
else
return num * fact(num - 1);
}
public void pascaleTriangleRecursive(int n) {
int formatSpace = 4;
for (int i = 0; i < n; i++) {
System.out.format("%" + (n - i) * formatSpace + "s", "");
for (int j = 0; j <= i; j++) {
System.out.format("%" + formatSpace * 2 + "d",
pascalRecursive(i, j));
}
System.out.println();
}
}
private int pascalRecursive(int i, int j) {
if (j == 0) {
return 1;
} else if (j == i) {
return 1;
} else {
return pascalRecursive(i - 1, j - 1) + pascalRecursive(i - 1, j);
}
}
}


Output:

Simple procedure Pascal Triangle Display
                               1
                           1       1
                       1       2       1
                   1       3       3       1
               1       4       6       4       1
           1       5      10      10       5       1

Binomial procedure Pascal Triangle Display
                               1
                           1       1
                       1       2       1
                   1       3       3       1
               1       4       6       4       1
           1       5      10      10       5       1

Recursive procedure Pascal Triangle Display
                               1
                           1       1
                       1       2       1
                   1       3       3       1
               1       4       6       4       1
           1       5      10      10       5       1

Example:

How it will generating?




Previous
Next Post »