How to resolve the algorithm Evaluate binomial coefficients step by step in the ALGOL W programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Evaluate binomial coefficients step by step in the ALGOL W programming language
Table of Contents
Problem Statement
This programming task, is to calculate ANY binomial coefficient. However, it has to be able to output
(
5 3
)
{\displaystyle {\binom {5}{3}}}
, which is 10. This formula is recommended:
See Also:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Evaluate binomial coefficients step by step in the ALGOL W programming language
Source code in the algol programming language
begin
% calculates n!/k! %
integer procedure factorialOverFactorial( integer value n, k ) ;
if k > n then 0
else if k = n then 1
else % k < n % begin
integer f;
f := 1;
for i := k + 1 until n do f := f * i;
f
end factorialOverFactorial ;
% calculates n! %
integer procedure factorial( integer value n ) ;
begin
integer f;
f := 1;
for i := 2 until n do f := f * i;
f
end factorial ;
% calculates the binomial coefficient of (n k) %
% uses the factorialOverFactorial procedure for a slight optimisation %
integer procedure binomialCoefficient( integer value n, k ) ;
if ( n - k ) > k
then factorialOverFactorial( n, n - k ) div factorial( k )
else factorialOverFactorial( n, k ) div factorial( n - k );
% display the binomial coefficient of (5 3) %
write( binomialCoefficient( 5, 3 ) )
end.
You may also check:How to resolve the algorithm Anonymous recursion step by step in the Scheme programming language
You may also check:How to resolve the algorithm Comments step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Arithmetic-geometric mean step by step in the Haskell programming language
You may also check:How to resolve the algorithm Nested function step by step in the REXX programming language
You may also check:How to resolve the algorithm User input/Text step by step in the PicoLisp programming language