Permutations Complete the following function using a combination of branching and a for-loop to compute the number of permutations of n given k. (1) If k is greater than n, then the function is...


The code in is Matlab. This is what I have so far. I"m not sure what to do.



function [P] = permutations(n, k)

% calculate the number of permutations P

P = 1;

if k > n

P = NaN;

else

for i = 1:n

P = P*i;

end

end


Permutations<br>Complete the following function using a combination of branching and a for-loop to compute the number of permutations of n given k. (1) If<br>k is greater than n, then the function is undefined and the number of permutations is NaN. (2) Otherwise, compute the function using the<br>following formula. You may not use the built-in function factorial or perms in the function.<br>n!<br>P(n, k) :<br>(n – k)!<br>It is very inefficient to calculate the factorial of both the numerator and the denominator. Instead, you should use a for-loop to calculate the<br>product of the terms in the numerator that are not cancelled by terms in the denominator. Consider the following example:<br>10 -9· 8·7·6·5·4·3·2·1<br>P(10,6) =<br>= 10· 9· 8· 7·6·5<br>4·3·2·1<br>

Extracted text: Permutations Complete the following function using a combination of branching and a for-loop to compute the number of permutations of n given k. (1) If k is greater than n, then the function is undefined and the number of permutations is NaN. (2) Otherwise, compute the function using the following formula. You may not use the built-in function factorial or perms in the function. n! P(n, k) : (n – k)! It is very inefficient to calculate the factorial of both the numerator and the denominator. Instead, you should use a for-loop to calculate the product of the terms in the numerator that are not cancelled by terms in the denominator. Consider the following example: 10 -9· 8·7·6·5·4·3·2·1 P(10,6) = = 10· 9· 8· 7·6·5 4·3·2·1

Jun 08, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here