這是一個質因數分解的程式。比目前你網路上能找到的 python 例子簡短很多。這是跟同學慢慢討論、一邊研究數論出來的。

def getPrime(number):
    result = []
    # 先將質數2去除

    while number % 2 == 0:
        number = number / 2
        result.append(2)
    current = 3
    while number != 1:
        while number % current == 0:
                number  = number / current
                result.append(current)
        current = current + 2
    return result

Comments

comments powered by Disqus