Ejemplo n.º 1
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.º 2
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.º 3
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.º 4
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.º 5
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.º 6
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.º 7
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: