Question

In: Computer Science

This function uses a curious mix of iteration and recursion: function F(n) if n < 1...

This function uses a curious mix of iteration and recursion:

function F(n)
    if n < 1 
        return 1
    t <- 0
    for i <- 0 to n
        for j <- i to n
            t <- t + j
    return t + F(n-1) 
What is the number of basic operations (additions and subtractions) performed? Answer: Θ(n³)

Could you tell me how I can solve this problem??

Solutions

Expert Solution

This is a recursive function. so, follow these steps for calculating it's time complexity.

total amount of work done per one recursive call:
--------------------------------------------------
There are two for loops
outer for loop iterates n times
inner for loop iterates n times
so, total number of operations = n^2 = O(n^2)

size of the recursive call: n-1.  (because of F(n-1))
number of recursive calls inside F(n) = 1. (single recursive calls to F(n-1)

so, overall time complexity T(n) = 1*T(n-1) + O(n^2)
T(n) = T(n-1) + O(n^2)

let's solve this recurrence relation:
--------------------------------------
T(n)
= T(n-1) + n^2
= T(n-2) + (n-1)^2 + n^2
= T(n-3) + (n-2)^2 + (n-1)^2 + n^2
= T(1) + 2^2 + ... + (n-2)^2 + (n-1)^2 + n^2
= 1 + 2^2 + ... + (n-2)^2 + (n-1)^2 + n^2
formula for sum of squares of first n numbers is n(n+1)(2n+1)/6
ignore constance terms.
so, time complexity is O(n^3)

Answer: O(n^3)

Related Solutions

Describe the similarities between using recursion versus iteration. Describe the difference between using recursion versus iteration.
Describe the similarities between using recursion versus iteration. Describe the difference between using recursion versus iteration.
Let function F(n, m) outputs n if m = 0 and F(n, m − 1) +...
Let function F(n, m) outputs n if m = 0 and F(n, m − 1) + 1 otherwise. 1. Evaluate F(10, 6). 2. Write a recursion of the running time and solve it . 3. What does F(n, m) compute? Express it in terms of n and m.
Suppose f : N→N satisestherecurrencerelation f(n + 1) (f(n) 2 if f(n)iseven 3f(n)+ 1 if f(n)isodd...
Suppose f : N→N satisestherecurrencerelation f(n + 1) (f(n) 2 if f(n)iseven 3f(n)+ 1 if f(n)isodd . Notethatwiththeinitialcondition f(0) 1,thevaluesofthefunction are: f(1) 4, f(2) 2, f(3) 1, f(4) 4, and so on, the images cyclingthroughthosethreenumbers. Thus f isNOTinjective(andalso certainlynotsurjective). Mightitbeunderotherinitialconditions?3 (a) If f satisestheinitialcondition f(0) 5,is f injective? Explain whyorgiveaspecicexampleoftwoelementsfromthedomain withthesameimage. (b) If f satisestheinitialcondition f(0) 3,is f injective? Explain whyorgiveaspecicexampleoftwoelementsfromthedomain withthesameimage. (c) If f satisestheinitialcondition f(0) 27,thenitturnsoutthat f(105) 10 and no two numbers less than 105 have the same...
Fibonacci Sequence: F(0) = 1, F(1) = 2, F(n) = F(n − 1) + F(n −...
Fibonacci Sequence: F(0) = 1, F(1) = 2, F(n) = F(n − 1) + F(n − 2) for n ≥ 2 (a) Use strong induction to show that F(n) ≤ 2^n for all n ≥ 0. (b) The answer for (a) shows that F(n) is O(2^n). If we could also show that F(n) is Ω(2^n), that would mean that F(n) is Θ(2^n), and our order of growth would be F(n). This doesn’t turn out to be the case because F(n)...
T/F 1) The function f(x) = x1 − x2 + ... + (−1)n+1xn is a linear...
T/F 1) The function f(x) = x1 − x2 + ... + (−1)n+1xn is a linear function, where x = (x1,...,xn). 2) The function f(x1,x2,x3,x4) = (x2,x1,x4,x3) is linear. 3) For a given matrix A and vector b, equation Ax = b always has a solution if A is wide
#2 The Jacobsthal sequence is defined by J(1)=J(2)=1 and J(n)=J(n-1)+2J(n-2). Use recursion to write a function...
#2 The Jacobsthal sequence is defined by J(1)=J(2)=1 and J(n)=J(n-1)+2J(n-2). Use recursion to write a function that takes in a positive integer n and returns the nth Jacobsthal number. >>> J(8) 85 >>> J(9) 171 #3 Use recursion to write a function that takes in a positive integer n and returns all n digit numbers containing only odd digits. >>> f(1) [1, 3, 5, 7, 9] >>> f(2) [11, 13, 15, 17, 19, 31, 33, 35, 37, 39, 51, 53,...
Let f:(a, b) → R be a function and n∈N. Assume that f is n-times differentiable...
Let f:(a, b) → R be a function and n∈N. Assume that f is n-times differentiable and f^(n)(x) = 0 for all x∈(a,b). Show that f is a polynomial of degree at most n−1.
a) write a program to compute and print n, n(f), f(f(n)), f(f(f(n))).........for 1<=n<=100, where f(n)=n/2 if...
a) write a program to compute and print n, n(f), f(f(n)), f(f(f(n))).........for 1<=n<=100, where f(n)=n/2 if n is even and f(n)=3n+1 if n is odd b) make a conjecture based on part a. use java
Define the following function f(n) = 5(2^n)-(2^(n-1)), n ≥ 1. Write a recursive definition for the...
Define the following function f(n) = 5(2^n)-(2^(n-1)), n ≥ 1. Write a recursive definition for the function f(n)? Consider the following recurrence: an= 2an-1 + 3 (where a1 = 1). Compute the values of an for n ≤ 5. Find a solution for the recurrence definition and validate its correctness. Consider the following recurrence: an=2an-1 +an-1an-2 (where a1 = 1). Compute the values of an for n ≤ 5.
Fibonacci Sequence in JAVA f(0) = 0, f(1) = 1, f(n) = f(n-1) + f(n-2) Part...
Fibonacci Sequence in JAVA f(0) = 0, f(1) = 1, f(n) = f(n-1) + f(n-2) Part of a code public class FS { public static void main(String[] args){ IntSequence seq = new FibonacciSequence(); for(int i=0; i<20; i++) { if(seq.hasNext() == false) break; System.out.print(seq.next()+" "); } System.out.println(" "); } } ///Fill in the rest of the code! Output 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 You should define...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT