Example #1
0
def findPrimePowerTriples():
    P_set = set()
    for p in primeCheckII.primes(2, -1):
        for q in primeCheckII.primes(2, -1):
            for r in primeCheckII.primes(2, -1):
                val = p**2 + q**3 + r**4
                if val < MAX_VAL:
                    P_set |= {val}
                else:
                    break_q = (r == 2)
                    break_p = break_q and (q == 2)
                    break
            if break_q:
                break
        if break_p:
            return (P_set)
def findSums(N, n_0, count_self=True):
    if count_self and PC.PrimeCheck(N) and N <= n_0:
        yield (1)
    for n in PC.primes(2, n_0 + 1):
        dn = N - n
        if dn > 1:
            yield (sum([s for s in findSums(dn, min([dn, n]))]))
def primePairSets(a_0,p_dict ):
    for a in primeCheckII.primes(a_0):
        print(a)
        ab_list = []
        for i_b in range(len(p_dict)):
            b = p_dict[i_b]
            if combinePrimes(b,a):
                ab_list.append(b)
        while len(ab_list) > 3:
            b = ab_list.pop(0)
            print("\t",b)
            bc_list = []
            for c in ab_list:
                if combinePrimes(b,c):
                    bc_list.append(c)
            while len(bc_list) > 2:
                c = bc_list.pop(0)
                print("\t\t",c)
                cd_list = []
                for d in bc_list:
                    if combinePrimes(c,d):
                        cd_list.append(d)
                while len(cd_list) > 1:
                    d = cd_list.pop(0)
                    print("\t\t\t",d)
                    for e in cd_list:
                        if combinePrimes(d,e):
                            return([b,c,d,e,a])
        p_dict[len(p_dict)] = a
Example #4
0
def isSquarefree(x):
    if x == 1:
        return (True)
    for p in primeCheckII.primes():
        p2 = p**2
        if p2 > x:
            return (True)
        elif x % p2 == 0:
            return (False)
def getFactors(x):
    X = x
    #factor_list = []
    while True:
        success = False
        max_p = int(sqrt(X))
        for p in primes(2, max_p + 1):
            if X % p == 0:
                success = True
                X /= p
                yield (p)
                break
        if not success:
            yield (int(X))
            break
Example #6
0
def findSpecialPrimes():
    checked_list = []
    for prime in primeCheckII.primes(1000,10000):
        #print(prime)
        for p in permutation([x for x in range(len(str(prime)))],len(str(prime))):
            s = [str(prime)[x] for x in p]
            next_prime = int("".join(s))
            if len(str(next_prime)) < PRIME_LENGTH or next_prime <= prime:
                continue
            if primeCheckII.PrimeCheck(next_prime):
                d_prime = next_prime - prime
                last_prime = next_prime + d_prime
                if len(str(last_prime)) != PRIME_LENGTH:
                    continue
                if primeCheckII.PrimeCheck(last_prime):
                    if Counter([s for s in str(last_prime)]) == Counter(s):
                        if prime not in checked_list:
                            checked_list.append(prime)
                            yield([prime,next_prime,last_prime])
Example #7
0
def findPrimeFactors(x, n):
    prime_factors = {}
    X = x
    for p in primeCheckII.primes():
        if p > X:
            break
        while X % p == 0:
            X /= p
            if p not in prime_factors:
                prime_factors[p] = 1
            else:
                prime_factors[p] += 1
        if len(prime_factors) > n:
            return (False)
        if X == 1:
            break
    if len(prime_factors) != n:
        return (False)
    #print(x,prime_factors)
    return (prime_factors)
Nstart = 5
Nend = 10000
SumD = 0
K = 1
K_list = []
for N in range(Nstart, Nend + 1):
    result = K
    for k in range(K, N):
        Q = ((k / (k + 1))**k) * (N / (k + 1))
        if Q < 1:
            result = k
            break
    if result > K:
        K = result
        K_list = [
            x for x in getFactors(K)
            if x in [p for p in primes(3, K + 1) if p != 5]
        ]
    D = -N
    M = N
    for factor in K_list:
        if M % factor != 0:
            D = N
            break
        else:
            M /= factor
    #print(N,K,D,K_list)
    SumD += D
