示例#1
0
def find_consecutive_integers_with_same_number_of_prime_factors(count):
    consecutive_found = 0
    limit = 300000
    primes = [p for p in eratosthenes_sieve(math.ceil(limit / 2))]

    for i in range(3, limit):
        factors = find_prime_factors(i, primes)
        # print(i, factors)

        if len(factors) == count:
            consecutive_found += 1

            if consecutive_found == count:
                return i-count+1
        else:
            consecutive_found = 0

    return 0
def find_largest_pan_digital_prime():
    primes = [x for x in eratosthenes_sieve(int(sqrt(1e9)))]

    # Generate all pandigital numbers.
    pan_digs = []

    for i in range(1, 10):
        pan_digs += generate_pan_digital_numbers(1, i)

    for i in range(len(pan_digs)):
        temp = pan_digs[-i]

        if temp % 2 == 0 or temp % 5 == 0:
            continue

        if is_prime(temp, primes):
            return temp

    return 1
def minimize_search_space_first():
    primes = [p for p in eratosthenes_sieve(9999) if p > 1000]
    prime_permutations = {}

    for p in primes:
        key = sort_as_string(p)

        if key not in prime_permutations:
            prime_permutations[key] = [p]
        else:
            prime_permutations[key].append(p)

    for x, y in prime_permutations.items():
        if len(y) >= 3:

            # Check if there are three items with the same offset in the current tuple.
            # TODO: This should be possible to do with a map/reduce construct.
            for i in range(0, len(y)-2):
                for j in range(i+1, len(y)-1):
                    offset = y[j] - y[i]

                    if y[j] + offset in y:
                        print(y[i], y[j], y[j] + offset)
def find_prime_as_sum_of_consecutive_primes(ceiling):
    primes = [p for p in eratosthenes_sieve(ceiling)]
    max_length = 1
    total = 0

    for i in range(0, len(primes)):
        # If no better solution is possible, we're done.
        if i > (len(primes) - max_length):
            break

        for j in range(i + max_length, len(primes)):
            next_sum = sum(primes[i:j+1])

            if next_sum > ceiling:
                break

            if next_sum in primes:
                if j - i > max_length:
                    max_length = j - i
                    total = next_sum
                    # print(total, i, j, new_prime)

    return total
示例#5
0
def minimize_search_space_first():
    primes = [p for p in eratosthenes_sieve(9999) if p > 1000]
    prime_permutations = {}

    for p in primes:
        key = sort_as_string(p)

        if key not in prime_permutations:
            prime_permutations[key] = [p]
        else:
            prime_permutations[key].append(p)

    for x, y in prime_permutations.items():
        if len(y) >= 3:

            # Check if there are three items with the same offset in the current tuple.
            # TODO: This should be possible to do with a map/reduce construct.
            for i in range(0, len(y) - 2):
                for j in range(i + 1, len(y) - 1):
                    offset = y[j] - y[i]

                    if y[j] + offset in y:
                        print(y[i], y[j], y[j] + offset)
示例#6
0
def find_prime_as_sum_of_consecutive_primes(ceiling):
    primes = [p for p in eratosthenes_sieve(ceiling)]
    max_length = 1
    total = 0

    for i in range(0, len(primes)):
        # If no better solution is possible, we're done.
        if i > (len(primes) - max_length):
            break

        for j in range(i + max_length, len(primes)):
            next_sum = sum(primes[i:j + 1])

            if next_sum > ceiling:
                break

            if next_sum in primes:
                if j - i > max_length:
                    max_length = j - i
                    total = next_sum
                    # print(total, i, j, new_prime)

    return total
def evaluate_candidates():
    primes = [p for p in eratosthenes_sieve(9999) if p > 1000]
    result = []
    counter = 0

    for p in primes:
        sorted_p = sort_as_string(p)

        for offset in range(1000, 4500, 2):
            counter += 1
            x1 = p + offset
            x2 = p + (2 * offset)

            if x2 > 9999:
                break

            if x1 in primes:
                if sorted_p == sort_as_string(x1):
                    if x2 in primes:
                        if sorted_p == sort_as_string(x2):
                            result.append([p, x1, x2])

    print(counter)
    return result
def find_circular_primes(upper):
    result = []
    primes = [i for i in eratosthenes_sieve(upper)]
    prime_arr = {i: True for i in primes}
    # print(primes)

    for i in primes:
        if i > upper:
            break

        temp = i
        is_prime = True

        for j in range(len(str(i)) - 1):
            temp = rotate_number(temp)
            is_prime = is_prime and prime_arr.get(temp)

            if not is_prime:
                break

        if is_prime:
            result.append(i)

    return result
