算法基礎

基本數學類

基本的初等數學類python

  • 閏年判斷
  • 找零錢
  • 斐波那契數列
  • 最大公約數
  • 公倍數
  • 素因子分解

閏年判斷

def isLeapYear(year):
    return (not year % 4 and year % 100) or (not year % 400)

找零錢

def mod(money):
    cent = int(money * 100)
    all_cent = {25: 0, 10: 0, 5: 0, 1: 0}
    for k in all_cent:
        all_cent[k], cent = divmod(cent, k)
    return all_cent

最大公約數,展轉相除法

def maxCommonDivisor(m, n):
    while True:
        remainder = m % n
        if not remainder:
            return n
        else:
            m, n = n, remainder

最小公倍數

def maxCommonMultiple(m, n):
    return m * n / maxCommonDivisor(m, n)

素數的判斷

def isprime(n):
    result = True
    for i in range(n / 2, 1, -1):
        if n % i == 0:
            result = False
            break
    return result

獲取n的全部因子

def getfactors(n):
    result = [n]
    for i in range(n / 2, 0, -1):
        if n % i == 0:
            result.append(i)
    return result

素因子分解

def decompose(n):
    all_factors = getfactors(n)
    all_factors.remove(1)
    all_factors.remove(n)
    prime_factors = [x for x in all_factors if isprime(x)]
    prime_factors.sort(reverse=True)
    result = []
    remainder = n
    for f in prime_factors:
        while remainder >= f:
            qut, rem = divmod(remainder, f)
            if rem != 0:
                break
            else:
                remainder = qut
                result.append(f)
    return result

獲取前N個斐波那契數列

def fibonacci(n):
    result = []
    if n == 1:
        result.append(1)
    elif n >= 2:
        result.append(1)
        result.append(1)
        for i in range(2, n):
            result.append(result[-1] + result[-2])
    return result
相關文章
相關標籤/搜索