How to 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

SuperStormer

4,7525 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

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

Is power function in Python using recursion?

call the power function and print its returned value. Can do. The recursive power function, power[base,exponent], must recursively calculate the value of the power and then return it.

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 an integer is a power to another integer in Python?

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 .

Chủ Đề