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


Python Euler.nextPrime方法代碼示例

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


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

示例1: main

# 需要導入模塊: import Euler [as 別名]
# 或者: from Euler import nextPrime [as 別名]
def main(n):

	# Returns the nth prime number.
	counter = 0
	testnum = 1
	while counter < n:
		testnum = Euler.nextPrime(testnum)
		counter += 1
	return testnum
開發者ID:Grae-Drake,項目名稱:Python_Euler,代碼行數:11,代碼來源:Problem_7.py

示例2: permutePrimes

# 需要導入模塊: import Euler [as 別名]
# 或者: from Euler import nextPrime [as 別名]
def permutePrimes(prime, character):
    # Replaces each instance of character in prime with 0-9 and
    # returns a list of each resulting number that is also prime
    digits = [x for x in range(10)]
    digits = list(filter(lambda x: x != character, digits))
    answers = [prime]
    for x in digits:
        testnum = replaceDigit(prime, character, x)
        if Euler.isPrime(testnum) == True:
            if len(str(testnum)) == len(str(prime)):
                answers.append(testnum)
    return answers

counter = 0
answer = 0
permutations = []
while answer == 0:
    counter = Euler.nextPrime(counter)
    for x in range(10):
        if digitCount(counter,x) > 1:
            if len(permutePrimes(counter, x)) > 7:
                   permutations = permutePrimes(counter, x)
                   answer = permutations[0]
    
print("The answer is " + str(answer))
print("The primes are: " + str(permutations))

t2 = time.clock()
print("Execution time = " + str(t2-t1)[:5] + " seconds")
開發者ID:Grae-Drake,項目名稱:Python_Euler,代碼行數:31,代碼來源:Problem_51.py

示例3: reversed

# 需要導入模塊: import Euler [as 別名]
# 或者: from Euler import nextPrime [as 別名]
        # Problem 46: Goldbach's Other Conjecture
        
        # Find the smallest odd composite number that cannot be written as
        # the sum of a prime and twice a square.

import time, Euler
t1 = time.clock()

primes = [2,3,5,7,11]
candidate = 9
answer = 0
while True:
    if candidate == primes[-1]:
        primes.append(Euler.nextPrime(candidate))
        candidate += 2
    else:
        answer_found = False
        goldbach = False
        for prime in reversed(primes[:-1]):
            counter = 1
            while goldbach == False:
                result = (prime + (2 * (counter**2)))
                if result == candidate:
                    goldbach = True
                    candidate += 2
                elif result < candidate:
                    counter += 1
                else:
                    break
            if goldbach == True:
                break
開發者ID:Grae-Drake,項目名稱:Python_Euler,代碼行數:33,代碼來源:Problem_46.py

示例4: int

# 需要導入模塊: import Euler [as 別名]
# 或者: from Euler import nextPrime [as 別名]
    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   
        large_prime_counter +=1
        if large_prime_counter not in primes:
            primes[large_prime_counter] = Euler.nextPrime(primes[large_prime_counter-1])
        large = primes[large_prime_counter]

        # Deal with the case where composite is too big
    else:
        small_prime_counter -= 1
        large_prime_counter = small_prime_counter +1
        primes[small_prime_counter] = Euler.previous_prime(primes[small_prime_counter+1])
        small = primes[small_prime_counter]
        large = primes[large_prime_counter]

min_ratio = 3
answer = 0
for key, value in composites.items():
    n = key
    phin = value[2]
開發者ID:Grae-Drake,項目名稱:Python_Euler,代碼行數:33,代碼來源:Problem_70.py


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