Run Code  | API  | Code Wall  | Misc  | Feedback  | Login  | Theme  | Privacy  | Patreon 

Catalan number optimised

//'main' method must be in a class 'Rextester'.
//openjdk version '11.0.5' 

import java.util.*;
import java.lang.*;

class Rextester
{  
    public static int combination(int n,int r){
        int res = 1;
        if(r > n-r){
            r = n-r;
        }
        
        for(int k = 0;k<r;k++){
            res *= (n-k);
            res /= k+1;
        }
        return res;
    }
    public static int catalina(int num){
        return combination(2*num,num)/(num+1);
    }
    public static void main(String args[])
    {
        System.out.println(catalina(8));
    }
}
 run  | edit  | history  | help 0