알고리즘/코드워

[python]What's a Perfect Power anyway?

(ㅇㅅㅎ) 2020. 4. 3. 21:22
728x90
반응형

https://www.codewars.com/kata/54d4c8b08776e4ad92000835/train/python

 

Codewars: Train your coding skills

Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.

www.codewars.com

더보기

내 코드

# My Code
import math
# 에라토스테네스 체 응용
def isPP(n):
    # 효율적으로 코딩하기 위해서는 n의 제곱근까지만 확인
    for m in range(2int(math.sqrt(n)) + 1):
        # 지수함수의 역연산이 필요하므로 log 사용
        # ex) log3 81 = 4
        k = int(math.log(n, m))
        # 정확하게 맞아 떨어지면 값 return(없을 경우 None이 return)
        if m ** k == n:
            return [m, k]
    return None
 
# Warriors Code
def isPP_(n):
    for i in range(2int(n**.5+ 1):
        number = n
        times = 0
        while number % i == 0:
            number /= i
            times += 1
            if number == 1:
                return [i, times]
    return None
 
if __name__=='__main__':
    answer = isPP(4)
    print(answer)
    answer = isPP(9)
    print(answer)
    answer = isPP(5)
    print(answer)
 
반응형