コード例 #1
0
def main():
    
    start = time()
    primeSet = set([])
    solutionSet = set([])
    
    for i in xrange(2, LIMIT + 1):
        if isPrime(i):
           primeSet.add(str(i)) 
               
    for prime in primeSet:
        tempPrime = prime
        isSolution = True
        for iteration in xrange(len(prime) - 1):
            tempPrime = tempPrime[1:] + tempPrime[0]
            if tempPrime not in primeSet:
                isSolution = False
                break
        if isSolution:
            solutionSet.add(prime)
    
    print "Solution set size: ", len(solutionSet)
    
    end = time()
    print "Runtime: ", end - start, " seconds."
コード例 #2
0
def main():

    start = time()
    primeSet = set([])
    solutionSet = set([])

    for i in xrange(2, LIMIT + 1):
        if isPrime(i):
            primeSet.add(str(i))

    for prime in primeSet:
        tempPrime = prime
        isSolution = True
        for iteration in xrange(len(prime) - 1):
            tempPrime = tempPrime[1:] + tempPrime[0]
            if tempPrime not in primeSet:
                isSolution = False
                break
        if isSolution:
            solutionSet.add(prime)

    print "Solution set size: ", len(solutionSet)

    end = time()
    print "Runtime: ", end - start, " seconds."
コード例 #3
0
def main():
    
    start = time()
   
    primeSet    = set([])
    solutionSet = set([])
    candidate   = str(11)           # skip 2,3,5,7 as per the question.
    
    while len(solutionSet) != SOLUTION_LIMIT:
                
        if checkCandidateWorthiness(candidate):
            
            
            isPrimeFromLeftTruncation = True
            
            for number in produceNumbersFromLeftTruncation(candidate):
                if number not in primeSet:
                    if isPrime(number):
                        primeSet.add(number)
                    else:
                        isPrimeFromLeftTruncation = False
                        break
            
            isPrimeFromRightTruncation = True
    
            if isPrimeFromLeftTruncation:
                for number in produceNumbersFromRightTruncation(candidate):
                    if number not in primeSet:
                        if isPrime(number):
                            primeSet.add(number)
                        else:
                            isPrimeFromRightTruncation = False
                            break
            
            if isPrimeFromLeftTruncation and isPrimeFromRightTruncation:
                solutionSet.add(int(candidate))
                
        candidate = str(int(candidate) + 1)
        
    end = time()
    
    print "Solutions: ", solutionSet
    print "Sum: ", sum(solutionSet)
    print "Runtime: ", end - start, " seconds."
コード例 #4
0
def main():

    start = time()

    primeSet = set([])
    solutionSet = set([])
    candidate = str(11)  # skip 2,3,5,7 as per the question.

    while len(solutionSet) != SOLUTION_LIMIT:

        if checkCandidateWorthiness(candidate):

            isPrimeFromLeftTruncation = True

            for number in produceNumbersFromLeftTruncation(candidate):
                if number not in primeSet:
                    if isPrime(number):
                        primeSet.add(number)
                    else:
                        isPrimeFromLeftTruncation = False
                        break

            isPrimeFromRightTruncation = True

            if isPrimeFromLeftTruncation:
                for number in produceNumbersFromRightTruncation(candidate):
                    if number not in primeSet:
                        if isPrime(number):
                            primeSet.add(number)
                        else:
                            isPrimeFromRightTruncation = False
                            break

            if isPrimeFromLeftTruncation and isPrimeFromRightTruncation:
                solutionSet.add(int(candidate))

        candidate = str(int(candidate) + 1)

    end = time()

    print "Solutions: ", solutionSet
    print "Sum: ", sum(solutionSet)
    print "Runtime: ", end - start, " seconds."
コード例 #5
0
def checkCandidateWorthiness(number):
    if '0' in number:
        return False
    if number[-1] not in SINGLE_DIGIT_PRIME_SET:
        return False
    if number[0] not in SINGLE_DIGIT_PRIME_SET:
        return False
    if number[:2] not in DOUBLE_DIGIT_PRIME_SET:
        return False
    if not isPrime(number):
        return False
    return True
コード例 #6
0
def checkCandidateWorthiness(number):
    if '0' in number:
        return False
    if number[-1] not in SINGLE_DIGIT_PRIME_SET:
        return False
    if number[0] not in SINGLE_DIGIT_PRIME_SET:
        return False
    if number[:2] not in DOUBLE_DIGIT_PRIME_SET:
        return False
    if not isPrime(number):
        return False
    return True
コード例 #7
0
def main():

    start = time()

    primeSet = set([])
    squaredDict = {}

    candidate = 11  # 3, 5, 7 are prime. 9 is given to not break the conjecture
    solution = 0

    while solution == 0:
        if candidate not in primeSet and not isPrime(candidate):
            createdUsingPrime = False
            for i in xrange(1, int(ceil(
                    sqrt(candidate /
                         2)))):  # only check natural numbers for primality

                if i not in squaredDict:
                    squaredDict[i] = 2 * i**2

                remainderValue = candidate - squaredDict[i]

                if remainderValue in primeSet or isPrime(remainderValue):
                    primeSet.add(remainderValue)
                    createdUsingPrime = True
                    break

            if not createdUsingPrime:
                solution = candidate

        else:
            primeSet.add(candidate)

        candidate += 2

    print "Smallest odd, composite number that breaks the conjecture: ", solution

    end = time()

    print "Runtime: ", end - start, " seconds."
