Advertisement
isabelgh

RecursivePascal

Sep 9th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3.  
  4. public class RecursivePascal extends ErrorPascal implements Pascal {
  5.  
  6.     public int binom(int n, int k)
  7.     {
  8.         termsBinom(n, k);
  9.        
  10.         //base-case
  11.         if(k == 0 || k == n)
  12.             return 1;
  13.  
  14.         else
  15.             //identity (n-1 k-1) + (n-1 k)
  16.             return binom(n-1, k-1) + binom(n-1, k);    
  17.     }
  18.  
  19.     public void printPascal(int n)
  20.     {
  21.         termsPascal(n);
  22.        
  23.         //right-side up, recursive call until n equals 0 then it prints out "1" and jumps to for loop
  24.         if(b == true){     
  25.             if(n == 0){
  26.                 System.out.println("1");
  27.             }
  28.  
  29.             else { 
  30.                 //recursive call
  31.                 printPascal(n-1);              
  32.            
  33.             //prints out the values of each level
  34.             for (int i = 0; i <= n; i++) {               
  35.                 System.out.print(binom(n, i) + " ");                               
  36.             }          
  37.             System.out.println();  
  38.             }
  39.         }
  40.        
  41.         //upside-down, prints out the elements of the levels starting from n first
  42.         if(b == false)         
  43.         {      
  44.             if(n == 0){
  45.  
  46.                 System.out.println("1");
  47.             }            
  48.             else {
  49.                 //prints out the elements of each level in triangle
  50.                 for (int i = 0; i <= n; i++) {               
  51.                     System.out.print(binom(n, i) + " ");                               
  52.                 }
  53.                 System.out.println();
  54.                 //recursive call
  55.                 printPascal(n-1);
  56.  
  57.             }
  58.         }
  59.     }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement