當前位置: 首頁>>代碼示例>>Python>>正文


Python Euler.previous_prime方法代碼示例

本文整理匯總了Python中Euler.previous_prime方法的典型用法代碼示例。如果您正苦於以下問題:Python Euler.previous_prime方法的具體用法?Python Euler.previous_prime怎麽用?Python Euler.previous_prime使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Euler的用法示例。


在下文中一共展示了Euler.previous_prime方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: evaluate_prime

# 需要導入模塊: import Euler [as 別名]
# 或者: from Euler import previous_prime [as 別名]
def evaluate_prime(prime):
    # Evaluates a prime number and adds a key: value pair to prime_table
    # with a list containing each set of pairs for that prime organized by
    # number of pairs

    candidate = Euler.previous_prime(prime)
    answer = []                                 # This will be the value we return
    finished = False
    pairs = [prime]
    while finished == False:
        while candidate > 2:
            if is_pair(prime, candidate) == True:
                pairs.append(candidate)
            candidate -= 2
        if len(pairs) > 1:
            answer.append((len(pairs), pairs))
            candidate = Euler.previous_prime(pairs[-1])
            pairs = pairs[:-1]
        if len(pairs) < 2:
            finished = True
    if len(answer) == 0:
        prime_table[prime] = False
    else:
        prime_table[prime] = answer
開發者ID:Grae-Drake,項目名稱:Python_Euler,代碼行數:26,代碼來源:Problem_60.py

示例2: int

# 需要導入模塊: import Euler [as 別名]
# 或者: from Euler import previous_prime [as 別名]
    # Problem 70: Totient permutation
    # candidate: 7026037

import time, Euler, math
t1 = time.clock()

limit = 10000000
root = Euler.previous_prime(int(math.sqrt(limit)))
# 3137 is the largest prime the square of which is < 10,000,000,
# Equivalent to Euler.previous_prime(int(math.sqrt(10000000)))

primes = {0: root}
large_prime_counter = 0
small_prime_counter = 0
composites = {}
large = root
small = root
while small > 2:
    composite = large*small

        # Deal with the case where composite is less than limit
    if composite < limit:
        
        phi = composite - (large + small - 1)

        # Put the composite in the dictionary if it's a permutation
        if (int("".join(sorted([x for x in str(composite)]))) ==
            int("".join(sorted([x for x in str(phi)])))):
            composites[composite] = [large, small, phi]

         # Increase large and add it to primes if necessary   
開發者ID:Grae-Drake,項目名稱:Python_Euler,代碼行數:33,代碼來源:Problem_70.py


注:本文中的Euler.previous_prime方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。