Пример #1
0
# 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
start = time()

from module import sieve, is_prime

def rotate(n: int) -> list:
    r = set()
    for i in range(len(str(n))):
        r.add(int(f'{str(n)[i:]}{str(n)[:i]}'))
    return r

circular_primes = []
for n in sieve(2, 1000000):
    counter = 0
    if '0' not in str(n):
        rotations = rotate(n)
        for r in rotations:
            if is_prime(r):
                counter += 1
        if counter == len(rotations):
            circular_primes.append(n)

print(len(circular_primes))

print(time() - start)

# Answer: 55
Пример #2
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
# (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 time import time
start = time()

from module import is_prime
for i in range(1488, 6671):
    j = i + 3330
    k = i + 6660
    if (is_prime(i) and is_prime(j) and is_prime(k)
            and sorted(str(i)) == sorted(str(j)) == sorted(str(k))):
        print(str(i) + str(j) + str(k))

print(time() - start)

# Answer: 296962999629
Пример #3
0
age = input("How old are you?")
age_plus3 = int(age) + 3
print(age_plus3.denominator)

hours = int(input("Enter hours: "))
rate = float(input("Enter rate: "))
pay = hours * rate
print("Payment: " + str(pay))

# 3.1
#   CONDITIONAL sentence

x = 10
if x < 10:
    print("less")
elif x > 10:
    print("greater")
print("end")

# 4.4 Math functions
print(math)
print(type)
print(max)

print("Some little changes")

for i in range(41):
    print(i)
    print(module.nice_prime(i))
    print(module.is_prime(module.nice_prime(i)))
Пример #4
0
# Find the sum of the only eleven primes that are both truncatable from left to right and right to left.
# NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.

from time import time
start = time()

from module import sieve, is_prime
interesting_primes = []
for n in sieve(20, 750000):
    n1 = n
    n2 = n
    # left
    counter = 1
    if '0' not in str(n):
        for i in range(len(str(n1)) - 1):
            if is_prime(int(str(n1)[1:])):
                counter += 1
            n1 = int(str(n1)[1:])
        if counter == len(str(n)):
            # right
            counter = 1
            for i in range(len(str(n2)) - 1):
                if is_prime(int(str(n2)[:-1])):
                    counter += 1
                    n2 = int(str(n2)[:-1])
            if counter == len(str(n)):
                interesting_primes.append(n)

print(interesting_primes)
print(sum(interesting_primes))
Пример #5
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 time import time
start = time()

from module import sieve, is_prime
primes = sieve(2, 10000)

fin_seq = []
l = len(primes)
j = l
while j != 0:
    i = 0
    while i + j < l + 1:
        seq = primes[i:i + j]
        if sum(seq) <= 1_000_000:
            if is_prime(sum(seq)):
                if len(seq) > len(fin_seq):
                    fin_seq = seq
        i += 1
    j -= 1
print(sum(fin_seq))

print(time() - start)

# Answer: 997651
Пример #6
0
# 27 = 19 + 2 × (2^2)
# 33 = 31 + 2 × (1^2)
# It turns out that the conjecture was false.
# What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?

from time import time
start = time()

from math import sqrt
from module import is_prime
primes = []

done = False
n = 2
while done is False:
    if is_prime(n):
        primes.append(n)
    elif n % 2 == 1:
        it_can = False
        for p in primes:
            square = sqrt((n - p) / 2)
            if square == int(square):
                it_can = True
        if it_can is False:
            print(n)
            done = True
    n += 1

print(time() - start)

# Answer: 5777
Пример #7
0
# find primes up to 1000 - these are possible values for b, since when n is 0, b must be prime
primes_up_to_one_thousand = sieve(2, 1000)
primes = primes_up_to_one_thousand.copy()

xy = 0
largest = 0

for x in primes_up_to_one_thousand:
    for y in primes_up_to_one_thousand:
        n = 0

        # positive 'a' and positive 'b'
        while True:
            quadratic = (n * (n + x)) + y
            if is_prime(quadratic) is False:
                if n - 1 > largest:
                    largest = (n - 1)
                    xy = x * y
                break
            n += 1

        n = 0

        # negative 'a' and positive 'b'
        while True:
            quadratic = (n * (n - x)) + y
            if is_prime(quadratic) is False or quadratic < 0:
                if n - 1 > largest:
                    largest = n - 1
                    xy = -1 * x * y
Пример #8
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 time import time
start = time()

from module import is_pandigital, is_prime

# sum of digits from 9 down to 1 is divisble by 3
# sum of digits from 8 down to 1 is divisble by 3
# sum of digits from 7 down to 1 is not divisble by 3
n = 7654321
while not(is_pandigital(n, 7) and is_prime(n)):
    n -= 2
print(n)

print(time() - start)

# Answer: 7652413