Coding/Python 삽질기2020. 7. 10. 00:07
import math

def get_prime_factor(number):
    result = []

    if number < 2:
        return result

    while number % 2 == 0:
        result.append('2')
        number /= 2

    for i in range (3, int(math.sqrt(number)) + 1, 2):
        if number % i == 0:
            result.append(str(i))
            number /= i

    if number > 2:
        result.append(str(int(number)))

    return result

def testPrimeFactor():
    assert len(get_prime_factor(-1)) == 0
    assert "2x2x3" == 'x'.join(get_prime_factor(12))
    assert "2x2x5x5" == 'x'.join(get_prime_factor(100))
    assert "67x337x2797x14251" == 'x'.join(get_prime_factor(900000001213))

if __name__ == '__main__':
    testPrimeFactor()

'Coding > Python 삽질기' 카테고리의 다른 글

강아지 나이 계산기  (0) 2020.07.16
[ChoboTimer] Version 0.17 Release  (0) 2020.04.22
[Python] venv 사용  (0) 2020.04.15
Posted by chobocho