コード例 #1
0
ファイル: problem60.py プロジェクト: pz325/ProjectEuler
def solution():
    N = 10000
    primes = []
    for p in prime_start():
        primes.append(p)
        if p > N:
            break

    print(len(primes), 'primes generated')
    lowSum = 5 * primes[-1]

    pairs = {}

    startTime = time.time()
    # a, b, c, d, e are primes array index
    for a in range(1, len(primes)):
        if primes[a] * 5 > lowSum: break
        if primes[a] not in pairs:
            pairs[primes[a]] = findPairs(a, primes)

        for b in range(a+1, len(primes)):
            if primes[a] + primes[b] * 4 > lowSum: break
            if primes[b] not in pairs[primes[a]]: 
                # print(primes[a], primes[b], 'no match found')
                continue
            if primes[b] not in pairs:
                pairs[primes[b]] = findPairs(b, primes)

            for c in range(b+1, len(primes)):
                if primes[a] + primes[b] + primes[c] * 3 > lowSum: break
                if primes[c] not in pairs[primes[a]] or primes[c] not in pairs[primes[b]]: 
                    continue
                if primes[c] not in pairs:
                    pairs[primes[c]] = findPairs(c, primes)

                for d in range(c+1, len(primes)):
                    if primes[a] + primes[b] + primes[c] + primes[d] * 2 > lowSum: break
                    if primes[d] not in pairs[primes[a]] or primes[d] not in pairs[primes[b]] or primes[d] not in pairs[primes[c]]: 
                        continue
                    if primes[d] not in pairs:
                        pairs[primes[d]] = findPairs(d, primes)

                    for e in range(d+1, len(primes)):
                        if primes[a] + primes[b] + primes[c] + primes[d] + primes[e] > lowSum: break
                        if primes[e] not in pairs[primes[a]] or primes[e] not in pairs[primes[b]] or primes[e] not in pairs[primes[c]] or primes[e] not in pairs[primes[d]]: continue
                        if lowSum > primes[a] + primes[b] + primes[c] + primes[d] + primes[e]:
                            lowSum = primes[a] + primes[b] + primes[c] + primes[d] + primes[e]                        
                        print('found', primes[a], primes[b], primes[c], primes[d], primes[e])

    print('search accomplished in {0} seconds'.format(time.time() - startTime))
    return lowSum
コード例 #2
0
ファイル: problem123.py プロジェクト: pz325/ProjectEuler
def bruteForce(target):
    n = 1
    for p in util.prime_start():
        # reminder is always 2 when n is even number
        if n % 2 == 0:
            n += 1
            continue

        r = calculateR_v2(p, n)
        if r > target:
            break

        n += 1
    
    return n
コード例 #3
0
ファイル: problem77.py プロジェクト: pz325/ProjectEuler
def solution():
    N = 5000
    primes = []
    for p in prime_start():
        primes.append(p)
        target = p

        ways = [0] * (target + 1)
        ways[0] = 1

        for i in primes:
            for j in range(i, target + 1):
                ways[j] += ways[j - i]
                if ways[j] > N:
                    return j
コード例 #4
0
ファイル: problem70.py プロジェクト: pz325/ProjectEuler
def solution():
    primes = []
    for p in prime_start(2000):
        if p > 4500: break
        primes.append(p)
    
    N = 10000000
    minResult = N
    index = 0
    for i in range(0, len(primes)):
        for j in range(i, len(primes)):
            n = primes[i] * primes[j]
            if n > N: break
            phi_n = (primes[i] - 1) * (primes[j] - 1)
            ratio = float(n) / float(phi_n)

            if isPermutation(n, phi_n) and ratio < minResult:
                minResult = ratio
                index = n
                print('found {}, phi_i: {}, min: {}, prime factors: {}'.format(n, phi_n, minResult, (primes[i], primes[j])))
    return index