示例#9
0
def evaluate_candidates():
    primes = [p for p in eratosthenes_sieve(9999) if p > 1000]
    result = []
    counter = 0

    for p in primes:
        sorted_p = sort_as_string(p)

        for offset in range(1000, 4500, 2):
            counter += 1
            x1 = p + offset
            x2 = p + (2 * offset)

            if x2 > 9999:
                break

            if x1 in primes:
                if sorted_p == sort_as_string(x1):
                    if x2 in primes:
                        if sorted_p == sort_as_string(x2):
                            result.append([p, x1, x2])

    print(counter)
    return result
# 21 = 3 + 2×3^2
# 25 = 7 + 2×3^2
# 27 = 19 + 2×2^2
# 33 = 31 + 2×1^2
#
# 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?
#
# Approach: Generate all possible combinations of prime + 2 * square until we find a missing odd composite.

from ProjectEuler.utils.primes import eratosthenes_sieve
import math

upper_bound = 10000
primes = [p for p in eratosthenes_sieve(upper_bound) if p > 2]


def generate_odd_composites():
    odd_composites = [odd for odd in range(9, primes[-1] + 2, 2) if odd not in primes]
    return odd_composites


def generate_goldbach():
    goldbach_composites = [(p + 2*i*i) for p in primes for i in range(1, int(math.sqrt(upper_bound)))
                           if (p + 2*i*i) < upper_bound and (p + 2*i*i) not in primes]
    return goldbach_composites


#print(primes)
#print(generate_goldbach())
from ProjectEuler.utils.primes import eratosthenes_sieve


def truncate_left(num):
    if num < 10:
        return num

    return int(str(num)[1:])


def truncate_right(num):
    return num // 10


prime_list = [i for i in eratosthenes_sieve(1000001)]
prime_set = set(prime_list)
result = []

for prime in prime_list:
    if prime < 11:
        continue

    is_prime = True
    temp = prime

    while temp >= 10:
        temp = truncate_left(temp)
        is_prime = is_prime and temp in prime_set

    if is_prime:
示例#12
0
# 21 = 3 + 2×3^2
# 25 = 7 + 2×3^2
# 27 = 19 + 2×2^2
# 33 = 31 + 2×1^2
#
# 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?
#
# Approach: Generate all possible combinations of prime + 2 * square until we find a missing odd composite.

from ProjectEuler.utils.primes import eratosthenes_sieve
import math

upper_bound = 10000
primes = [p for p in eratosthenes_sieve(upper_bound) if p > 2]


def generate_odd_composites():
    odd_composites = [odd for odd in range(9, primes[-1] + 2, 2) if odd not in primes]
    return odd_composites


def generate_goldbach():
    goldbach_composites = [(p + 2*i*i) for p in primes for i in range(1, int(math.sqrt(upper_bound)))
                           if (p + 2*i*i) < upper_bound and (p + 2*i*i) not in primes]
    return goldbach_composites


# print(primes)
# print(generate_goldbach())
示例#13
0
__author__ = 'johan'

# By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
#
# What is the 10001st prime number?


from ProjectEuler.utils.primes import eratosthenes_sieve

a = [p for p in eratosthenes_sieve(11000000)]
print(len(a), a[-1])

if len(a) > 500000:
    print('10001st prime:', a[10000])
    print('prime 5\t\t\t:', a[4])
    print('prime 50\t\t:', a[49])
    print('prime 500\t\t:', a[499])
    print('prime 5000\t\t:', a[4999])
    print('prime 50000\t\t:', a[49999])
    print('prime 500000\t:', a[499999])
示例#14
0
# n*n + a*n + b = p
# Observations:
# n = 0 => p = b => b must be a prime
# n = 1 => 1 + a + b = p => a + b = p - 1 => always even => a must be odd (and a prime?)
# n = 2 => 4 + 2a + b = p
# a = (p - b - n*n) / n for all n > 0
# p - b will always be an even number (prime - prime)
# n*n is always odd if n is odd, always even if n is even.
#
# Let p be all primes from 1 to 1000 000.
# Let b be all primes from 1 to 1000.
# Let a be all numbers from 0 to 1000.

from ProjectEuler.utils.primes import eratosthenes_sieve

primes = [p for p in eratosthenes_sieve(100000)]
ax = [x for x in range(-999, 1001, 2)]
bx = [-x for x in primes if x < 1000]
bx.extend([-x for x in bx])

print(len(bx), bx)

result = (0, 0, 0)

# ax = [1]
# bx = [41]
# print((1 in ax), (41 in bx))

for a in ax:
    for b in bx:
        n = 0
__author__ = 'johan'

# The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
#
# Find the sum of all the primes below two million.

from ProjectEuler.utils.primes import eratosthenes_sieve

a = [p for p in eratosthenes_sieve(2000000)]
print(len(a), a[-1])

print(sum(a))