Example #1
0
def is_truncatable(prime):
    prime = str(prime)
    for i in range(1, len(prime)):          # Remove digits from the left
        if not u.is_prime(int(prime[i:])):
            return False
    for i in range(len(prime), 0, -1):      # If none of the above checks fail, remove digits from the right
        if not u.is_prime(int(prime[:i])):
            return False
    return True         # We only reach this is we pass through both truncation directions
Example #2
0
def is_truncatable(prime):
    prime = str(prime)
    for i in range(1, len(prime)):  # Remove digits from the left
        if not u.is_prime(int(prime[i:])):
            return False
    for i in range(
            len(prime), 0, -1
    ):  # If none of the above checks fail, remove digits from the right
        if not u.is_prime(int(prime[:i])):
            return False
    return True  # We only reach this is we pass through both truncation directions
Example #3
0
def is_circular_prime(num):
    rotations = generate_rotations(i)
    # Don't check num for primeness again
    rotations.remove(num)
    for rotation in rotations:
        if not is_prime(rotation):
            return False
    return True
Example #4
0
def main():
    utilities.generate_primes_sieve(100000)
    primes = sorted([
        ''.join(p) for p in itertools.chain.from_iterable(
            itertools.permutations(all_digits, r)
            for r in range(1,
                           len(all_digits) + 1))
        if utilities.is_prime(int(''.join(p)))
    ],
                    key=lambda x: sorted(x))
    prime_groups = [
        (''.join(k), len(list(g)))
        for k, g in itertools.groupby(primes, key=lambda x: sorted(x))
    ]
    return find_sets(prime_groups, all_digits, 0)
Example #5
0
def main():
	utilities.generate_primes_sieve(1000000)
	prime_set = set(utilities.primes)
	prime_count = 0
	total_count = 1
	n = 1
	for i in range(2, 1000000, 2):
		for j in range(3):
			n += i
			total_count += 1
			if n in prime_set or utilities.is_prime(n):
				prime_count += 1
		n += i
		total_count += 1
		if prime_count * 10 < total_count:
			return i + 1
Example #6
0
def get_large(n):
    if is_prime(n):
        return n

    largest = -1
    while n % 2 == 0:
        largest = 2
        n /= 2

    for i in range(3, int(math.sqrt(n)) + 1, 2):
        while n % i == 0:
            largest = i
            n /= i

    if n > 2:
        return n
    return largest
def main():
    utilities.generate_primes_sieve(100000)
    total = 0
    for d in range(10):
        base = list(itertools.repeat(str(d), DIGITS))
        digit_total = 0
        for i in range(1, DIGITS):
            index_combos = list(itertools.combinations(range(DIGITS), i))
            digit_groups = list(itertools.product("0123456789", repeat=i))
            for combo in index_combos:
                for group in digit_groups:
                    p = base.copy()
                    for j in range(i):
                        p[combo[j]] = group[j]
                    p = int(''.join(p))
                    if len(str(p)) == DIGITS and utilities.is_prime(p):
                        digit_total += p
            if digit_total > 0:
                break
        total += digit_total
    return total
Example #8
0
# 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 utilities import is_prime, generate_rotations


def is_circular_prime(num):
    rotations = generate_rotations(i)
    # Don't check num for primeness again
    rotations.remove(num)
    for rotation in rotations:
        if not is_prime(rotation):
            return False
    return True


num_circular_primes = 0

for i in range(2, 1000000):
    if is_prime(i) and is_circular_prime(i):
        num_circular_primes += 1

print(num_circular_primes)
def test_is_prime():
    assert is_prime(3) == True
    assert is_prime(4) == False
    assert is_prime(5) == True
    assert is_prime(6) == False
Example #10
0
import utilities

a_max = 0
b_max = 0
n_max = 0

for a in range(-999, 1000):
    for b in utilities.primes_below(0, 1001):
        for signed_b in [-b, b]:
            n = 0
            while utilities.is_prime(abs(n * n + a * n + signed_b)):
                n += 1

            if n > n_max:
                a_max = a
                b_max = signed_b
                n_max = n

