Write Prolog Program Computes Binomial Coefficient Q37124082

Write a Prolog program that computes binomial coefficient


Solution


program.pl:

bc(N, 0, 1) :- N >= 0.% base case 1 binomial_coefficient is 1for (N,0)
bc(N, N, 1) :- N > 0.% base case 2 binomial_coefficient is 1 for(N,N)
bc(N,K,R):-
    factorial(N,F1),% finds n!
    factorial(K,F2),% finds k!
    factorial(N-K,F3),% finds (n-k)!
    R is F1/(F2*F3).
% we know that binomial coefficient formula binomial_coefficient (nk) = n!/(k!(n-k)!
% program to find factorial of n

factorial(0,1).

factorial(N,F) :-
   N>0,
   N1 is N-1,
   factorial(N1,F1),
   F is N * F1.

Output:

shyam@shyam:-/Documents/chegg$ swipl program.pl Welcome to SWI-Prolog (threaded, 64 bits, version 7.6.4) SHI-Prolog comes wit

shyam@shyam:-/Documents/chegg$ swipl program.pl Welcome to SWI-Prolog (threaded, 64 bits, version

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.