# The incredible formula  n² − 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² + an + b, where |a| < 1000 and |b| < 1000

# 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 time
import primes

crible = primes.Sieve(100000)
l = crible.listPrimes()
first_primes = l[:2000]  # we extract the 200 first primes


def f(a, b):
    """ int*int -> int->int """
    g = lambda n: (n**2 + a * n + b)
    return g


def find(a, l):
    """ int * int list -> bool : find if a is in l assuming l sorted """
    i = 0
    while (l[i] != a) and (l[i] < a):
        i += 1
Ejemplo n.º 2
0
#                 									                         #
#      				   Project Euler : probleme 41        			 	     #
#                                           						         #
#                                           						    	 #
# ========================================================================== #

# 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 time
import primes as pm

s = pm.Sieve(10**7)
lst_primes = s.listPrimes()  # list of primes under 10**9


def check_pandigital(n):
    """ int -> bool : check if n is a pandigital number"""
    length = len(str(n))
    return set(range(1, length + 1)) == set(map(int, list(str(n))))


def euler_41():
    """ unit -> int : find the largest n-pandigital prime """
    # we start from the end of the list (the largest primes)
    # so the first we find if the largest.
    for i in reversed(lst_primes):
        if check_pandigital(i):
# 9 = 7 + 2×12
# 15 = 7 + 2×22
# 21 = 3 + 2×32
# 25 = 7 + 2×32
# 27 = 19 + 2×22
# 33 = 31 + 2×12

# It turns out that the conjecture was false.

# What is the smallest odd composite that cannot be written as the sum of a
# prime and twice a square?

import primes as pr
import time

crible = pr.Sieve(10**4)
prime_lst = crible.listPrimes()  # list of primes


def is_sqrt(n):
    """ int -> bool : is n a squared number """
    x = n
    y = (x + 1) / 2
    while y < x:
        x = y
        y = (x + n / x) / 2
    return x * x == n


def is_solution(n):
    """ int -> bool : try to find if n can be written as prime + 2*square """
# ========================================================================== #

# The number 3797 has an interesting property. Being prime itself, it is possible
# to continuously remove digits from left to right, and remain prime at each
# stage: 3797, 797, 97, and 7. Similarly we can work from right to
# left: 3797, 379, 37, and 3.

# Find the sum of the only eleven primes that are both truncatable from left to
# right and right to left.

# NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.

import time
import primes

crible = primes.Sieve(10**6)
crible_list = crible.listPrimes()


def is_truncatable_prime(n):
    """ int -> bool : test if n is a truncatable prime """
    lth = len(str(n))
    # We check quikly if
    if not (int(str(n)[0]) in [2, 3, 5, 7]):
        return False
    trunc_lst = []  # a list containing the truncation of n
    for i in xrange(1, lth):
        trunc_lst.append(int(str(n)[0:i]))
        trunc_lst.append(int(str(n)[i:lth]))
    g = lambda n: n in crible_list
    return all(map(g, trunc_lst))
# 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 time
import primes

# we generate the 4 digits prime numbers
pr_lst = primes.Sieve(10**4).listPrimes()

# we notice there is two parameters :
# * the gap of the arithemtic sequence
#   the gap is roughtly between [1 - 4500] since all primes are 4-digits
#   1000 + 2*gap < 10**5
# * the first prime number
#   which starts from 1009 up to 9973


def explode(n):
    """ 
    int -> int list
    """
    if n < 10:
        return [n]
Ejemplo n.º 6
0
#                                           						         #
#                                           						    	 #
# ========================================================================== #

# The number, 197, is called a circular prime because all rotations of the
# digits: 197, 971, and 719, are themselves prime.

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

# How many circular primes are there below one million?

import time
import primes as p

crible = p.Sieve(1000000)  # a sieve for getting primes below 10**6
first_primes = crible.listPrimes()


def rotate(n):
    """ int -> int list : return a list containg circular rotation of n"""
    rotlist = []
    m = str(n)
    counter = 0
    while counter < len(str(n)):
        m = m[1:] + m[0]  # rotation one time : one step left
        rotlist.append(int(m))
        counter += 1
    return rotlist

# 41 = 2 + 3 + 5 + 7 + 11 + 13
# This is the longest sum of consecutive primes that adds to a prime below
# one-hundred.

# The longest sum of consecutive primes below one-thousand that adds to a prime,
# contains 21 terms, and is equal to 953.

# Which prime, below one-million, can be written as the sum of the most
# consecutive primes?

import time
import primes

# we start by generating the primes below 10**6
lst = primes.Sieve(10**6).listPrimes()


def euler_50():
    res = 1  # the max length we will found
    size = len(lst)
    for i in xrange(size):
        print lst[i]
        for j in range(i + res, size):
            # j will be the position of the last prime in the sum
            # as the max length we restrict the area we search
            chunk = lst[i:j + 1]  # we sub list the primes we will try to add
            chunklength = len(chunk)
            chunktotal = sum(chunk)
            if chunktotal > 10**6:
                break