如何计算一个数字是否为另一个数字的幂?

时间:2020-05-14 12:16:04

标签: python-3.x

def is_power_of(数字,基数): #基本情况:当数字小于基数时。 填写空格以使is_power_of函数返回数字是否为给定基数的幂。注意:假定基数为正数。提示:对于返回布尔值的函数,您可以返回比较结果。

   def is_power_of(number, base):
  # Base case: when number is smaller than base.
  if number < base:
    # If number is equal to 1, it's a power (base**0).
    return __

  # Recursive case: keep dividing number by base.
  return is_power_of(__, ___)

   print(is_power_of(8,2)) # Should be True
   print(is_power_of(64,4)) # Should be True
   print(is_power_of(70,10)) # Should be False```

11 个答案:

答案 0 :(得分:6)

def is_power_of(number, base):
  # Base case: when number is smaller than base.
  if number < base:
    # If number is equal to 1, it's a power (base**0).
    return number == 1
  result = number//base
  # Recursive case: keep dividing number by base.
  return is_power_of(result, base)

答案 1 :(得分:1)

不需要递归或蛮力。只是对数。

拥有number = base ** x时,您得到x = log(number) / log(base)。只需检查x是否为整数即可为您提供答案。

def is_power_of(number, base):
    return (math.log(number) / math.log(base)).is_integer()

如果您还想处理负数,则只需计算x,然后检查base ** x等于number

def is_power_of(number, base):
    x = math.log(abs(number)) / math.log(abs(base))
    return base ** x == number

答案 2 :(得分:1)

正确的方法是使用递归,因为在本练习的章节中会对此进行询问。

所以它将像这样

def is_power_of(number, base):
# Base case: when number is smaller than base.
number= number/base
 if number < base:
# If number is equal to 1, it's a power (base**0).
  return False
 else: 
  return True
return is_power_of(number, base)


print(is_power_of(8,2)) # Should be True
print(is_power_of(64,4)) # Should be True
print(is_power_of(70,10)) # Should be False

答案 3 :(得分:0)

您随时可以尝试蛮力。

def is_power_of(number,base):
  if number%base or base==1: return False # preliminary check for divisibility
  i=1
  while i<number:
    i*=base
    if i==number: return True
  return False

注意:这不适用于负数或复数。

答案 4 :(得分:0)

def is_power_of(number, base):
  
    if number<base or base==1: return False 
    i=1
    while i<number:
      i*=base
      if i==number:
         return True
    return False     
    return is_power_of(number,base)  

答案 5 :(得分:0)

def is_power_of(number,base):
    if number == base:
        return True
    elif number < base:
        return False
    return is_power_of(number / base, base)

这是通过递归完成的

答案 6 :(得分:0)

在基数情况下,如果基数确实为基数,则返回number == 1以得出true或false。 声明一个新变量n =数字/基数,以允许递归返回is_power_of(n,base)继续直到发生基本情况(n

答案 7 :(得分:0)

def is_power_of(number, base):
    # Base case: when number is smaller than base.
    if number < base:
        # If number is equal to 1, it's a power (base**0).
        return number==1

     # Recursive case: keep dividing number by base.
     return is_power_of(number/base, base)

print(is_power_of(8,2)) # Should be True
print(is_power_of(64,4)) # Should be True
print(is_power_of(70,10)) # Should be False

答案 8 :(得分:0)

def is_power_of(number, base):
    if number < base: 
        return number==1
    return is_power_of(number/base,base)

print(is_power_of(8,2)) 
print(is_power_of(64,4))
print(is_power_of(70,10)) 

答案 9 :(得分:0)

def is_power_of(number, base):
  number =number/base
  # Base case: when number is smaller than base.

  if number < base:
    # If number is equal to 1, it's a power (base**0).
    return False
  if number > base:
      return True

  # Recursive case: keep dividing number by base.
  return is_power_of(number, base)

print(is_power_of(8,2)) # Should be True
print(is_power_of(64,4)) # Should be True
print(is_power_of(70,10)) # Should be False

output
True
True
False

答案 10 :(得分:-1)

def is_power_of(number, base):
  # Base case: when number is smaller than base.
  if number < base:
    # If number is equal to 1, it's a power (base**0).
    if number == 1:
      return True

    else:
      return False
  # Recursive case: keep dividing number by base.
  return is_power_of(number/base , base)

print(is_power_of(8,2)) # Should be True
print(is_power_of(64,4)) # Should be True
print(is_power_of(70,10)) # Should be False