Esempio n. 1
0
def square_free(n):
    pgen = primes()
    p = next(pgen)
    while p ** 2 <= n:
        if n % p ** 2 == 0:
            return False
        p = next(pgen)

    return True
Esempio n. 2
0
    while True:
        while n != 1:
            if n % plist[i] == 0:
                while n % plist[i] == 0:    # factor out current prime number
                    n /= plist[i]
                distinct_count += 1
                if distinct_count > 4:
                    return False

            i += 1

        return distinct_count == 4

start_time = time()

pgen = primes()     # prime generator
p = next(pgen)
plist = []
while p < 1000000:
    plist.append(p)
    p = next(pgen)

# start with i=10 and check at each iteration if i, i+1, i+2, i+3 all have
# 4 distinct prime factors, when found, print i and exit loop
# h4dpf1 = has 4 distinct prime factor 1
# don't start at has_four_dpf(0), the function will get into a infinite loop
h4dpf1 = has_four_dpf(1)
h4dpf2 = has_four_dpf(2)
h4dpf3 = has_four_dpf(3)
h4dpf4 = has_four_dpf(4)
i = 4
Esempio n. 3
0
    n_str = str(n)

    for i in range(1, len(n_str)):
        if not is_prime(int(n_str[i:])):
            return False

    # test for right truncatable
    for i in range(1, len(n_str)):
        if not is_prime(int(n_str[:i])):
            return False

    return True

start_time = time()

pstream = primes()

for i in range(4):      # skip first 4 primes
    p = next(pstream)

l = []
while True:
    p = next(pstream)
    if truncatable(p):
        l.append(p)
        if len(l) == 11:    # found all, we are done
            break

print l
print sum(l)
Esempio n. 4
0
# Circular primes

# The number, 197, is called a circular prime because all rotations of the
# digits: 197, 971, and 719, are themselves prime.

# There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71,
# 73, 79, and 97.

# How many circular primes are there below one million?

from time import time
from UsefulFunctions import primes, is_prime, rotations

start_time = time()

p = primes()

candidate = next(p)
l = []
while candidate < 1000000:
    still_prime = True
    for e in rotations(candidate):
        if not is_prime(e):
            still_prime = False
            break

    if still_prime:
        l.append(candidate)

    candidate = next(p)
Esempio n. 5
0
# A composite is a number containing at least two prime factors. For example,
# 15 = 3 × 5; 9 = 3 × 3; 12 = 2 × 2 × 3.

# There are ten composites below thirty containing precisely two, not
# necessarily distinct, prime factors: 4, 6, 9, 10, 14, 15, 21, 22, 25, 26.

# How many composite integers, n < 10^8, have precisely two, not necessarily
# distinct, prime factors?

from time import time
from UsefulFunctions import primes

start_time = time()

N = 100000000
pgen = primes()
pnumbers = []      # list of prime numbers < N/2
p = next(pgen)
while p <= N / 2:
    pnumbers.append(p)
    p = next(pgen)

print len(pnumbers)
count = 0
i = 0
while pnumbers[i] ** 2 < N:
    j = i
    while pnumbers[i] * pnumbers[j] <= N:
        count += 1
        j += 1
        if j == len(pnumbers):
Esempio n. 6
0

def ocgen():
    n = 9
    while True:
        if not is_prime(n):
            yield n
        n += 2

start_time = time()

oc = ocgen()            # odd composite stream
continue_ = True
while continue_:
    c = next(oc)        # get an odd composite candidate
    pgen = primes()     # start with 1st prime all over again

    while True:         # verification loop
        p = next(pgen)  # try a new prime number
        i = 1
        while p + 2 * (i ** 2) < c: # try to see if an i could make the current prime work
            i += 1

        if p + 2 * (i ** 2) == c:   # if formula is verified, break 
            break

        if p > c:       # the formula fails
            continue_ = False
            break

print c