Example #1
0
def answer(limit=1000000):
    _refresh(limit // pow(10, sqrt(log10(limit)) - 1))
    for length in range(int(sqrt(limit)), 0, -1):
        for end in range(len(prime_list), length - 1, -1):
            candidate = sum(prime_list[end - length:end])
            if candidate < limit and isprime(candidate):
                return candidate
Example #2
0
def answer(limit=1000000):
    _refresh(limit // pow(10, sqrt(log10(limit))-1))
    for length in range(int(sqrt(limit)), 0, -1):
        for end in range(len(prime_list), length-1, -1):
            candidate = sum(prime_list[end-length:end])
            if candidate < limit and isprime(candidate):
                return candidate
Example #3
0
File: 037.py Project: sanand0/euler
def is_right_truncatable(l):
    is_truncatable = 1
    for size in xrange(0, len(l)):
        n = num(l[size:])
        prime._refresh(int(math.sqrt(n)))
        if not prime._isprime(n):
            is_truncatable = 0
            break
    return is_truncatable
Example #4
0
File: 037.py Project: sanand0/euler
def is_left_truncatable(l):
    is_truncatable = 1
    for size in xrange(1, len(l)+1):
        n = num(l[:size])
        prime._refresh(int(math.sqrt(n)))
        if not prime._isprime(n):
            is_truncatable = 0
            break
    return is_truncatable
Example #5
0
def answer(limit=1000000):
    global prime_list
    _refresh(limit)

    count = 0
    for candidate in prime_list:
        if all(map(isprime, rotations(candidate))):
            count += 1
    return count
Example #6
0
def answer(limit=1000000):
    global prime_list
    _refresh(limit)

    count = 0
    for candidate in prime_list:
        if all(map(isprime, rotations(candidate))):
            count += 1
    return count
Example #7
0
def p37():
    start, answer, count = 1, 0, 0
    while True:
        for i, v in enumerate(prime.prime_list[start + 1 : :], start=start + 1):
            if v > 7 and is_left_truncatable(v) and is_right_truncatable(v):
                answer += v
                count += 1
                if count == 11:
                    return answer
        start = i
        prime._refresh(v * 2)
Example #8
0
File: 041.py Project: sanand0/euler
'''
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.

What is the largest n-digit pandigital prime that exists?
'''

import prime
from combinatorics import permutations

# Pan-digital primes are 4 or 7 digits. Others divisible by 3
prime._refresh(2766)    # sqrt(7654321)
for perm in permutations(range(7, 0, -1)):
    num = 0
    for n in perm: num = num * 10 + n
    if prime._isprime(num):
        print num
        break
Example #9
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import prime

max_pair = (0, 0, 0)
for a in xrange(-999, 1000):
    for b in xrange(max(2, 1 - a), 1000):  # b >= 2, a + b + 1 >= 2
        n, count = 0, 0
        while True:
            v = n * n + a * n + b
            prime._refresh(v)
            if prime.isprime(v):
                count = count + 1
            else:
                break
            n = n + 1
        if count > max_pair[2]:
            max_pair = (a, b, count)

print max_pair[0] * max_pair[1]
Example #10
0
#!/usr/bin/python3

import prime

MAX = 10000
prime._refresh(MAX)
squares = dict.fromkeys((x*x for x in range(1, MAX)), 1)

for x in range(35, MAX, 2):
    if not prime.isprime(x):
        is_goldbach = 0
        for p in prime.prime_list:
            if p >= x: break
            key = (x-p)/2
            if key in squares:
                is_goldbach = 1
                break
        if not is_goldbach:
            print(x)
            break

Example #11
0
#!/usr/bin/python

import prime, string
prime._refresh(80000)

def is_8_prime_family(p, d):
    c = 0
    for r in '0123456789':
        np = int(string.replace(p, d, r))
        if(np > 100000 and np < 999999 and prime.isprime(np)): c += 1
    return c==8

n=9000
while(True):
    n += 1
    p = prime.prime(n)
    if p < 100000: continue
    if p > 999999: break
    ps = str(p)
    ld = ps[5:6]
    if (ps.count('0')==3 and is_8_prime_family(ps, '0')) or (ps.count('1')==3 and ld!='1' and is_8_prime_family(ps, '1')) or \
        (ps.count('2')==3 and is_8_prime_family(ps, '2')):
        print "Answer: %s %s" % (n, ps)
        break
Example #12
0
import prime

MAX = 28124
prime._refresh(MAX/2)
abundants = [n for n in xrange(1, MAX) if sum(prime.all_factors(n)) > n+n]
abundants_dict = dict.fromkeys(abundants, 1)

total = 0
for n in xrange(1, MAX):
    sum_of_abundants = 0
    for a in abundants:
        if a > n: break
        if abundants_dict.get(n - a):
            sum_of_abundants = 1
            break
    if not sum_of_abundants:
        total = total + n

print total
Example #13
0
File: 049.py Project: sanand0/euler
'''
The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.

There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.

What 12-digit number do you form by concatenating the three terms in this sequence?

'''

import prime
from combinatorics import permutations

prime._refresh(10000)
for num in xrange(1000, 10000):
    if str(num).find('0') >= 0: continue

    if prime.isprime(num):
        prime_permutations = { num: 1 }
        for x in permutations(list(str(num))):
            next_num = int(''.join(x))
            if prime.isprime(next_num):
                prime_permutations[next_num] = 1

        primes = sorted(prime_permutations.keys())
        for a in xrange(0, len(primes)):
            if primes[a] == 1487: continue
            for b in xrange(a+1, len(primes)):
                c = (primes[a] + primes[b]) / 2
                if prime_permutations.has_key(c):
                    print str(primes[a]) + str(c) + str(primes[b])
                    exit()
Example #14
0
def answer(num):
    """ returns the sum of all primes below num """
    _refresh(num)
    return sum(prime_list)
Example #15
0
File: 023.py Project: sanand0/euler
'''
A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.

A number whose proper divisors are less than the number is called deficient and a number whose proper divisors exceed the number is called abundant.

As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
'''

import prime

MAX = 28124
prime._refresh(MAX/2)
abundants = [n for n in xrange(1, MAX) if sum(prime.all_factors(n)) > n+n]
abundants_dict = dict.fromkeys(abundants, 1)

total = 0
for n in xrange(1, MAX):
    sum_of_abundants = 0
    for a in abundants:
        if a > n: break
        if abundants_dict.get(n - a):
            sum_of_abundants = 1
            break
    if not sum_of_abundants:
        total = total + n

print total
Example #16
0
File: 027.py Project: sanand0/euler
It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible by 41, and certainly when n = 41, 41^2 + 41 + 41 is clearly divisible by 41.

Using computers, the incredible formula  n^2 - 79n + 1601 was discovered, which produces 80 primes for the consecutive values n = 0 to 79. The product of the coefficients, -79 and 1601, is -126479.

Considering quadratics of the form:

n^2 + an + b, where |a| <= 1000 and |b| <= 1000

where |n| is the modulus/absolute value of n
e.g. |11| = 11 and |4| = 4
Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0.

'''

import prime

max_pair = (0,0,0)
for a in xrange(-999, 1000):
    for b in xrange(max(2, 1-a), 1000): # b >= 2, a + b + 1 >= 2
        n, count = 0, 0
        while True:
            v = n*n + a*n + b
            prime._refresh(v)
            if prime.isprime(v): count = count + 1
            else: break
            n = n + 1
        if count > max_pair[2]:
            max_pair = (a,b,count)

print max_pair[0] * max_pair[1]
Example #17
0
File: 058.py Project: sanand0/euler
'''
Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length 7 is formed.

37 36 35 34 33 32 31
38 17 16 15 14 13 30
39 18  5  4  3 12 29
40 19  6  1  2 11 28
41 20  7  8  9 10 27
42 21 22 23 24 25 26
43 44 45 46 47 48 49

It is interesting to note that the odd squares lie along the bottom right diagonal, but what is more interesting is that 8 out of the 13 numbers lying along both diagonals are prime; that is, a ratio of 8/13 ~ 62%.

If one complete new layer is wrapped around the spiral above, a square spiral with side length 9 will be formed. If this process is continued, what is the side length of the square spiral for which the ratio of primes along both diagonals first falls below 10%?
'''

import prime
prime._refresh(50000)

width, diagonal, base, primes = 1, 1, 1, 0
while True:
    width = width + 2
    increment = width - 1
    for i in xrange(0, 4):
        diagonal = diagonal + increment
        if i < 3 and prime._isprime(diagonal): primes += 1
    base = base + 4
    if primes * 10 < base:
        print width
        break