Recursive definition for factorial: a0 = 1, an = n * an-1
procedure factorial(n: nonnegative integer)
if n = 0 then return 1
else return n * factorial( n - 1 )
Trace the execution of the factorial algorithm described above for input 7. Track the number of times factorial is invoked (with the first invocation with input 7 as invocation 0) and the value returned by each invocation.
`Hey,
Note: If you have any queries related the answer please do comment. I would be very happy to resolve all your queries.
TRACE
factorial(7) returns 7 * factorial(6)
factorial(6) returns 6 * factorial(5)
factorial(5) returns 5 * factorial(4) factorial(4) returns 4 * factorial(3) factorial(3) returns 3 * factorial(2) factorial(2) returns 2 * factorial(1) factorial(1) returns 1 * factorial(0) factorial(0) returns 1
Once factorial(0) executes and returns 1 that value can be substituted back into the previous method call, starting at the top of the stack (shown at the bottom here) and working our way back to the bottom of the stack (shown at the top here).
factorial(7) returns 7 * factorial(6) = 7 * 720 = 5040
factorial(6) returns 6 * factorial(5) = 6 * 120= 720
factorial(5) returns 5 * factorial(4) = 5 * 24 = 120 factorial(4) returns 4 * factorial(3) = 4 * 6 = 24 factorial(3) returns 3 * factorial(2) = 2 so 3 * 2 = 6 factorial(2) returns 2 * factorial(1) = 1 so 2 * 1 = 2 factorial(1) returns 1 * factorial(0) = 1 so 1 * 1 = 1 factorial(0) returns 1
Kindly revert for any queries
Thanks.
Recursive definition for factorial: a0 = 1, an = n * an-1 procedure factorial(n: nonnegative integer)...
Given algorithm- procedure factorial (n: nonnegative integer) if n = 0 then return 1 else return n*factorial(n-1) {output is n!} Trace the above algorithm when it is given n = 7 as input. That is, show all steps used by above algorithm to find 7!
Consider the following recursive definition of a factorial function. int factorial ( int n) { if ( n == 0 || n ==1 ) return 1; else return n * factorial (n-1); } Suppose the function factorial (4) is invoke. Trace through the function call, explicitly show how the factorial function is repeatedly called and what is the value of n in each call. Also show the value returned by each call. Give an...
The following recursive method factRecursive computes the factorial of positive integer n. Demonstrate that this method is recursive. public static int factRecursive(int n) { int result = 0; if (n == 0) { result = 1; } else { result = n * factRecursive(n - 1); } return result; }
ALGORITHM RecS(n) // Input: A nonnegative integer n ifn=0 return 0 else return RecS(n+ n n n Determine what this algorithm computes. You must justify your answer. made by this algorithm and solve it. You must justify your answer. same thing using for/while loop(s) developed in (3). You must justify your answer. 1) 2) Set up the initial condition and recurrence relation for the number of multiplications 3) Write the pseudocode for the non-recursive version of this algorithm, i.e., compute...
5. (10 points) The factorial of a nonnegative integer n is written n! and is defined as follows. n 2) ..1 (for values of n greater than 1) nn (n-l) and n-# 1 (for n 0 or n-1) l. Write a program that reads a nonnegative integer and computes and prints its factoria
permutations are there with people)? 34. A class has enough time for 4 questions. 10 students are present. Suppose a student can ask up to 4 questions, how many combinations can there be of students asking questions? (How many 4- combinations are there from 10 students, with repetition)? 35. Show that if there are 30 students in a class, then at least two of the students have first names that 3pts begin with the same letter. What principle can you...
The factorial of a nonnegative n written as n! is defined as follows: n!= n*(n-1)*(n-2) * .... *1 (for all values of n greater than 0) and 0! =1. For example 5! = 5*4*3*2*1 which is 120. (can also be 1*2*3*4*5) Write a C++ program that reads a nonnegative integer and computes and prints its factorial.
ASSEMBLY LANGUAGE (Mars MIPS)
Starting code:
Factorial: #Factorial Recursive function
subu $sp, $sp, 4
sw $ra, 4($sp) # save the return address on stack
beqz $a0, terminate # test for termination
subu $sp, $sp, 4 # do not terminate yet
sw $a0, 4($sp) # save the parameter
sub $a0, $a0, 1 # will call with a smaller argument
jal Factorial
# after the termination condition is reached these lines
# will be executed
lw $t0, 4($sp) # the argument I...
1. Let m be a nonnegative integer, and n a positive integer. Using the division algorithm we can write m=qn+r, with 0 <r<n-1. As in class define (m,n) = {mc+ny: I,Y E Z} and S(..r) = {nu+ru: UV E Z}. Prove that (m,n) = S(n,r). (Remark: If we add to the definition of ged that gedan, 0) = god(0, n) = n, then this proves that ged(m, n) = ged(n,r). This result leads to a fast algorithm for computing ged(m,...
Consider the following recursive algorithm for computing the sum
of the first n cubes: S(n) = 13 +
23 + … + n3.
(a) Set up a recurrence relation for the number of
multiplications made by this algorithm.
(b) Provide an initial condition for the
recurrence relation you develop at the question (a).
(c) Solve the recurrence relation of the
question (a) and present the time complexity as described at the
question number 1.
Algorithm S n) Input: A positive...