Evaluate binomial coefficients: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: whitespace/format)
Line 491: Line 491:
<lang j> 3 ! 5
<lang j> 3 ! 5
10</lang>
10</lang>

=={{header|Java}}==
<lang java>public class Binom {

static long combinations(int n, int k) {
long coeff = 1;
for (int i = n - k + 1; i <= n; i++) {
coeff *= i;
}
for (int i = 1; i <= k; i++) {
coeff /= i;
}
return coeff;
}

public static void main(String[] args){
System.out.println(combinations(5, 3));
}
}</lang>
Output:
<pre>10.0</pre>
{{trans|Python}}
<lang java>public class Binom {
public static double binomCoeff(double n, double k) {
double result = 1;
for (int i = 1; i < k + 1; i++) {
result *= (n - i + 1) / i;
}
return result;
}

public static void main(String[] args) {
System.out.println(binomCoeff(5, 3));
}
}
</lang>
Output:
<pre>10.0</pre>


=={{header|JavaScript}}==
=={{header|JavaScript}}==