Question

What is the time complexity ? int fact(int n) { if( n==1) {return 1;} n=n*fact(n-1); return...

What is the time complexity ?

int fact(int n)

{ if( n==1)

{return 1;}

n=n*fact(n-1);

return n;

}

Homework Answers

Answer #1

The given function is

int fact(int n)
        {
                if (n == 1)
                {
                        return 1;
                }
                n = n * fact(n - 1);
                return n;
        }

This is a program to calculate factorial.

Time complexity when parameter passed is 1

when 1 is passed it returns 1, and it includes only one comparision i..e if(n == 1) , nothing else is executed

So

T(1) = 1

Calculating T(n)

When any number is passed below opeations are done

time for one comparison(if (n==1)), time for one multiplication(n * fact(n-1)), time for one subtraction(n - 1)

and time for fact(n-1)

so

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

= T(n-2) + 3 + 3 = T(n - 2) + 2*3

= T(n - 3) + 3 + 3 + 3 = T(n - 3) + 3*3

= T(n - k) + k * 3 = T(n-k) + 3k

T(1) = 1

if we put

n - k = 1

k = n + 1

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

= T(1) + 3n + 3

= 1 + 3n + 3

= 3n + 4

= O(n)

So O(n) is answer

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
What does the following function compute? Give an analysis of its complexity int fun1 (int n)...
What does the following function compute? Give an analysis of its complexity int fun1 (int n) { if (n == 0)     return 1; else      return fun1(n-1) + fun1(n-1); }
Determine the order of complexity for the following algorithm: function(int n) { int l, u, m;...
Determine the order of complexity for the following algorithm: function(int n) { int l, u, m;    l=0; u= n-1;    while (l<u) {        m= (l+u)/2;        if (a[m] < x) l= m+1;        else if (a[m] >x) u=m-1;            else return “found”    }    return (“not found”); }
static int F(int n) { if (n < 1) return 1; else return 2*F(n-1)+n; } (a)...
static int F(int n) { if (n < 1) return 1; else return 2*F(n-1)+n; } (a) [7 points] Give the recurrence that describes the running time of F(int n). (b) [8 points] Solve the recurrence equation from (a). Note: For (b), you must show your work
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
Given the following function: int bar(int n) {     if( n < 0 )         return...
Given the following function: int bar(int n) {     if( n < 0 )         return -2;     else if (n <= 1)         return 2;     else       return (3 + bar(n-1) + bar(n-2)); } What is returned by bar (4)? Show all the steps
Fact 1) int(E) is open. Fact 2) int(E) is the largest (with respect to subsets) open...
Fact 1) int(E) is open. Fact 2) int(E) is the largest (with respect to subsets) open set that contains E.
Given the following function:      int C(int n, int k)                  {              
Given the following function:      int C(int n, int k)                  {                     if (k= =0) return 1;                        else return (C(n-1, k-1) * n)/k;                                       } What type of function is this? Recursive or Iterative. Explain your answer.
Consider the following function: 01: void cpUniq (const int* source, int N, vector<int>& dest) 02: {...
Consider the following function: 01: void cpUniq (const int* source, int N, vector<int>& dest) 02: { 03: list<int> L; 04: for (int i = 0; i < N; ++i) 05: { 06: copy (source, source + i, front_inserter<int>(L)); 07: } 08: for (int j: L) 09: { 10: if (find(dest.begin(), dest.end(), j) != dest.end()) 11: dest.push_back(j); 12: } 13: } and the following list of complexity values: A: O(1), B: O(log N), C: O(N), D: O(N log N), E: O(N2),...
Assume that the functions CS303 and UMKC have been defined as follows: int CS303(int n) {...
Assume that the functions CS303 and UMKC have been defined as follows: int CS303(int n) {      if (n == 0) {            return 1;      } else {            return UMKC(2, CS303(n - 1));      } } int UMKC(int n1, int n2) {      if (n1 == 0) {            return 0;      } else {            return n2 + UMKC(n1 - 1, n2);      } } What is the value of CS303(5)?
Assume that the functions CS303 and UMKC have been defined as follows: int CS303(int n) {...
Assume that the functions CS303 and UMKC have been defined as follows: int CS303(int n) {      if (n == 0) {            return 1;      } else {            return UMKC(2, CS303(n - 1));      } } int UMKC(int n1, int n2) {      if (n1 == 0) {            return 0;      } else {            return n2 + UMKC(n1 - 1, n2);      } } What is the value of CS303(4)?
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT