Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1. import java.util.*;
  2. /*
  3.  
  4. fibonacci method
  5. aP[2][0]
  6. aP[3][0] + aP[2][1]
  7. aP[4][0] + aP[3][1] + aP[2][2]
  8. aP[5][0] + aP[4][1] + aP[3][2]
  9. aP[6][0] + aP[5][1] + aP[4][2] + aP[3][3]
  10. aP[7][0] + aP[6][1] + aP[5][2] + aP[4][3]
  11.  
  12. */
  13.  
  14. public class PascalFibonacci{
  15.     public static void main(String[] args) {
  16.         Scanner sc = new Scanner(System.in);
  17.         long[][] arrPascal = new long[65][65];
  18.         for (int i = 0; i < arrPascal[i].length - 1; i++) {
  19.             for (int j = 0; j < arrPascal.length - 1; j++) {
  20.                 arrPascal[j][i] = choose(j,i);
  21.             }
  22.         }
  23.        
  24.         System.out.print("Input row index: ");
  25.         int rowIndex = sc.nextInt();
  26.         System.out.print("Input column index: ");
  27.         int colIndex = sc.nextInt();
  28.         // try{
  29.         //  Math.addExact();
  30.         // }
  31.         // catch Exception e{
  32.         //  choose()
  33.         // }
  34.         int total = 0;
  35.         int i = 0;
  36.         int j = rowIndex;
  37.         while (i<colIndex && j>0){
  38.             total += arrPascal[i][j];
  39.             i++;
  40.             j--;
  41.         }
  42.        
  43.         System.out.println("-----------------------");
  44.         System.out.println("Fibonacci(" + i + ")     = " + total);
  45.  
  46.         // for (int i = 0; i<colIndex+1; i++) {
  47.         //  System.out.print("arrPascal[" + (rowIndex-i) +"][" + i + "]   = ");
  48.         //  System.out.printf("%d2 arrPascal[");
  49.         // }
  50.  
  51.     }
  52.  
  53.     public static long choose(long n,long r){
  54.         long nFact = 1;
  55.         long rFact = 1;
  56.         long nrFact = 1;
  57.         for (long i = 2; i<=n; i++) {
  58.             nFact *= i;
  59.         }
  60.         for (long i = 2; i<=r; i++) {
  61.             rFact *= i;
  62.         }
  63.         for (long i = 2; i<=(n-r); i++) {
  64.             nrFact *= i;
  65.         }
  66.         long num = nFact;
  67.         long denom = rFact*nrFact;
  68.         // System.out.println(num + ", " + denom);
  69.         // Starts dying when num = 67
  70.         //if(denom == 0) System.out.println(n);
  71.         long nCr = num/denom;
  72.  
  73.  
  74.         return nCr;
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement