Complexity Maniac

Agnishom gave Chris a program written in Python.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def comp(n):
    if n == 0:
        return 0
    if n == 1:
        return 1

    val = 0
    for i in range(a):
        val += comp(n-1)
    for i in range(b):
        val += comp(n-2)
    return val

How many ordered pair of non-negative integers ( a , b ) (a,b) are there such that the time complexity of this recursive function is O ( 3 n ) O(3^n) ?


The answer is 4.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Joe Mansley
Aug 3, 2016

3 n = a 3 n 1 + b 3 n 2 { 3 }^{ n }\quad=\quad a\cdot { 3 }^{ n-1 }+b\cdot { 3 }^{ n-2 }

9 = 3 a + b 9\quad =\quad 3a+b b must be a multiple of 3, so let's say b = 3 b 2 b=3{ b }_{ 2 }

so we now have b 2 + a = 3 { b }_{ 2 }+a=3

the solutions of which are ( 0 , 3 ) ( 1 , 2 ) ( 2 , 1 ) ( 3 , 0 ) (0,3)\quad (1,2)\quad (2,1)\quad (3,0)

So we have 4 solutions

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...