How do you check if a number is a power of another number using recursion in python?

How do I make the is_power_of function return whether the number is a power of the given base? base is assumed to be a positive number.

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

How do you check if a number is a power of another number using recursion in python?

SuperStormer

4,7515 gold badges20 silver badges32 bronze badges

asked May 14, 2020 at 12:16

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).
    return number == 1
  result = number//base
  # Recursive case: keep dividing number by base.
  return is_power_of(result, base)

answered Jul 27, 2020 at 6:46

ShishirShishir

1962 silver badges7 bronze badges

1

No recursion or brute force is required. Just logarithms.

When you have number = base ** x, you get x = log(number) / log(base). Simply checking if x is an integer should give you the answer.

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

If you want to handle negative numbers as well, you could just calculate x and then check that base ** x is equal to number

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

answered Jul 15, 2020 at 15:59

Pranav HosangadiPranav Hosangadi

19k5 gold badges41 silver badges65 bronze badges

2

The right way to do it is using recursion since it is asked in the chapter of this exercise.

So it will work like this

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

answered Aug 17, 2020 at 1:25

How do you check if a number is a power of another number using recursion in python?

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

answered Jul 12, 2021 at 2:05

2

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

It is done by recursion

answered Jul 15, 2020 at 14:27

3

How do you check if a number is a power of another number in Python?

1) Initialize pow = x, i = 1 2) while (pow < y) { pow = pow*pow i *= 2 } 3) If pow == y return true; 4) Else construct an array of powers from x^i to x^(i/2) 5) Binary Search for y in array constructed in step 4. If not found, return false.

How do you find the power of a recursive number?

Example: Program to Computer Power Using Recursion This technique can only calculate power if the exponent is a positive integer. To find power of any number, you can use pow() function. result = pow(base, exponent);

How do you check if a number is power of a given number?

To check if a given integer x is a power of a given integer y , check if x > 0 && Y % x == 0 : Y is the largest power of y that can fit into an integer datatype. The general idea is that if A is some power of Y , A can be expressed as B/Ya , where a is some integer and A < B .

How do you find the power of a number in Python?

How to find the power of a number in Python.
import math. print(math. pow(4,2)) Run. Importing math module in Python..
def power(n,e): res=0. for i in range(e): res *= n. return res. print(pow(4,2)) Run. ... .
def power(n, e): if e == 0: return 1. elif e == 1: return n. else: return (n*power(n, e-1)).

What does it mean when a number is a power of another number?

The power (or exponent) of a number says how many times to use the number in a multiplication. It is written as a small number to the right and above the base number. In this example the little "2" says to use 8 two times in a multiplication: 82 = 8 × 8 = 64.

How do you check if a number is a power of 5?

If last digit is 5; divide it by 5. If result of division is 1, then number is power of 5.