Ejemplo n.º 1
0
def prob_027():
    b_list = primes.primes_list(1000)
    max_primes, ans = 0, 0
    for a in range(-999, 1000):
        for b in b_list:
            number_primes = first_non_prime(a, b)
            if number_primes > max_primes:
                max_primes, ans = number_primes, a*b
    return ans
Ejemplo n.º 2
0
def prob_027():
    b_list = primes.primes_list(1000)
    max_primes, ans = 0, 0
    for a in range(-999, 1000):
        for b in b_list:
            number_primes = first_non_prime(a, b)
            if number_primes > max_primes:
                max_primes, ans = number_primes, a * b
    return ans
Ejemplo n.º 3
0
def find():
    primes = primes_list(10**4)
    for length in xrange(1000, 0, -1):
        first = 0
        while True:
            summ = sum(primes[first:first+length])
            # print summ
            if summ >= 10**6: break
            if prime(summ): return summ
            first += 1
Ejemplo n.º 4
0
from primes import primes_list, primes_dict
PRIMES_LIST = primes_list(999999)
PRIMES_DICT = primes_dict(999999)
print("Primes list and dict built")

def shift_one(num):
    str_num = str(num)
    cycled_num_str = str_num[1:len(str_num)] + str_num[0]
    return int(cycled_num_str)

def is_cyclic_prime(prime):
    shifted = prime
    if '0' in str(prime):
        return False
    for _ in range(len(str(prime))+2):
        shifted = shift_one(shifted)
        if shifted not in PRIMES_DICT:
            return False
    return True

def check_all_primes(n, print_circular=False):
    cyclic_count = 0
    for prime in PRIMES_LIST:
        if prime > n:
            break
        if is_cyclic_prime(prime):
            if print_circular:
                print(prime)
            cyclic_count += 1
    return cyclic_count
Ejemplo n.º 5
0
#!/usr/bin/env python2
# coding=utf-8
"""
Summation of primes
Problem 10

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.
"""
from primes import primes_list
from functools import reduce

print(reduce(lambda x, y: x + y, primes_list(2000000)))
Ejemplo n.º 6
0
from performance import make_timed
from primes import primes_list, primes_dict
from math import sqrt, floor

PL = primes_list(1000000)
PD = primes_dict(1000000)

def is_pandigital(number):
    str_num = str(number)
    for i in range(1,len(str_num)+1):
        if not str(i) in str_num:
            return False
    return True

def is_prime(n):
    if n < 1000000:
        if n in PD:
            return True
        else:
            return False
    else:
        for i in PL:
            if n % i == 0:
                return False
        for i in range(1000001, int(sqrt(n)), 2):
            if n % i == 0:
                return False
        return True

def run():
    largest = 0
Ejemplo n.º 7
0
from performance import make_timed
from primes import primes_list, primes_dict
from math import sqrt, floor

PL = primes_list(1000000)
PD = primes_dict(1000000)


def is_pandigital(number):
    str_num = str(number)
    for i in range(1, len(str_num) + 1):
        if not str(i) in str_num:
            return False
    return True


def is_prime(n):
    if n < 1000000:
        if n in PD:
            return True
        else:
            return False
    else:
        for i in PL:
            if n % i == 0:
                return False
        for i in range(1000001, int(sqrt(n)), 2):
            if n % i == 0:
                return False
        return True
Ejemplo n.º 8
0
from primes import primes_list, primes_dict

PRIMES_LIST = primes_list(1000000)
PRIMES_DICT = primes_dict(1000000)


def truncate_right(n):
    return int(str(n)[0:-1])


def truncate_left(n):
    return int(str(n)[1:])


def is_prime(n):
    return n in PRIMES_DICT


def is_truncatable(n):
    if '0' in str(n):
        return False
    if len(str(n)) < 2:
        return False
    return True


def is_truncatable_prime(n):
    if not is_truncatable(n):
        return False
    if not is_prime(n):
        return False
Ejemplo n.º 9
0
from primes import primes_list, primes_dict

P_LIST = primes_list(100000)
P_DICT = primes_dict(100000)

S_LIST = [2*(n**2) for n in range(1,100000)]
S_DICT = {}
for i in range(len(S_LIST)):
    S_DICT[S_LIST[i]] = None

def test(n):
    for prime in P_LIST:
        if prime > n:
            break
        for sq in S_LIST:
            if prime + sq > n:
                break
            if prime + sq == n:
                return True
    return False

for i in range(33, 1000000, 2):
    if i in P_DICT:
        continue
    if not test(i):
        print(i)
        break
    

Ejemplo n.º 10
0
def prob_010():
    return sum(primes.primes_list(2000000))
Ejemplo n.º 11
0
from primes import primes_list, primes_dict

PRIMES_LIST = primes_list(1000000)
PRIMES_DICT = primes_dict(1000000)

def truncate_right(n):
    return int(str(n)[0:-1])

def truncate_left(n):
    return int(str(n)[1:])

def is_prime(n):
    return n in PRIMES_DICT

def is_truncatable(n):
    if '0' in str(n):
        return False
    if len(str(n)) < 2:
        return False
    return True

def is_truncatable_prime(n):
    if not is_truncatable(n):
        return False
    if not is_prime(n):
        return False
    left = truncate_left(n)
    right = truncate_right(n)
    while(is_truncatable(left)):
        if (not is_prime(left)) or (not is_prime(right)):
            return False
Ejemplo n.º 12
0
from performance import make_timed, memoize
from primes import primes_list, primes_dict
MAX_NUM = 28123
P_LIST = primes_list(MAX_NUM)
P_DICT = primes_dict(MAX_NUM)

def sum_prop_divs(n):
    return sum(proper_divisors(n))

def proper_divisors(n):
    p_factors = prime_factors(n)
    divisors = {}
    width = len(p_factors)
    for i in range(2**width-1):
        divisor = choose_factors(p_factors, binary_str(i, width))
        divisors[divisor] = None
    return list(divisors.keys())
        
def choose_factors(factors, bit_str):
    product = 1
    for i in range(len(factors)):
        if bit_str[i] == '1':
            product *= factors[i]
    return product

def binary_str(value, length):
    return str(bin(value))[2:].zfill(length)

def prime_factors(n, primes_list=P_LIST, primes_dict=P_DICT):
    factors = []
    if n == 1 or n == 0:
Ejemplo n.º 13
0
#!/usr/bin/env python2
import primes

def is_prime(n):
	if n < 2: return False
	for i in xrange(3, int(n**0.5) + 1, 2):
		if not n % i: return False
	return True

result = len((2,5))
for number in primes.primes_list(10**6):
	snum = str(number)
	if filter(lambda d: d in '024568', snum): continue
	rotations = {int(snum[i:] + snum[:i]) for i in xrange(len(snum))}
	result += len(rotations) == len(filter(is_prime, rotations))

print 'result: %s' % result