print(a_max * b_max)
Example #11
0
# The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (
# i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.
#
# There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property,
# but there is one other 4-digit increasing sequence.
#
# What 12-digit number do you form by concatenating the three terms in this sequence?

from utilities import is_prime, is_permutation

four_digit_primes = [i for i in range(1000, 10000) if is_prime(i)]
answer = None

for i in range(len(four_digit_primes)):
    for j in range(i + 1, len(four_digit_primes)):
        first_prime = four_digit_primes[i]
        second_prime = four_digit_primes[j]
        if not is_permutation(second_prime, first_prime):
            continue
        third_candidate = second_prime + (second_prime - first_prime)
        if is_prime(third_candidate) and is_permutation(
                third_candidate, second_prime):
            print(first_prime, second_prime, third_candidate)
Example #12
0
# 41 = 2 + 3 + 5 + 7 + 11 + 13
#
# This is the longest sum of consecutive primes that adds to a prime below one-hundred.
#
# The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.
#
# Which prime, below one-million, can be written as the sum of the most consecutive primes?
from utilities import is_prime, all_prime_numbers

primes = []
prime_generator = all_prime_numbers()

for i in range(10000):
    primes.append(next(prime_generator))

longest_streak = 0
longest_streak_prime = None

for i in range(len(primes)):
    for j in range(i, len(primes)):
        prime_sum = sum(primes[i:j])
        if prime_sum > 1000000:
            # Stop increasing the streak test length
            break
        if (j - i) > longest_streak and is_prime(prime_sum):
            longest_streak = j - i
            longest_streak_prime = sum(primes[i:j])

print('Sum of {} primes:'.format(longest_streak))
print(longest_streak_prime)
Example #13
0
#
# n^2+an+b
#
# , where |a|<1000 and |b|≤1000
#
# where |n|
# is the modulus/absolute value of n
# e.g. |11|=11 and |−4|=4
#
# Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of
# primes for consecutive values of n, starting with n=0.

from utilities import is_prime, solve_quadratic

longest_streak = 0
longest_streak_coefficients = 0, 0

for a in range(-999, 1001):
    for b in range(-999, 1001):
        n = 0
        while is_prime(int(solve_quadratic(a, b, n))):
            n += 1

        if n > longest_streak:
            longest_streak = n
            longest_streak_coefficients = a, b

print(longest_streak)
print(longest_streak_coefficients)
print(longest_streak_coefficients[0] * longest_streak_coefficients[1])
# https://projecteuler.net/problem=58

from utilities import is_prime

# Return the corners of layer n, indexed from 1 is the central 1
def corners(num):
    if(num == 1): return [1]
    return sorted([pow(num*2-1,2)-(num-1)*i*2 for i in range(0,4)])

numPrime, layer = 3, 2
while float(numPrime)/float(4*layer-3) > 0.1:
    layer += 1
    numPrime += sum([is_prime(i) for i in corners(layer)])

print((layer-1)*2+1)
Example #15
0
import random
from utilities import is_prime

count = 0
lower_limit = 0
upper_limit = 100
numbers_needed = int(input("How many random primes do you need?"))
primes = []

for number in range(lower_limit, upper_limit):
    if is_prime(number):
        primes.append(number)
if len(primes) < numbers_needed:
    print(
        f"Upper and Lower limits are too low to produce {numbers_needed} random primes"
    )
else:
    while count < numbers_needed:
        selection = random.choice(primes)
        primes.remove(selection)
        print(f"{selection} is your random prime")
        count += 1
Example #16
0
# We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For
# example, 2143 is a 4-digit pandigital and is also prime.
#
# What is the largest n-digit pandigital prime that exists?

from utilities import is_prime, all_string_permutations

pandigital_string = '987654321'

pandigitals = []

for i in range(9):
    pandigitals.extend([
        int(perm) for perm in all_string_permutations(
            pandigital_string[i:len(pandigital_string)])
    ])

pandigitals = reversed(sorted(pandigitals))

for pandigital in pandigitals:
    if is_prime(pandigital):
        print(pandigital)
        exit(0)