Question: Using Matlab Written in expanded form, the usual factorial function is n! = n middot (n - 1) middot (n - 2) ... 3 middot

Using Matlab

Using Matlab Written in expanded form, the usual factorial function is n!

Written in expanded form, the usual factorial function is n! = n middot (n - 1) middot (n - 2) ... 3 middot 2 middot 2 middot 1. The difference between elements in the product is always 1. It can be written in recursive form as n! = n middot (n - 1)! (e.g., 10! = 10.9!, 23! = 23 middot 22!, 4! = 4 middot 3!, etc.). The purpose of this problem is to generalize the factorial function so that the difference between elements in the product is always a given positive integer k. One possible generalization is the skip factorial function, denoted by n!_k. The skip factorial is defined recursively for positive integers n and k by the equation n!_k = n middot (n - k)!_k, where n!_k = n when n lessthanorequalto k. Thus, for example 15!_4 = 15 middot 11_4 = 15 middot 11 middot 7!_4 = 15 middot 11 middot7 middot3!_4 = 15 middot 11 middot 7 middot 3 = 3465. Some further examples of the skip factorial, written in expanded form, are 12!_2 = 12 middot 10 middot 8 middot 6 middot 4. middot 2 = 46080, 23!_ 3 = 23 middot 20 middot 17 middot 14 middot 11 middot 8 middot 5 middot 2 = 96342400, 193!_37 = 193 middot 156 middot 119 middot 82 middot 45 middot 8 = 105765791040. Write a recursive Matlab function called skipfact.m which will calculate the value of the skip factorial when given two positive integers n and k. If either of n or k is not an integer, the function should return the flag value -1. If either of n or k is nonpositive, the function should return the flag value -2. Program specifications and sample function calls are given below. input parameter n a positive integer input parameter k a positive integer output parameter va1 the value of n!_k sample function calls skipfact (7, 1) produces 5040 skipfact (23, 3) produces 96342400 skipfact (193, 37) produces 105765791040 skipfacts (9.2, 3) produces -1 skipfact (-9.2, 3) produces -1 skipfact(-8, 3) produces -2

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!