I was asked this question in interview:
Check if a number N is a power of K.
Example: N = 32, K = 2 => True
N = 40, K = 5 => False
I wrote following code but got the feedback that, complexity could have be improved, How to improve its complexity?
def check_kth_power(n, k):
while n%k == 0:
n = n/k
if n != 1:
return False
return True
print check_kth_power(128, 5)
I was asked this question in interview:
Check if a number N is a power of K.
Example: N = 32, K = 2 => True
N = 40, K = 5 => False
I wrote following code but got the feedback that, complexity could have be improved, How to improve its complexity?
def check_kth_power(n, k):
while n%k == 0:
n = n/k
if n != 1:
return False
return True
print check_kth_power(128, 5)
I was asked this question in interview:
Check if a number N is a power of K.
Example: N = 32, K = 2 => True
N = 40, K = 5 => False
I wrote following code but got the feedback that, complexity could have be improved, How to improve its complexity?
def check_kth_power(n, k):
while n%k == 0:
n = n/k
if n != 1:
return False
return True
print check_kth_power(128, 5)
Check if a number N is a power of K
I was asked this question in interview:
Check if a number N is a power of K.
Example: N = 32, K = 2 => True
N = 40, K = 5 => False
I wrote following code but got the feedback that, complexity could have be improved, How to improve its complexity?
def check_kth_power(n, k):
while n%k == 0:
n = n/k
if n != 1:
return False
return True
print check_kth_power(128, 5)