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.

5
  • 1
    Related: stackoverflow.com/questions/4429044/… Commented Dec 13, 2010 at 13:58
  • Is this homework? power a == b**x; Commented Dec 13, 2010 at 14:03
  • @kevpie: The question is whether an integer x exist. Commented Dec 13, 2010 at 14:08
  • @adam-matan, yes. My cryptic message was supposed to help show that. Not so much I guess. Commented Dec 13, 2010 at 14:25
  • yes, it's homework and it's from Singpath. Commented Dec 13, 2010 at 14:32

9 Answers 9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

yup..my answer is giving me "All the public tests passed but some private tests failed. You need to generalize your solution."
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.

2 Comments

yup...that's why my answer is not fully corrected although it fulfilled the given situations. may I know how to correct it?
@MyatNoe: I've updated my post. I'm trying not to just tell you the answer.
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.

Comments

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

Comments

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).

1 Comment

One edge case is enough: is_power(1, x).
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)

Comments

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 

1 Comment

Would you consider explaining this a bit, and why/how it is an answer to the question which was asked above?
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

Comments

-1
def is_power(a, b):
    if a%b == 0 and a/b**b:
        return True
    else:
        return False

is_power(10, 12)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.