Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- /*
- fibonacci method
- aP[2][0]
- aP[3][0] + aP[2][1]
- aP[4][0] + aP[3][1] + aP[2][2]
- aP[5][0] + aP[4][1] + aP[3][2]
- aP[6][0] + aP[5][1] + aP[4][2] + aP[3][3]
- aP[7][0] + aP[6][1] + aP[5][2] + aP[4][3]
- */
- public class PascalFibonacci{
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- long[][] arrPascal = new long[65][65];
- for (int i = 0; i < arrPascal[i].length - 1; i++) {
- for (int j = 0; j < arrPascal.length - 1; j++) {
- arrPascal[j][i] = choose(j,i);
- }
- }
- System.out.print("Input row index: ");
- int rowIndex = sc.nextInt();
- System.out.print("Input column index: ");
- int colIndex = sc.nextInt();
- // try{
- // Math.addExact();
- // }
- // catch Exception e{
- // choose()
- // }
- int total = 0;
- int i = 0;
- int j = rowIndex;
- while (i<colIndex && j>0){
- total += arrPascal[i][j];
- i++;
- j--;
- }
- System.out.println("-----------------------");
- System.out.println("Fibonacci(" + i + ") = " + total);
- // for (int i = 0; i<colIndex+1; i++) {
- // System.out.print("arrPascal[" + (rowIndex-i) +"][" + i + "] = ");
- // System.out.printf("%d2 arrPascal[");
- // }
- }
- public static long choose(long n,long r){
- long nFact = 1;
- long rFact = 1;
- long nrFact = 1;
- for (long i = 2; i<=n; i++) {
- nFact *= i;
- }
- for (long i = 2; i<=r; i++) {
- rFact *= i;
- }
- for (long i = 2; i<=(n-r); i++) {
- nrFact *= i;
- }
- long num = nFact;
- long denom = rFact*nrFact;
- // System.out.println(num + ", " + denom);
- // Starts dying when num = 67
- //if(denom == 0) System.out.println(n);
- long nCr = num/denom;
- return nCr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement