Пример #1
0
def solve(N, ceiling):
    primes = sieve_eratosthenes(ceiling)
    print('Sieve prepared')
    mp = {}
    K = int(math.log10(ceiling))

    for k in range(2, K):
        print('k=' + str(k))
        for p in primes:
            for starred_number in transform_starred(p, k):
                try:
                    mp[starred_number].append(p)
                except KeyError, e:
                    mp[starred_number] = [p]
Пример #2
0
def solve(N):
    result = 0 #@UnusedVariable
    primes = sieve_eratosthenes(N)
    print('Number of primes: ' + str(len(primes)))
    
    start = time.clock()
    max_consecutive_primes = [2]
    len_max_consecutive_primes = len(max_consecutive_primes)

    # special treatment for prime sequences starting with 2
    s = 0
    for i in range(0, len(primes)):
        s += primes[i]
        if s in primes:
            max_consecutive_primes = primes[:i+1]
            len_max_consecutive_primes = len(max_consecutive_primes)            
        if s > N: 
            break
    print(len_max_consecutive_primes, sum(max_consecutive_primes), max_consecutive_primes)
    
    # all following sequence lengths must be odd
    if len_max_consecutive_primes % 2 == 0:
        len_max_consecutive_primes += 1

    for i in range(1, len(primes)):
        cons_primes = primes[i:i + len_max_consecutive_primes + 2] 
        cons_sum = sum(cons_primes)

        for j in range(i+ len_max_consecutive_primes + 2, len(primes)-1, 2):
            cons_primes += primes[j:j+2]
            cons_sum += primes[j] + primes[j+1]

            if cons_sum > N:
                break
            if cons_sum in primes:
                max_consecutive_primes = [p for p in cons_primes]
                print(len_max_consecutive_primes, sum(max_consecutive_primes), max_consecutive_primes)
        if i % 5000 == 0:
            print(i, time.clock() - start)
        
    result = sum(max_consecutive_primes)
    print(time.clock() - start)
        
    
    print("The highest prime sum of the most consecutive primes below %(N)d is %(result)d" % vars())
Пример #3
0
def solve(N):
    primes = sieve_eratosthenes(lsqrt(N)*2)
    print_sieve_stats(primes)
    min_frac = N
    min_number = N
    for step in range(1, 500):
        for (p, q) in [(primes[i], primes[i-step]) for i in range(len(primes)-1, step-1, -1)]:
            for (p_power, q_power) in zip(powers_below(p, N), powers_below(q, N)):
                n = p_power*q_power
                if n > N:
                    continue
        
                phi = (p_power - p_power/p)*(q_power - q_power/q)
                if permutation.is_permuted(n, phi):
                    frac = float(n)/phi
                    if frac < min_frac:
                        print(n, p, q, phi, frac, step)
                        min_frac = frac
                        min_number = n
    return min_number
    if n1s == n2s: return True
    return False

# Big primes will be the closest to 1 for p/phi(p), since the 
# difference between p and phi(p) is pretty much negligible, but 
# primes will never be permutations of their phi(p) values.
# product of two primes should still be relatively close to 1 for n/phi(n)
# compared to normal numbers.
# phi(p) = p-1 and phi(pq) = phi(p) * phi(q) so products of primes is good for
# the math too.
# Obviously we need to keep our products of primes below the limit,
# and the bigger the primes, the smaller the n/phi(n).

limit = 10**7
minimum = 1000000
ans = 0
primes = sieve_eratosthenes(int((limit**0.5)*2))
for pp in product(primes[100:], repeat=2):
    n = pp[0]*pp[1]
    if n > limit: continue
    phi_n = (pp[0]-1)*(pp[1]-1)
    if compare(n,phi_n):
        div = float(n)/phi_n
        if div < minimum:
            minimum = div
            ans = n

print ans
print time()-start

Пример #5
0
def solve(D):
    primes = sieve_eratosthenes(D)
    factorizations = list(factorizations_below(D, primes))
    assert len(factorizations)+1 == D 
    return sum([phi_of_factorization(factorization) for factorization in factorizations])
Пример #6
0
def solve(D, i):
    primes = sieve_eratosthenes(D)
    l = list(factorizations_below(D, primes)) + [((1,1),)]
    E = sorted([(num(factors), rad(factors)) for factors in l], cmp_rad)
    return E[i-1][0]
Пример #7
0
"""

from sieve import sieve_eratosthenes
from projecteuler import list_to_dict
from itertools import product
from itertools import takewhile


def quadratics(a,b, start):
    # produces sequence: n^2 + an + b
    n = start
    while(1):
        yield n**2 + a*n + b
        n += 1


if __name__ == '__main__':
    primes = list_to_dict(sieve_eratosthenes(10**6), True)
    print("Prepared sieve")
        
    (ma, mb, mn) = (0,0,0)
    for (a, b) in product(range(-1000,1000), repeat=2):
            if not (mn**2 + a*mn + b) in primes:
                continue
            consecutive_primes = list(takewhile(lambda q: q in primes, quadratics(a,b, 0)))
            if(len(consecutive_primes) > mn):
                (ma, mb, mn) = (a, b, len(consecutive_primes))
    
    result = ma*mb
    print("The coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n are (%(ma)d, %(mb)d)" % vars())    
    print("Their product is %(result)d" % vars())