Open In App

Getting binomial coefficient of a number in Julia – binomial() Method

Improve
Improve
Like Article
Like
Save
Share
Report
The binomial() is an inbuilt function in julia which is used to return the binomial coefficient $\binom{n}{k}$ which is the coefficient of the kth term in the polynomial expansion of $(1+x)^n$. Its formula is –

    \[\binom{n}{k} = \frac{n!}{k! (n-k)!}\]

, where $n!$ is the factorial of n. If n is negative, then it is defined in terms of the identity

    \[\binom{n}{k} = (-1)^k \binom{k-n-1}{k}\]

.
Syntax: binomial(n::Integer, k::Integer) Parameters:
  • n: Specified number
  • k: Specified number
Returns: It returns the binomial coefficient $\binom{n}{k}$.
Example 1:
# Julia program to illustrate 
# the use of binomial() method
  
# Getting the binomial coefficient
println(binomial(6, 4))
println(factorial(6) ÷ (factorial(6-4) * factorial(4)))

                    
Output:
15
15
Example 2:
# Julia program to illustrate 
# the use of binomial() method
  
# Getting the binomial coefficient
println(binomial(6, 3))
println(binomial(-6, 3))
println(binomial(-6, -2))
println(binomial(5, 2))

                    
Output:
20
-56
0
10


Last Updated : 21 Apr, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads