Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PascalsTriangle
- {
- /**
- * @param args
- */
- public static void main( String[] args )
- {
- drawPascalsTriangleFactorial(11);
- //generatePascalsTriangleArrays(100);
- }
- public static long getFactorialOf(int n)
- {
- long startat = 1;
- if(n < 0)
- {
- throw new IllegalArgumentException("N must be a positive number: Your input: " + n);
- }
- for(int num = 2; num <= n; num++)
- {
- startat *= num;
- }
- return startat;
- }
- public static void drawPascalsTriangleFactorial(int maxrows)
- {
- for(int r = 0; r <= maxrows; r++)
- {
- for(int c = 0; c <= (maxrows - r - 1); c++)
- {
- System.out.printf( "%2s"," " );
- }
- for(int c = 0; c <= r; c++)
- {
- System.out.printf( "%4d", ( getFactorialOf(r) / (getFactorialOf(c) * getFactorialOf(r-c) ) ) );
- }
- System.out.println();
- }
- }
- public static void generatePascalsTriangleArrays(int depth)
- {
- long current[] = new long[1];
- current[0] = 1;
- //System.out.println( topnum[0] );
- for(int i = 0; i <= depth; i++)
- {
- //System.out.print( "Row(" + i +") ");
- for(int c = 0; c <= (depth - i - 1); c++)
- {
- System.out.printf( "%2s"," " );
- }
- for(int i2 = 0; i2 < current.length; i2++)
- {
- System.out.printf("%4d ", current[i2]);
- }
- System.out.println();
- current = generateNextRow(current);
- }
- }
- public static long[] generateNextRow(long last[])
- {
- long current[] = new long[(last.length + 1)];
- for(int col = 0; col < current.length; col++)
- {
- current[col] = 1;
- }
- int lColS1 = 0;
- int lColS2 = 0;//lColS1 + 1
- //System.out.println("Last Array Target Cols: " + lColS1 + " " + lColS2 );
- for(int col = 0; col < current.length; col++)
- {
- if(lColS1 < last.length - 1 && col > 0)
- {
- lColS1++;
- }
- if(lColS2 < last.length - 1 && col > 0)
- {
- lColS2 = lColS1 - 1;
- }
- //System.out.println("Last Array Target Cols: " + lColS1 + " " + lColS2 );
- /*if( (col == 0 && col == current.length - 1) )
- {
- current[col] = 1;
- System.out.println( "Is First Or Last" );
- }*/
- if( col > 0 && col < current.length - 1)
- {
- current[col] = (last[lColS1] + last[lColS2]);
- //System.out.println( "Is Not First Or Last " + "SlotA " + last[lColS1] + " + " + last[lColS2] + " = " + (last[lColS1] + last[lColS2]) );
- }
- }
- return current;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement