Пример #1
0
def main(limit):
    """
    Get sum of all integers which can't be expressed
    as the sum of two abundant numbers
    """
    primes = list(primes_upto(limit))
    abundant = Abundant(limit)
    print sum(i for i in range(1, limit) if not abundant.has_pair(i))
Пример #2
0
def find_prime_sequence(biggest_number):
    primes = set(primes_upto(biggest_number))
    primes_sorted = sorted(primes)
    longest_streak = []
    for i, p in enumerate(primes_sorted):
        current_streak = [p]
        current_sum = p
        for next_p in primes_sorted[i + 1:]:
            if current_sum + next_p > biggest_number:
                break
            current_streak.append(next_p)
            current_sum = current_sum + next_p
            if len(current_streak) <= len(longest_streak):
                continue
            if current_sum in primes:
                longest_streak = current_streak[:]
    return longest_streak
Пример #3
0
 def __init__(self, limit):
     self.limit = limit
     self.primes = list(primes_upto(limit))
     self.calculate_abundant_numbers()
     self.calculate_pairs()
Пример #4
0
from euler_math import primes_upto

# Warning: Ugly, imperative code ahead.
# Don't do this at home kids.
# I just wanted to get the solution quickly.
# This should really be done using at least some functions.

maximum = 10000
primes = primes_upto(maximum)
primes_ordered = list(primes)
primes_set = set(primes_ordered)

for i in range(9, maximum, 2):
  if i in primes_set:
    # Only checking composite numbers
    continue
  #print i
  found = False
  for p in primes_ordered:
    # i = p + 2*x^2 with min(x) = 1
    # Therefore the upper bound
    # for the prime numbers is as follows:
    if p > i - 2:
      break
    for square_num in range(1, (i - p)/2 + 1):
      if i == p + 2*square_num**2:
        #print "{} = {} + 2*{}^2".format(i, p, square_num)
        found = True
        break
    if found:
      break
Пример #5
0
"""
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.
"""

from euler_math import primes_upto

sieve = list(primes_upto(20000))

def is_prime(x):
    return x in sieve

def consecutive_primes(a,b):
    n = 0
    while True:
        x = n**2 + a*n + b
        if is_prime(x):
            yield x
            n += 1
        else:
            break

# Tests
# print len(list(consecutive_primes(1, 41))) # 40
# print len(list(consecutive_primes(-79, 1601))) # 80

max = 0
for a in range(-1000, 1000):
    for b in primes_upto(1000):
        length = len(list(consecutive_primes(a,b)))