Hunter
Hunter

Reputation: 25

How to program an equation with factorials

I need help programing this equation using java

c(n)=((2n-3)!) / ( (n!)*((n+1)!+5) )

This is what I have and it gives me 0

Any help would be appreciated

Upvotes: 0

Views: 89

Answers (2)

laune
laune

Reputation: 31300

This is less likely to exceed some integer/long max value (and uses requires less cycles):

public static long d(int n){
    long top = 1;
    long bom = n + 1;
    for(int q = 1; q <= n; q++){
        top *= n + q; 
        bom *= q;
    }
    return top/bom;
}

Upvotes: 1

David Eisenstat
David Eisenstat

Reputation: 65506

top=2*q*top;

should be

top=2*q*(2*q - 1)*top;

as otherwise top omits the odd factors (but I agree with the comment that you should factor out a factorial function).

Upvotes: 0

Related Questions