Skip to main content
Code Review

Return to Revisions

3 of 3
Commonmark migration

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)
Harsha
  • 1.3k
  • 1
  • 10
  • 23
lang-py

AltStyle によって変換されたページ (->オリジナル) /