Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MatLab: Your help is Needed A function which is related to the factorial functio

ID: 3768382 • Letter: M

Question

MatLab: Your help is Needed

A function which is related to the factorial function, n!, is the double factorial function n!!. If n is an odd positive integer, the double factorial of that integer is the product of all the odd positive integers from 1 up to n. If n is an even positive integer, the double factorial of that integer is the product of all even positive integers from 2 up to n. For reasons of consistency in other areas of mathematics, 0!! = 1!! = 1. For example: 7!! = 7 5 3 1 = 105 and 8!! = 8 6 4 2 = 384. Write a Mat-lab function called thedfact.m which calculates the double factorial of any positive integer. Your program must be recursive; your function must call itself and it may not use any kind of loop. If the input is not a positive integer, your function should return the value -1. You may not use ANY built-in Matlab functions in your code. Program specifications and some sample function calls are listed below.

Explanation / Answer

function dfact = thedfact(n)
if n%1!= 0
dfact=-1;
end
  
if n==0 || n==1
dfact=1;
elseif n<0
dfact=-1;
else
if n>1
if n%2==0
dfact = n * thedfact(n-2);
elseif n%2==1
dfact = n * thedfact(n-2);
else
dfact= 1;
end
end
end
end