Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

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)
Tweeted twitter.com/StackCodeReview/status/853710097536647168
edited tags
Link
Harsha
  • 1.3k
  • 1
  • 10
  • 23
Source Link
Harsha
  • 1.3k
  • 1
  • 10
  • 23

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)
lang-py

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