示例#1
0
def factors_as_powers(n):
    """Yields the factors of n grouped as (factor, power)"""

    # Avoid the case where n = 1
    if n == 1:
        yield (1, 1)

    # If n is prime itself, we can early terminate
    elif Primes.is_prime(n):
        yield (n, 1)
    else:
        primes = iter(Primes(__get_until(n)))
        while n != 1:
            try:
                # If we have a next prime and we can divide, do it
                prime = next(primes)
                power = 0
                while n % prime == 0:
                    power += 1
                    n //= prime

                # If we executed the while loop at least once, return this prime factor
                if power > 0:
                    yield (prime, power)

            except StopIteration:  # n is now prime itself
                yield (n, 1)
                n = 1
示例#2
0
def factors(n):
    """Yields the factors of n"""

    # Avoid the case where n = 1
    if n == 1:
        yield 1

    # If n is prime itself, we can early terminate
    elif Primes.is_prime(n):
        yield n

    # Otherwise, try to divide n by every prime and yield the factors
    else:
        primes = iter(Primes(__get_until(n)))

        # While we can divide it, do it
        while n != 1:
            try:
                # If we have a next prime and we can divide, do it
                prime = next(primes)
                while n % prime == 0:
                    n //= prime
                    yield prime
            except StopIteration:  # n is now prime itself
                yield n
                n = 1
示例#3
0
def count_factors(n):
    """Counts the factors of n"""

    # If n equals 1 or is prime itself, it only has one factor
    # (since 1 is always a factor when n > 1, it is not counted)
    if n == 1 or Primes.is_prime(n):
        return 1
    else:
        count = 0
        primes = iter(Primes(__get_until(n)))

        # While we can find more factors
        while n != 1:
            try:
                # If we have a next prime and we can divide, do it
                prime = next(primes)
                while n % prime == 0:
                    n //= prime
                    count += 1

            except StopIteration:  # n is now prime itself
                count += 1
                n = 1

        return count
示例#4
0
def search():
    max_goal = 8
    max_primes = 0
    for prime in Primes(1000000):
        for fam in families(prime):
            prime_count = 0
            for child in fam():
                if Primes.is_prime(child):
                    prime_count += 1

            if prime_count > max_primes:
                max_primes = prime_count
                print('New max of {} for prime {}'.format(prime_count, prime))
                for child in fam():
                    if Primes.is_prime(child):
                        print(child, end=' ')
                print()
                if prime_count == max_goal:
                    return
示例#5
0
def work():
    prime_count = 0
    number_count = 1  # skip 1, so we never get the ratio 0/1
    for x in range(1, 20000):
        for n in get_diagonals(x):
            number_count += 1
            if Primes.is_prime(n):
                prime_count += 1

        ratio = prime_count / number_count
        if x % 100 == 0:
            print('{:.2%}'.format(ratio))

        if ratio < 0.1:  # less than 10%
            print(spiral_length(x))
            return
示例#6
0
def check_primes(prime_count=5, primes=[], last_largest_prime=0):
    if len(primes
           ) >= 2:  # perform checks as soon as we can for early termination
        # Concatenate all possible permutations and ensure they're primes
        for p in permutations(primes, 2):
            if not Primes.is_prime(int(str(p[0]) + str(p[1]))):
                return

        # We can avoid some combinations... (previously checked ones)
        if len(primes) == prime_count:
            print('Succeed for {}, which adds to {}'.format(
                primes, sum(primes)))

    if len(primes) < prime_count:  # carry on getting more primes
        for prime in Primes(max_prime):
            if prime <= last_largest_prime:
                continue  # do not use a lower prime (already used)

            # Clone list, append prime, and dive into the next recursion level
            next_primes = primes[:]
            next_primes.append(prime)
            check_primes(prime_count, next_primes, prime)
