Exemplo n.º 1
0
from utils.prime import is_prime

ans = 0
for i in range(2, 2000000):
    if is_prime(i):
        ans += i

print(ans)
Exemplo n.º 2
0
# -*- coding: utf-8 -*-

"""
Problem 41 - Pandigital prime
=============================

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 utils.number import gen_permutation
from utils.prime import is_prime

digits = [9, 8, 7, 6, 5, 4, 3, 2, 1]

found = False
i = 0
while not found and i < len(digits):
    for permutation in gen_permutation(digits[i:]):
        n = int(''.join(map(str, permutation)))
        if is_prime(n):
            found = True
            print(n)
            break
    i += 1

# Answer: 7652413
Exemplo n.º 3
0
    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 utils.prime import is_prime, gen_prime

primes = []
for prime in gen_prime():
    if prime > 1000:
        break
    primes.append(prime)

max_count = 0
product = 0
for a in range(-999, 1000):
    for b in primes:
        n = 0
        while is_prime(n**2 + a * n + b):
            n += 1
        if n > max_count:
            max_count = n
            product = a * b

print(product)

# Answer: -59231
Exemplo n.º 4
0
def check(num):
    x = int(math.sqrt(num // 2))  # x is the bound for square number
    for i in range(1, x + 1):
        if is_prime(num - 2 * i * i):
            return True
    return False
Exemplo n.º 5
0
# in the name of God
import math

from utils.prime import is_prime


def check(num):
    x = int(math.sqrt(num // 2))  # x is the bound for square number
    for i in range(1, x + 1):
        if is_prime(num - 2 * i * i):
            return True
    return False


for i in range(3, 10000000, 2):
    if not is_prime(i) and not check(i):
        print(i)
        break
Exemplo n.º 6
0
 def test_is_prime(self, n: int, expected: bool) -> None:
     self.assertEqual(is_prime(n), expected)
Exemplo n.º 7
0
import math

from utils.prime import is_prime

pivot = 600851475143

ans = 0
for i in range(2, int(math.sqrt(pivot)) + 10):
    if pivot % i != 0:
        continue

    if is_prime(i):
        ans = max(ans, i)
    if is_prime(pivot // i):
        ans = max(ans, pivot // i)

print(ans)
Exemplo n.º 8
0
from utils.prime import is_prime

primes = []
for i in range(2, 10 ** 6):
    if is_prime(i):
        primes.append(i)

# calculate partial sum for consecutive primes
partial_sum = [primes[0]]
for i in range(1, len(primes)):
    partial_sum.append(partial_sum[i - 1] + primes[i])

maxi = 0
ans = -1
for i in range(len(primes)):
    for j in range(i + 1, min(i + 1000, len(primes))):  # do you know why i + 1000? ask if u cant get the meaning
        primes_sum = partial_sum[j] - (partial_sum[i - 1] if i > 0 else 0)
        if primes_sum < 10 ** 6 and j - i + 1 > maxi and is_prime(primes_sum):
            maxi = j - i + 1
            ans = primes_sum
print(maxi, ans)