1

I am doing exercise on Singpath and I am stuck at this question. This question is under recursion exercises but I have no idea what the question means.

A number, a, is a power of b if it is divisible by b and a/b is a power of b.
Write a function called is_power that takes parameters a and b and returns True if a is a power of b.

Update:

Just thought of the answer and I've posted it below.

Kev
  • 118,037
  • 53
  • 300
  • 385
Michelle Chan
  • 163
  • 2
  • 6

9 Answers9

1

It's a recursive definition of power. You are supposed to write the function

def is_power(a, b):
  ...

The definition give a general property. hint for the terminal case. if a and b are equals the function should answer true.

kriss
  • 23,497
  • 17
  • 97
  • 116
  • yup..my answer is giving me "All the public tests passed but some private tests failed. You need to generalize your solution." – Michelle Chan Dec 13 '10 at 14:26
1

Think about what it will do, at present, if you give it, say a=32 and b=2. b*b will give you 4, 16, 256...

So, you have to keep track of the original b as you're calling your function recursively. You could have a third variable with a default value (original_b), but there's a way to do it without replacing b at all.

Thomas K
  • 39,200
  • 7
  • 84
  • 86
1

Look more closely at the information you are given:

A number, a, is a power of b if it is divisible by b and a/b is a power of b.

It says "... and a/b is a power of b". It does not say "... and a is a power of b*b". There is a reason for that: you don't get the same results with the two different definitions.

Now look at your code for the recursive call:

return is_power(a,b*b)

We don't care if a is a power of b*b; we care if a/b is a power of b. So why are we calling is_power(a, b*b) # is a a power of b*b? ? Instead, we should call... well, I think you can figure it out :)

Why it's different: let's say the recursion happens twice. When we start out calling the function, let's say b = 2. On the first recursion, we pass 2 * 2 = 4. On the next recursion, the input was 4, so we pass 4 * 4 = 16. But we skipped the check for 2 * 2 * 2 = 8. 8 is a power of 2, but if we call is_power(8, 2), then is_power(8,8) never happens, and then is_power(8, 16) returns False.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
1
def isp(a,b):  
    if a%b==0 and isp(a/b,b):
        return True
    elif a/b==b:
        return True
    else:
        return False
Zach Young
  • 10,137
  • 4
  • 32
  • 53
0
def power(a,b):
if a<=b:
    if a==b: return True
    else:return False
elif a%b==0: return power(a/b,b)
else: return 
kleopatra
  • 51,061
  • 28
  • 99
  • 211
0

You need to consider the edge case where a = 0 (which some of the answers above do). I just wanted to point in out in particular, because it's easy to see why a = 1 is an important edge case, but a = 0 is also important because if you don't acknowledge it in some way you may end up with infinite recursion.

If it helps, this is how I approached it:

def is_power(a, b):
    if a == b or a == 1:
        # a == b is the 'success' base case (a is a power of b)
        # a == 1 is the success edge case where a is 1 (b ^ 0)
        return True
    elif a % b != 0 or a == 0:
        # a % b != 0 is the 'failure' base case (a is not a power of b)
        # a == 0 is the failure edge case where a is 0. If
        # you don't acknowledge this case in some way, the function
        # will recurse forever
        return False
    else:
        # else, keep recursing
        return is_power(a / b, b)


print is_power(8, 2) # True
print is_power(6, 2) # False
print is_power(0, 2) # False
print is_power(1, 2) # True
mithos
  • 51
  • 1
  • 2
0

You should start with the trivial cases, there are two in fact: is_power(x,x) and is_power(1,x).

Once you have the edge case you just need to write down the definition correctly. It mentions a/b and b, but you wrote return is_power(a,b*b). Maybe you think that is the same, just scaled both arguments with b, but it is not. Think about the values of b in is_power(27,3).

Jochen Ritzel
  • 104,512
  • 31
  • 200
  • 194
0

Here is my answer...

def is_power(a,b):
    if(a%b != 0):
        return False
    elif(a/b == 1):
        return True
    else:
        return is_power(a/b,b)
Michelle Chan
  • 163
  • 2
  • 6
-1
def is_power(a, b):
    if a%b == 0 and a/b**b:
        return True
    else:
        return False

is_power(10, 12)
RamenChef
  • 5,557
  • 11
  • 31
  • 43
urosh
  • 1
  • 1