コード例 #8
0
def main():
    
    start = time()
    
    primeSet = set([])
    squaredDict = {}

    candidate   = 11     # 3, 5, 7 are prime. 9 is given to not break the conjecture
    solution    = 0
    
    while solution == 0:
        if candidate not in primeSet and not isPrime(candidate):
            createdUsingPrime = False
            for i in xrange(1, int(ceil(sqrt(candidate / 2)))): # only check natural numbers for primality
                
                if i not in squaredDict:
                    squaredDict[i] = 2 * i ** 2              
                
                remainderValue = candidate - squaredDict[i]
                                
                if remainderValue in primeSet or isPrime(remainderValue):
                    primeSet.add(remainderValue)
                    createdUsingPrime = True
                    break

            
            if not createdUsingPrime:
                solution = candidate
                
        else:
            primeSet.add(candidate)
        
        candidate += 2
            
    print "Smallest odd, composite number that breaks the conjecture: ", solution     
        
    end = time()
    
    print "Runtime: ", end - start, " seconds."
コード例 #9
0
ファイル: 49.py プロジェクト: kaizensoze/project-euler
def run():
    primes = set([x for x in range(1000, 9999+1) if isPrime(x)])
    primesByPerm = {}
    groups = []
    for x in primes:
        primesByPerm[x] = identity(x)

    for v in primesByPerm.values():
        blah = sorted([x for x in primesByPerm.keys() \
                                           if primesByPerm[x] == v])
        if blah not in groups:
            groups.append(blah)

    for x in groups:
        if len(x) >= 3:
            #print(x)
            constDiffCheck(x)
コード例 #10
0
ファイル: 50.py プロジェクト: kaizensoze/project-euler
'''
Created on Jun 12, 2009

@author: anon
'''

import math
from ProjectEulerLibrary import getPrimes, isPrime

n = 1000000
primes = getPrimes(math.ceil(n/2))

maxLength = 0
maxLengthSum = 0

for i in range(len(primes)):
    sum = 0
    length = 0
    for y in primes[i:]:
        sum += y
        if sum >= n:
            break
        if isPrime(sum) and length > maxLength:
            maxLengthSum = sum
            maxLength = length
        length += 1
               
print(maxLengthSum)
コード例 #11
0
ファイル: 41.py プロジェクト: kaizensoze/project-euler
'''
Created on Apr 12, 2009

@author: anon
'''
from ProjectEulerLibrary import isPrime, isPandigital

for n in range(1,9000000):
    if isPandigital(n) and isPrime(n):
        print(n)
コード例 #12
0
ファイル: 37.py プロジェクト: kaizensoze/project-euler
'''
Created on Apr 17, 2009

@author: anon
'''
from ProjectEulerLibrary import isPrime

truncatablePrimes = []

for n in range(10,740000+1):
    if isPrime(n):
        prime = True
        for x in range(len(str(n))):
            left = [int(y) for y in str(n)]
            right = [int(y) for y in str(n)]
            
            for y in range(x+1):
                left.pop(0)
                right.pop(-1)
            
            if len(left) == 0 or len(right) == 0:
                break
            
            if len(left) == 1:
                left = left[0]
            else:
                left = int(''.join([str(y) for y in left]))
                
            if len(right) == 1:
                right = right[0]
            else:
コード例 #13
0
ファイル: 46.py プロジェクト: kaizensoze/project-euler
from ProjectEulerLibrary import isPrime

# find odd non-prime that != prime + 2*square

# iterate on odd non-prime numbers starting with... 9
# 2*2 + 5

done = False

compositeOdd = (x for x in range(3,10000000000) if x%2 == 1 and not isPrime(x))

while not done:
    canBeWritten = False
    x = next(compositeOdd)
    i = 1
    amt = 2*(i**2)
    while amt <= x:
        print('x: ', x)
        print('amt: ', amt)
        if isPrime(x - amt):
            canBeWritten = True
            break
        i += 1
        amt = 2*(i**2)
    if not canBeWritten:
        print(x)
        done = True
コード例 #14
0
ファイル: 27.py プロジェクト: kaizensoze/project-euler
'''
Created on Apr 12, 2009

@author: anon
'''
from ProjectEulerLibrary import isPrime

maxPrimeCount = 0
coeffProd = 1
lastWasPrime = True

for b in [x for x in range(1,1000) if isPrime(x)]:
    for a in range(-999, 999):
        primeCount = 0
        n = 0
        while isPrime(n**2 + a*n + b):
            primeCount = primeCount + 1
            n = n + 1
            
        if primeCount > maxPrimeCount:
            maxPrimeCount= primeCount
            coeffProd = a * b
            
        print('a=%s b=%s' % (a,b))

print(coeffProd)
コード例 #15
0
ファイル: 58.py プロジェクト: kaizensoze/project-euler
'''
Created on Apr 18, 2009

@author: anon
'''
from ProjectEulerLibrary import isPrime

primeCount = 0
compositeCount = 0
n = 1

skipAmount = 1

while skipAmount <= 100000:
    for i in range(4):
        n = n + (skipAmount + 1)
        if isPrime(n):
            primeCount = primeCount + 1
        else:
            compositeCount = compositeCount + 1
    ratio = primeCount / (compositeCount + primeCount) 
    if ratio * 100 < 10:
        print(skipAmount)
        break
    skipAmount = skipAmount + 2