Advertisement
StormWingDelta

Pascals Triangle two different tries

Feb 4th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. public class PascalsTriangle
  2. {
  3.    
  4.     /**
  5.      * @param args
  6.      */
  7.     public static void main( String[] args )
  8.     {
  9.        
  10.         drawPascalsTriangleFactorial(11);
  11.         //generatePascalsTriangleArrays(100);
  12.     }
  13.    
  14.     public static long getFactorialOf(int n)
  15.     {
  16.         long startat = 1;
  17.         if(n < 0)
  18.         {
  19.             throw new IllegalArgumentException("N must be a positive number: Your input: " + n);
  20.         }
  21.         for(int num = 2; num <= n; num++)
  22.         {
  23.             startat *= num;
  24.         }
  25.         return startat;
  26.     }
  27.    
  28.     public static void drawPascalsTriangleFactorial(int maxrows)
  29.     {
  30.         for(int r = 0; r <= maxrows; r++)
  31.         {
  32.            
  33.             for(int c = 0; c <= (maxrows - r - 1); c++)
  34.             {
  35.                 System.out.printf( "%2s"," " );
  36.             }
  37.            
  38.             for(int c = 0; c <= r; c++)
  39.             {
  40.                 System.out.printf( "%4d", ( getFactorialOf(r) / (getFactorialOf(c) * getFactorialOf(r-c) ) ) );
  41.                
  42.             }
  43.             System.out.println();
  44.         }
  45.        
  46.     }
  47.    
  48.     public static void generatePascalsTriangleArrays(int depth)
  49.     {
  50.        
  51.         long current[] = new long[1];
  52.         current[0] = 1;
  53.         //System.out.println( topnum[0] );
  54.         for(int i = 0; i <= depth; i++)
  55.         {
  56.             //System.out.print( "Row(" + i +") ");
  57.             for(int c = 0; c <= (depth - i - 1); c++)
  58.             {
  59.                 System.out.printf( "%2s"," " );
  60.             }
  61.             for(int i2 = 0; i2 < current.length; i2++)
  62.             {
  63.                 System.out.printf("%4d ", current[i2]);
  64.             }
  65.             System.out.println();
  66.             current = generateNextRow(current);
  67.            
  68.            
  69.         }
  70.        
  71.        
  72.        
  73.     }
  74.    
  75.     public static long[] generateNextRow(long last[])
  76.     {
  77.         long current[] = new long[(last.length + 1)];
  78.         for(int col = 0; col < current.length; col++)
  79.         {
  80.             current[col] = 1;
  81.         }
  82.         int lColS1 = 0;
  83.         int lColS2 = 0;//lColS1 + 1
  84.         //System.out.println("Last Array Target Cols: " + lColS1 + " " + lColS2 );
  85.         for(int col = 0; col < current.length; col++)
  86.         {
  87.            
  88.            
  89.             if(lColS1 < last.length - 1 && col > 0)
  90.             {
  91.                 lColS1++;
  92.                
  93.             }
  94.             if(lColS2 < last.length - 1 && col > 0)
  95.             {
  96.                 lColS2 = lColS1 - 1;
  97.             }
  98.            
  99.             //System.out.println("Last Array Target Cols: " +  lColS1 + " " + lColS2 );
  100.             /*if( (col == 0 && col == current.length - 1) )
  101.             {
  102.                 current[col] = 1;
  103.                 System.out.println( "Is First Or Last" );
  104.             }*/
  105.             if( col > 0 && col < current.length - 1)
  106.             {
  107.                 current[col] = (last[lColS1] + last[lColS2]);
  108.                 //System.out.println( "Is Not First Or Last  " + "SlotA " + last[lColS1] + " + " + last[lColS2] + " = " + (last[lColS1] + last[lColS2]) );
  109.             }
  110.            
  111.            
  112.         }
  113.        
  114.        
  115.        
  116.         return current;
  117.     }
  118.    
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement