Пример #1
0
Файл: 050.py Проект: 3kwa/euler
def prime_sum_of_primes_generator(maximum):
    """
    >>> sop100 = prime_sum_of_primes_generator(100)
    >>> sop100.next()
    [2, 3]
    >>> max( (l for l in sop100), key=lambda l : len(l) )
    [2, 3, 5, 7, 11, 13]
    """
    primes = list(generator(maximum / 2))
    count = len(primes)
    sum_of_primes = []
    for start, prime in enumerate(primes):
        for stop in range(start + 1, count):
            consecutive = primes[start:stop]
            sum_ = sum(consecutive)
            if sum_ > maximum:
                break
            if is_prime(sum_) and len(consecutive) > 1:
                yield consecutive
Пример #2
0
Файл: 050.py Проект: 3kwa/euler
def prime_sum_of_primes_generator(maximum):
    """
    >>> sop100 = prime_sum_of_primes_generator(100)
    >>> sop100.next()
    [2, 3]
    >>> max( (l for l in sop100), key=lambda l : len(l) )
    [2, 3, 5, 7, 11, 13]
    """
    primes = list( generator(maximum/2) )
    count = len(primes)
    sum_of_primes = []
    for start, prime in enumerate(primes):
        for stop in range(start + 1, count):
            consecutive = primes[start:stop]
            sum_ = sum(consecutive)
            if sum_ > maximum:
                break
            if is_prime(sum_) and len(consecutive) > 1:
                yield consecutive
Пример #3
0
def factors(number):
    """
    >>> factors(3)
    [3]
    >>> factors(4)
    [2]
    >>> factors(6)
    [2, 3]
    >>> factors(12)
    [2, 3]
    """
    if is_probable_prime(number):
        return [number]
    for p in generator():
        if number % p == 0:
            while number % p == 0:
                number = number / p
            if number == 1:
                return [p]
            else:
                return [p] + factors(number)
Пример #4
0
Файл: 072.py Проект: 3kwa/euler
def factors(number):
    """
    >>> factors(3)
    [3]
    >>> factors(4)
    [2]
    >>> factors(6)
    [2, 3]
    >>> factors(12)
    [2, 3]
    """
    if is_probable_prime(number):
        return [number]
    for p in generator():
        if number % p == 0:
            while number % p == 0:
                number = number / p
            if number == 1:
                return [p]
            else:
                return [p] + factors(number)
Пример #5
0
Файл: 051.py Проект: 3kwa/euler
def first_member(D, SIZE):
    """
    Returns the first member of a family of SIZE primes obtain by substituting
    the same digit D times (0 to 9)

    >>> first_member(1, 6)
    13
    >>> first_member(2, 7)
    56003
    """

    for p in generator(1000000):
        string = str(p)
        counter = Counter(string)
        if D in counter.values():
            # getting the digit
            for digit, count in counter.iteritems():
                if count == D:
                    break
            # building the family
            family = []
            for d in range(10):
                # substitute digits
                member = string.replace(str(digit), str(d))

                # skipping if start with 0
                if member[0] == '0':
                    continue

                # testing for primality
                member = int(member)
                if is_prime(member):
                    family.append(member)

            if len(family) == SIZE:
                return p
Пример #6
0
Файл: 051.py Проект: 3kwa/euler
def first_member(D, SIZE):
    """
    Returns the first member of a family of SIZE primes obtain by substituting
    the same digit D times (0 to 9)

    >>> first_member(1, 6)
    13
    >>> first_member(2, 7)
    56003
    """

    for p in generator(1000000):
        string = str(p)
        counter = Counter(string)
        if D in counter.values():
            # getting the digit
            for digit, count in counter.iteritems():
                if count == D:
                    break
            # building the family
            family = []
            for d in range(10):
                # substitute digits
                member = string.replace(str(digit), str(d))

                # skipping if start with 0
                if member[0] == '0':
                    continue

                # testing for primality
                member = int(member)
                if is_prime(member):
                    family.append(member)

            if len(family) == SIZE:
                return p
Пример #7
0
import primes

print sum(p for p in primes.generator(2000000))
Пример #8
0
Файл: 069.py Проект: 3kwa/euler
which is obtain for small values of pi

2 * 3 = 6
2 * 3 * 5 = 30
2 * 3 * 5 * 7 = 210
2 * 3 * 5 * 7 * 11  = 2310
2 * 3 * 5 * 7 * 11 * 13 = 30030
2 * 3 * 5 * 7 * 11 * 13 * 17 = ...

Smallest n under 1 000 000 giving a a max n / phi(n)
"""

from operator import mul
from primes import generator

primes = list(generator(1000000))
set_ = set(primes)


def factors(number):
    """
    >>> f = factors(4)
    >>> f.next()
    (2, 2)
    >>> list(factors(6))
    [(2, 1), (3, 1)]
    >>> list(factors(12))
    [(2, 2), (3, 1)]
    """
    for p in primes:
        if number < p / 2:
Пример #9
0
from itertools import permutations
from collections import defaultdict

from primes import generator

four_digits_prime = [p for p in generator(10000) if p > 999]

candidates = defaultdict(list)

for prime in four_digits_prime:

    # generating a set of all the permutations of the digit of prime
    permutations_of_prime = set(
        map(lambda tuple_: int(''.join(tuple_)), permutations(str(prime))))

    # for every permutation
    for permutation in permutations_of_prime:
        # check for primality, 3330 and greater than prime (key is smallest prime)
        if permutation in four_digits_prime and \
           (permutation - prime) % 3330 == 0 and \
           permutation > prime:
            candidates[prime].append(permutation)

# display the concatenated string of 3 primes satisfying the conditions
for k, v in candidates.iteritems():
    v.append(k)
    if len(v) == 3:
        print ''.join(map(str, sorted(v)))
Пример #10
0
Файл: 035.py Проект: 3kwa/euler

def is_circular_prime(number):
    """
    >>> is_circular_prime(197)
    True
    >>> is_circular_prime(8)
    False

    There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97

    >>> [number for number in generator(100) if is_circular_prime(number)]
    [2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 97]
    >>> sum(1 for number in generator(100) if is_circular_prime(number))
    13
    """
    if number not in (2, 5):
        if '5' in str(number):
            return False
        for digit in str(number):
            if int(digit) % 2 == 0:
                return False
    # the previous 6 lines make the execution time go from 65 sec to 3 sec
    return all(is_prime(i) for i in rotation(number))


if __name__ == '__main__':
    import doctest
    doctest.testmod()
    print sum(1 for number in generator(1000000) if is_circular_prime(number))
Пример #11
0
Файл: 035.py Проект: 3kwa/euler
        result.append( int(string[i:] + string[:i]) )
    return result

def is_circular_prime(number):
    """
    >>> is_circular_prime(197)
    True
    >>> is_circular_prime(8)
    False

    There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97

    >>> [number for number in generator(100) if is_circular_prime(number)]
    [2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 97]
    >>> sum(1 for number in generator(100) if is_circular_prime(number))
    13
    """
    if number not in (2, 5):
        if '5' in str(number):
            return False
        for digit in str(number):
            if int(digit) % 2 == 0:
                return False
    # the previous 6 lines make the execution time go from 65 sec to 3 sec
    return all(is_prime(i) for i in rotation(number))

if __name__ == '__main__':
    import doctest
    doctest.testmod()
    print sum(1 for number in generator(1000000) if is_circular_prime(number))
Пример #12
0
Файл: 037.py Проект: 3kwa/euler
from primes import generator, is_prime

def truncate(number):
    """
    >>> truncate(3797)
    [797, 3, 97, 37, 7, 379]
    """
    string = str(number)
    result = []
    for i in range(1, len(string)):
        result.append(int(string[i:]))
        result.append(int(string[:i]))
    return result


if __name__ == '__main__':
    import doctest
    doctest.testmod()

    g = generator()
    truncatables_primes = []
    while len(truncatables_primes) < 11:
        prime = g.next()
        if prime in (2, 3, 5, 7):
            continue
        if all(map(is_prime, truncate(prime))):
            truncatables_primes.append(prime)
    print truncatables_primes
    print sum(truncatables_primes)
Пример #13
0
"""
13 5197 5701 6733 8389 - ugly but works :P
"""

from primes import generator
from millerrabin import is_probable_prime


P = list(generator(10000))

def valid(a, b):
    s1 = str(a)+str(b)
    s2 = str(b)+str(a)
    if is_probable_prime(int(s1)) and is_probable_prime(int(s2)):
        return True
    return False

found = False
for i in P:
    for j in (p for p in P if p > i):
        if not valid(i,j):
            continue
        for k in (p for p in P if p > j):
            if not valid(i,k) or not valid(j,k):
                continue
            for l in (p for p in P if p > k):
                if not valid(i,l) or not valid(j,l) or not valid(k,l):
                    continue
                for m in (p for p in P if p > l):
                    if not valid(i,m) or not valid(j,m) or not valid(k,m) or not valid(l,m):
                        continue
Пример #14
0
from primes import generator, is_prime


def truncate(number):
    """
    >>> truncate(3797)
    [797, 3, 97, 37, 7, 379]
    """
    string = str(number)
    result = []
    for i in range(1, len(string)):
        result.append(int(string[i:]))
        result.append(int(string[:i]))
    return result


if __name__ == '__main__':
    import doctest
    doctest.testmod()

    g = generator()
    truncatables_primes = []
    while len(truncatables_primes) < 11:
        prime = g.next()
        if prime in (2, 3, 5, 7):
            continue
        if all(map(is_prime, truncate(prime))):
            truncatables_primes.append(prime)
    print truncatables_primes
    print sum(truncatables_primes)
Пример #15
0
Файл: 049.py Проект: 3kwa/euler
from itertools import permutations
from collections import defaultdict

from primes import generator

four_digits_prime = [p for p in generator(10000) if p>999]

candidates = defaultdict(list)

for prime in four_digits_prime:

    # generating a set of all the permutations of the digit of prime
    permutations_of_prime = set(map(
        lambda tuple_:int(''.join(tuple_)),
        permutations(str(prime))))

    # for every permutation
    for permutation in permutations_of_prime:
        # check for primality, 3330 and greater than prime (key is smallest prime)
        if permutation in four_digits_prime and \
           (permutation - prime) % 3330 == 0 and \
           permutation > prime:
            candidates[prime].append(permutation)

# display the concatenated string of 3 primes satisfying the conditions
for k,v in candidates.iteritems():
    v.append(k)
    if len(v) == 3:
        print ''.join(map(str,sorted(v)))

Пример #16
0
import primes

p = primes.generator()
for i in range(10000):
    p.next()
print p.next()
Пример #17
0
Файл: 069.py Проект: 3kwa/euler
which is obtain for small values of pi

2 * 3 = 6
2 * 3 * 5 = 30
2 * 3 * 5 * 7 = 210
2 * 3 * 5 * 7 * 11  = 2310
2 * 3 * 5 * 7 * 11 * 13 = 30030
2 * 3 * 5 * 7 * 11 * 13 * 17 = ...

Smallest n under 1 000 000 giving a a max n / phi(n)
"""

from operator import mul
from primes import generator

primes = list( generator(1000000) )
set_ = set( primes )

def factors(number):
    """
    >>> f = factors(4)
    >>> f.next()
    (2, 2)
    >>> list(factors(6))
    [(2, 1), (3, 1)]
    >>> list(factors(12))
    [(2, 2), (3, 1)]
    """
    for p in primes:
        if number < p / 2:
            break
Пример #18
0
Файл: 070.py Проект: 3kwa/euler
from math import sqrt
from primes import generator

def is_permutation(a, b):
    return ''.join(sorted(str(a))) == ''.join(sorted(str(b)))

N = 10000000
primes = list( generator(N / 2) )

ratio = N
result = 0

for small in primes[:len(primes) / 2]:
    if small > sqrt(N):
        break
    for big in primes[len(primes)/2::-1]:
        if big <= small:
            break
        n = small * big
        if n <= N:
            phi = (small - 1) * (big - 1)
            if is_permutation(n, phi):
                man = float(n) / phi
                result, ratio = (n, man) if man < ratio else (result, ratio)

print result