示例#7
0
    but there is one other 4-digit increasing sequence.

    What 12-digit number do you form by concatenating the three terms in this sequence?'''


def are_permutation(a, b):
    a = tuple(str(a))
    b = tuple(str(b))
    for permutation in permutations(a):
        if permutation == b:
            return True
    return False


for i in range(1000, 10000):
    if not Primes.is_prime(i):
        continue

    for j in range(1000, 10000):
        next2 = i + j + j
        if next2 > 9999:  # must be 4-digit
            break
        if not Primes.is_prime(next2):
            continue

        next1 = i + j
        if not Primes.is_prime(next1):
            continue

        if not are_permutation(i, next2):
            continue
示例#8
0
        # If the first prime is already larger than the difference
        # between the latest largest sum and the maximum to check,
        # there is no point on carrying on checking, because any
        # sum will be larger with less numbers being summed
        if firstprime:
            firstprime = False
            if prime > maximum - largestprime:
                done = True
                break

        primesum += prime
        countsum += 1

        # We cannot check this, it is outside bounds
        if primesum >= maximum:
            break

        # We already found a larger one
        if countsum < largestcount:
            continue

        # Ensure it's prime, if it is, we found a new larger!
        if Primes.is_prime(primesum, False):
            Primes.ensure_cached_are_primes()
            largestprime = primesum
            largestcount = countsum

            print('Found {} which a sum of {} consecutive primes (from the {}th prime)'
                  .format(primesum, countsum, _skip + 1))
示例#9
0
def specific_solution():
    for prime1 in Primes(max_prime):
        s_prime1 = str(prime1)

        for prime2 in Primes(max_prime):
            # Avoid checking the same primes
            if prime2 <= prime1:
                continue
            s_prime2 = str(prime2)

            # Check if concatenating is prime
            if not Primes.is_prime(int(s_prime1 + s_prime2)):
                continue
            if not Primes.is_prime(int(s_prime2 + s_prime1)):
                continue

            # print('Checking level 2: {}, {}'.format(prime1, prime2))
            for prime3 in Primes(max_prime):
                # Avoid checking the same primes
                if prime3 <= prime2:
                    continue
                s_prime3 = str(prime3)

                # Check if concatenating is prime
                if not Primes.is_prime(int(s_prime1 + s_prime3)):
                    continue
                if not Primes.is_prime(int(s_prime3 + s_prime1)):
                    continue
                if not Primes.is_prime(int(s_prime2 + s_prime3)):
                    continue
                if not Primes.is_prime(int(s_prime3 + s_prime2)):
                    continue

                # print('Checking level 3: {}, {}, {}'.format(prime1, prime2, prime3))
                for prime4 in Primes(max_prime):
                    # Avoid checking the same primes
                    if prime4 <= prime3:
                        continue
                    s_prime4 = str(prime4)

                    # Check if concatenating is prime
                    if not Primes.is_prime(int(s_prime1 + s_prime4)):
                        continue
                    if not Primes.is_prime(int(s_prime4 + s_prime1)):
                        continue
                    if not Primes.is_prime(int(s_prime2 + s_prime4)):
                        continue
                    if not Primes.is_prime(int(s_prime4 + s_prime2)):
                        continue
                    if not Primes.is_prime(int(s_prime3 + s_prime4)):
                        continue
                    if not Primes.is_prime(int(s_prime4 + s_prime3)):
                        continue

                    # print('Checking level 4: {}, {}, {}, {}'.format(prime1, prime2, prime3, prime4))
                    for prime5 in Primes(max_prime):
                        # Avoid checking the same primes
                        if prime5 <= prime4:
                            continue
                        s_prime5 = str(prime5)

                        # Check if concatenating is prime
                        if not Primes.is_prime(int(s_prime1 + s_prime5)):
                            continue
                        if not Primes.is_prime(int(s_prime5 + s_prime1)):
                            continue
                        if not Primes.is_prime(int(s_prime2 + s_prime5)):
                            continue
                        if not Primes.is_prime(int(s_prime5 + s_prime2)):
                            continue
                        if not Primes.is_prime(int(s_prime3 + s_prime5)):
                            continue
                        if not Primes.is_prime(int(s_prime5 + s_prime3)):
                            continue
                        if not Primes.is_prime(int(s_prime4 + s_prime5)):
                            continue
                        if not Primes.is_prime(int(s_prime5 + s_prime4)):
                            continue

                        print('We did it! {} + {} + {} + {} + {} = {}'.format(
                            prime1, prime2, prime3, prime4, prime5,
                            prime1 + prime2 + prime3 + prime4 + prime5))

                        return