Engineering

Computer ScienceQ&A LibraryWrite a C++ program, no need to be object-oriented, to test the following three functions. Each functionreceives one parameter n, which is a positive integer.int F1(int n){if (nif (n % 2int result0) return 1;0) {F1(n / 2);return result * result;else1);return 2 * F1(n}int F2(int n){if (nreturn 2 * F2(n - 1);0) return 1;int F3(int n){if (n0) return 1;return F3(n - 1) + F3(n - 1);}Test the functions by calling them with the following values for n: 1, 3, 5, 16, 24, 26, 30.Submit your test program and in your report answer the following questions:a. What does each function return as a function of input parameter n?b. Which function is the worst in terms of time efficiency? Support your answer by stating, withoutproof, the running time growth rate, as a function of n, of each of the above functions.Question

Asked Feb 7, 2020

66 views

Please help me with question b to find the running time. Thank you.

Function F1

For function F1 the number of steps assuming only if branch:

T(1) = 1

T(n) = 1 + T(n/2)

Hence by repeated substitution we get

T(n) = log(n) + 1 i.e. O(logn)

For function F1 the number of steps assuming only the else branch:

T(1) = 1

T(n) = 1 + T(n-1)

Hence by repeated substitution we get

T(n) = k + T(n-k) = … = n -1 + 1 = O(n)

Hence the worst case time complexity is O(n) as O(n) is tighter than O(logn)

Function F2

For function F2 doing similar analysis as for F1 we get a worst case time complexity de...

Tagged in

Find answers to questions asked by student like you

Show more Q&A

Q: In a computer instruction format, the instruction length is 10 bits and the size of an addressfield ...

A: Total number of instructions =210=1024Size of address field=3 bitsAnd the instruction length is 10 ...

Q: - For all the employees supervised by KING, give them a salary increase, which is equal to the 2% of...

A: Programming Instructions:Create two database tables ‘EMP’ and ‘DEPT’ and declare all required attrib...

Q: (1) Prompt the user for a title for data. Output the title. Ex: Enter a title for the data: Number o...

A: Answer:1.Program: #include <iostream>#include <string> using namespace std; int main(){ ...

Q: Please see attachment.

A: Given expression:

Q: #include <stdio.h> int main(){printf(" enter two integers and ill add them \n"); int integer1;...

A: Program Instructions: In the given C code at the time of taking input from the user by using 'scanf(...

Q: 13. The reciprocal Fibonacci constant y is defined by the infinite sum: ν- ΣΕ where F, are the Fibon...

A: Program:n = [10 50 100];for j = 1:3a = 1;b = 1;reci = 1/a+1/b;for i = 1:n(j) - 2c = a + b;reci = rec...

Q: Do the following program in Java Eclipse. Java Class that can be used to create a Fruit object as de...

A: The java program and approach are given below:Note: Run the same code on Eclipse.Approach: Creating ...

Q: Convert the following decimals numbers to binary. 187 20486

A: For converting the given decimal number into the binary number, keep dividing the available number w...

Q: Draw the logic diagram for the following Boolean expressions. The diagram should correspond exactly ...

A: OR gate is used where + is provided in the boolean expression.AND gate is used where .(or multiplica...