How to check if a number is the power of base b?

In python, how can you check if number n is the exact power of base b?

Note: it must be generalized to any base that is specified as a parameter.

Here is what I got:

Suppose n and base are integers> 0.

import math
def is_power(n,base):
    return math.log(n,base) == base**n
+5
source share
5 answers

First, if you have a specific logarithm operator (many languages ​​provide logarithms only on a base 10or base basis e), you can calculate how (where , obviously, is a base, in your language).logablogxb / logxax

Python is doing better because it can work out a logarithm for an arbitrary base without this complex equality above.

, . , b a ( 1) b a.

, , :

# Don't even think about using this for negative powers :-)

def isPower (num, base):
    if base == 1 and num != 1: return False
    if base == 1 and num == 1: return True
    if base == 0 and num != 1: return False
    power = int (math.log (num, base) + 0.5)
    return base ** power == num

., , , :

import math

def isPower (num, base):
    if base == 1 and num != 1: return False
    if base == 1 and num == 1: return True
    if base == 0 and num != 1: return False
    power = int (math.log (num, base) + 0.5)
    return base ** power == num

print isPower (127,2)       # false
print isPower (128,2)       # true
print isPower (129,2)       # false
print

print isPower (26,3)        # false
print isPower (27,3)        # true
print isPower (28,3)        # false
print isPower (3**10,3)     # true
print isPower (3**129,3)    # true
print

print isPower (5,5)         # true
print isPower (1,1)         # true
print isPower (10,1)        # false

, , , , . , isPower(128,2), isPower(verybignum,2).

:

def isPower (num, base):
    if base == 1 and num != 1: return False
    if base == 1 and num == 1: return True
    if base == 0 and num != 1: return False
    testnum = base
    while testnum < num:
        testnum = testnum * base
    return testnum == num

, , , .


( 1) , , . , " " .

+9

:

def ispower(n, base): 

    if n == base:
        return True

    if base == 1:
        return False

    temp = base

    while (temp <= n):
        if temp == n:
            return True
        temp *= base

    return False

:

>>> ispower(32, 2)
True
>>> ispower(81, 3)
True
>>> ispower(625, 5)
True
>>> ispower(50, 5)
False
>>> ispower(32, 4)
False
>>> ispower(2,1)
False
>>> ispower(1,1)
True
+3

, , :

def is_power(n, base):
    if base == 1:
        # note: the question stated "Assume n and base are integers > 0"
        return n == 1
    while n % base == 0:
        n //= base
        if n == 1:
            return True
    return False

, reduce lambda, pythonic.

0

>>>(math.log(int(num),int(base))).is_integer()

true false. . , .

0
>>> def isPower(n, b):
...   return b**int(math.log(n, b)+.5)==n
... 
>>> isPower(128, 2)
True
>>> isPower(129, 2)
False
>>> isPower(3**10, 3)
True
>>> isPower(3**129, 3)
True
>>> isPower(10**500, 10)
True
>>> isPower(10**(10**6), 10)
True


EDIT: 1,1:
>>> isPower(1,1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in isPower
ZeroDivisionError: float division by zero

, , .

-1

All Articles