Question

The factorial of n is defined as n!=1×2×3×···×n. Write a Matlab.m function myfactorial to compute factorials...

The factorial of n is defined as n!=1×2×3×···×n. Write a Matlab.m function myfactorial to compute factorials in a recursive manner by calling the m-function myfactorial itself. Test your m-function myfactorial forn = 0,1,2,3,4,5,6,7,8,9,10.

Homework Answers

Answer #1

MATLAB Script:

close all
clear
clc

for n = 0:1:10
fprintf('myfactorial(n = %2d) = %d\n', n, myfactorial(n))
end

function f = myfactorial(n)
if n == 0
f = 1;
return
end
f = n * myfactorial(n - 1);
end

Output:

myfactorial(n = 0) = 1
myfactorial(n = 1) = 1
myfactorial(n = 2) = 2
myfactorial(n = 3) = 6
myfactorial(n = 4) = 24
myfactorial(n = 5) = 120
myfactorial(n = 6) = 720
myfactorial(n = 7) = 5040
myfactorial(n = 8) = 40320
myfactorial(n = 9) = 362880
myfactorial(n = 10) = 3628800

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a C++ program with a user-defined function myFactorial, that calculates the factorial of a number...
Write a C++ program with a user-defined function myFactorial, that calculates the factorial of a number entered by the user. Return the calculated factorial and print it in the main function.
For any integer n > 0, n!(n factorial) is defined as the product n * n...
For any integer n > 0, n!(n factorial) is defined as the product n * n - 1 * n − 2 … * 2 * 1. And 0! is defined to be 1 Create function that takes n as input and computes then returns the accurate value for: n!= n * n - 1 * n − 2 … * 2 * 1 prompt the user to enter an integer n, call functions to compute the accurate value for...
Be able to write Python programming of a factorial of an integer (given input parameters, output...
Be able to write Python programming of a factorial of an integer (given input parameters, output characteristics and expected output behavior, do not use internal python functions for factorials), recall for example that: 5! = 5 ∗ 4 ∗ 3 ∗ 2 ∗ 1 = 120 and to continue asking unless user says to stop NOT USING FACTORIAL FUNCTION
needed in c++ (no step by step) The gcd(m, n) can also be defined recursively as...
needed in c++ (no step by step) The gcd(m, n) can also be defined recursively as follows: If m % n is 0, gcd (m, n) is n. Otherwise, gcd(m, n) is gcd(n, m % n). Write a recursive function to find the GCD. Write a test program that prompts the user to enter two integers and displays their GCD.
(in java) a. Include a good comment when you write the method described below: Write a...
(in java) a. Include a good comment when you write the method described below: Write a method factorial which receives a positive integer n and calculates n! (n factorial), defined as follows: n!=1*2*…*(n-1)*n. For example, 5! = 1*2*3*4*5 = 120. The method should return this value to the calling program. b. Write a driver program (main method) which will read an integer from the console, and pass it to the method to calculate its factorial. The main method then prints...
Consider the recursive algorithm given to compute factorial. Which statement below most closely relates to the...
Consider the recursive algorithm given to compute factorial. Which statement below most closely relates to the concept of the inductive hypothesis in induction? n = 0 factorial(n-1) return n * factorial(n-1) return Which counting rule is best suited to solving the following problem: You are a pop star performing a concert for your fans. You say “Everybody put your hands up!” and you count 28 hands. Assuming everyone in your audience has 2 hands, how many audience members are there?...
Design a recursive algorithm to compute 3^n based on the formula 3^n=3^(n−1) + 3^(n−1) + 3^(n−1)....
Design a recursive algorithm to compute 3^n based on the formula 3^n=3^(n−1) + 3^(n−1) + 3^(n−1). Also do the recurrence relation.
Write a recursive C++ function writeLine(ch,n) that writes a character repeatedly to form a line of...
Write a recursive C++ function writeLine(ch,n) that writes a character repeatedly to form a line of n characters. For example, writeLine('*', 5) produces the line *****. 2. Now write a iterative function writeBlock (ch,n,m) that uses writeLine(ch, n) to write m lines of n characters each. For example, writeBlock('*', 5, 3) produces the output ***** ***** ***** 3. provide a main() function for testing, if time and space allowed.
Write Java program Lab42.java which takes as input two positive integers m and n and computes...
Write Java program Lab42.java which takes as input two positive integers m and n and computes their least common multiple by calling method lcm(m,n), which in turn calls recursive method gcd(m,n) computing the greatest common divisor of m and n. Recall, that lcm(m,n) can be defined as quotient of m * n and gcd(m,n).
C++ please! Write a recursive function to compute the sum of the Taylor series. use double...
C++ please! Write a recursive function to compute the sum of the Taylor series. use double taylor(int a, int n)