开发者

Finding out whether a is a power of b

开发者 https://www.devze.com 2023-02-05 07:34 出处:网络
I\'m currently using singpath.com to practice on my python, but I face an issue with a problem: A number, a, is a power of b if it is divisible by b and a/b is a power of b.

I'm currently using singpath.com to practice on my python, but I face an issue with a problem:

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.

def is_p开发者_JAVA百科ower(a,b):
    c = a/b
    if (((a%b) == 0) and ((c%b) == 0)):
        return True
    else:
        return False 

Above is my solution but the system prompt me to generalize my solution. Can anyone tell me whats wrong with my solution?


The reason why your original code does not work is the following: You just check (c%b) == 0) aka (a/b) is divisible by b, which is much weaker than the a/b is a power of b part of the definition.

When you want to solve a problem such as this you should always start with the trivial cases. In this case there are two such cases: is_power(x,x) and is_power(1,x) - in both the answer is True, because x**1==x and x**0==1.

Once you have these cases covered you just need to write down the rest of the definition. Write code for (a is divisible by b) and (a/b is a power of b) and put it all together.

The final function will look like this:

def is_power(a,b):
    if <trivial case 1> or <trivial case 2>:
        return True
    # its a recursive definition so you have to use `is_power` here
    return <a is divisible by b> and <a/b is a power of b>

The only question left is how to answer <a/b is a power of b>. The easiest way to do this is using the function is_power itself - this is called recursion.


def is_power(a,b):
'''this program checks if number1 is a power of number2'''

if (a<b):  # lesser number isn't a power of a greater number
    return False
elif (b==0) or (b==1) : # Exception cases
    return False
elif a%b == 0 and is_power(a/b, b) == True:  # Condition check for is_power (Recursion!)
    return True
else:
    return False


You are only checking the first two powers: a divides b and a/b divides b. It could be that a = b ** 3 or b ** 4 (or b ** n in general), so the actual solution will have to involve recursion or a loop.


Here is my solution.

def is_power(a,b):
if a == 1: # base case for recursion
    return True
elif b == 0 or a%b !=0 or a<b: # exception cases.
    return False
return is_power(a//b,b) # recursion

I tested with several cases (16,2),(6,2),(1,2),(0,0),(0,1) and it works well.


A much simpler solution is:

def is_power(a, b):
    while a % b == 0:
        a //= b
    return a == 1

Recursion is really not needed for this problem. Moreover recusion may cause a recursion limit error if a = b ** 1001.


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

The end condition which is a == b is crucial here which stops when both numbers are equal. If this is not included the function could show False for even legitimate numbers by dividing a/b in the next iteration which gives 1 where 1 % b = 1 which in turn returns False instead of True.


I wouldn't say to generalize it. I would say to correct it as it's incorrect. Using your solution is_power(12,2) returns True as does is_power(18,3).

I think that the reason that the system says to generalize it is that it's probably working correctly for some of their test cases, but not others. It's likely that the test cases for which it is working are coincidentally those for which it would work if it were hard-coded in a certain way (only checking powers of 2, for example).


You're checking whether a/b is divisible by b (in the expression (c%b) == 0), rather than whether a/b is a power of b. Hint: What function would you call to see whether something is a power of b?


To understand recursion, you need first to understand recursion.

 def is_power(a, b):
     if a < b: # 3 is never a power of 10, right?
         return False # prevent recursion
     if a == b:  # a is always a**1, right?
         return True  # prevent recursion
     else:
         return is_power(a / b, b) # recursion!

Note that for integers a / b will give you rounding errors. Make sure you pass floats.


I don't think you have the right implementation. Based on the problem, the is_power function should look something like this:

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


You are answering to the first constraint but not to the second,
You check to see that (a/b)%b == 0 which is a special case of "(a/b) is a power of b". Therefor the generalization error (try to think of generalizing that specific case.

What you wrote is not a solution to is power of for example you will indicate 12 as a power of 2 since:

  • 12%2 = 0,
  • (12/2)%2 = 0

But that is clearly wrong.

As others said, think of recursion (or the less preferable looping solution).


I was just working on this question myself, and this is what I came up with.

To write this as a recursive function, you need the recursive part and the trivial part. For the recursive part, a number is the power of another number if:

((a%b)==0) and (is_power(a/b, b) # a is evenly divisible by b and a/b is also a power of b.

For the trivial case, b is a power of a if a=b.

However, we're not done. Because we are dividing by b, we have to make an exception for when b is zero.

And the other exception is when b = 1. Since when b=1, a/b is a, we will end up with infinite recursion.

So, putting it all together:

def is_power(a,b):
    # trivial case
    if (a==b): return True

    # Exception Handling
    if (b==0) or (b==1): return False # unless a==b==0 or a==b==1, which are handled by the trivial case above

    # recursive case
    return ((a%b)==0) and (is_power(a/b,b))


This example should fix your problem :

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


You can use log.

import math

def is_power(a, b):
    return math.log(a, b) % 1 == 0


I hope that this works, this one worked fine for me.

import math # I will use the math module for the next function

def is_power (a, b):
    if b == 1 and a!= 1: 
        return False
    if b == 1 and a == 1:
        return True
    if b == 0 and a!= 1:
        return False
    power = int (math.log(a, b) + 0.5)
    return b ** power == a


Your solution is correct however you just need to remove ALL of the parenthesis in your if statement.

def ab(a, b): c = b/a if a%b == 0 and c%b == 0: return True else: return False

print (ab(32, 2))

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号