print(SumD)
Example #9
0
            ]
            new_list += val_list
            p += 1
            x = X**p
        for item in new_list:
            yield (item)


def computeValue(numList):
    val = 1
    for num in numList:
        val *= num
    return (val)


prime_list = [p for p in primes(2, 30)]

number_list = [2]

total_divisors = 1000

try_list = [number_list]
k = 0
result = []
while True:
    #print([[computeValue(i),len([x for x in generateDivisors(i)]),i] for i in try_list])
    tried_list = []
    for attempt in try_list:

        skip_attempt = False
# PROJECT EULER PROBLEM 60 - Prime Pair Sets
import permutations
import primeCheckII
from memoise import Memoise

@Memoise
def combinePrimes(a,b):
    return(primeCheckII.PrimeCheck(int(str(a)+str(b))) and primeCheckII.PrimeCheck(int(str(b)+str(a))))


UNIQUE_PRIMES = 5
MIN_SUM = 792
p_dict = {x: p for x,p in enumerate(primeCheckII.primes(3,673))}

def primePairSets(a_0,p_dict ):
    for a in primeCheckII.primes(a_0):
        print(a)
        ab_list = []
        for i_b in range(len(p_dict)):
            b = p_dict[i_b]
            if combinePrimes(b,a):
                ab_list.append(b)
        while len(ab_list) > 3:
            b = ab_list.pop(0)
            print("\t",b)
            bc_list = []
            for c in ab_list:
                if combinePrimes(b,c):
                    bc_list.append(c)
            while len(bc_list) > 2:
                c = bc_list.pop(0)
Example #11
0
# PROJECT EULER PROBLEM 7 - 10001st Prime

from primeCheckII import primes

count = 1
max_count = 10001
for p in primes():
    if count == max_count:
        print(p)
        break
    count += 1

Example #12
0
def _rotPrime(str_p):
    new_str_p = str_p[1:] + str_p[0]
    p_list = [int(str_p)]
    while new_str_p != str_p:
        if not primeCheckII.PrimeCheck(int(new_str_p)):
            return (False)
        p_list.append(int(new_str_p))
        new_str_p = new_str_p[1:] + new_str_p[0]
    return (p_list)


BAD_NUMS = ['2', '4', '5', '6', '8', '0']

circ_primes = []
for p in primeCheckII.primes(2, 1000000):
    if p in circ_primes:
        continue
    str_p = str(p)
    if len(str_p) == 1:
        circ_primes.append(p)
    else:
        if not any([b in str_p for b in BAD_NUMS]):
            rot_primes = _rotPrime(str_p)
            if rot_primes:
                for val in rot_primes:
                    if val not in circ_primes:
                        circ_primes.append(val)
                print(rot_primes)

print(circ_primes)
# PROJECT EULER PROBLEM 10 - Summation of Primes

from primeCheckII import primes

max_prime = 2000000
total = 0
for p in primes(2, max_prime):
    total += p

print(total)
# PROJECT EULER PROBLEM 46 - Goldbach's Other Conjecture

from math import sqrt
import primeCheckII

def oddComposite():
    x = 9
    yield(x)
    while True:
        x += 2
        if primeCheckII.PrimeCheck(x):
            continue
        yield(x)


for x in oddComposite():
    if not any([sqrt((x-p)//2)%1==0 for p in primeCheckII.primes(3,x)]):
        print(x)
        break
    #print(x)

Example #15
0
# PROJECT EULER PROBLEM 43 - Sub-string Divisibility

from primeCheckII import primes
from permutations import permutation

GOOD3 = [0, 2, 4, 6, 8]
GOOD5 = [0, 5]

prime_list = [p for p in primes(2, 18)]

results = []
for digit3 in GOOD3:
    digit_list = [x for x in range(10)]
    new_num = ['X' for x in range(10)]
    new_num[3] = digit3
    digit_list.remove(digit3)
    for digit5 in GOOD5:
        new_digit_list = digit_list[:]
        if digit3 == digit5:
            continue
        new_num[5] = digit5
        new_digit_list.remove(digit5)
        for p in permutation(new_digit_list, len(new_digit_list)):
            num = ''
            for d in new_num:
                if d == 'X':
                    num += str(p.pop(0))
                else:
                    num += str(d)

            good_num = True