Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class RecursivePascal extends ErrorPascal implements Pascal {
- public int binom(int n, int k)
- {
- termsBinom(n, k);
- //base-case
- if(k == 0 || k == n)
- return 1;
- else
- //identity (n-1 k-1) + (n-1 k)
- return binom(n-1, k-1) + binom(n-1, k);
- }
- public void printPascal(int n)
- {
- termsPascal(n);
- //right-side up, recursive call until n equals 0 then it prints out "1" and jumps to for loop
- if(b == true){
- if(n == 0){
- System.out.println("1");
- }
- else {
- //recursive call
- printPascal(n-1);
- //prints out the values of each level
- for (int i = 0; i <= n; i++) {
- System.out.print(binom(n, i) + " ");
- }
- System.out.println();
- }
- }
- //upside-down, prints out the elements of the levels starting from n first
- if(b == false)
- {
- if(n == 0){
- System.out.println("1");
- }
- else {
- //prints out the elements of each level in triangle
- for (int i = 0; i <= n; i++) {
- System.out.print(binom(n, i) + " ");
- }
- System.out.println();
- //recursive call
- printPascal(n-1);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement