Example #1
0
def decompose_prime_square(n):
    """
    Attempt to write n as the sum of a prime and twice a square.

    >>> decompose_prime_square(9)
    (7, 1)
    >>> decompose_prime_square(15)
    (7, 2)
    >>> decompose_prime_square(21)
    (3, 3)
    >>> decompose_prime_square(25)
    (7, 3)
    >>> decompose_prime_square(27)
    (19, 2)
    >>> decompose_prime_square(33)
    (31, 1)
    """
    for p in up_to(n, primes()):
        if p == 2:
            continue
        residue = n - p
        assert residue % 2 == 0, residue
        square = residue // 2
        if is_perfect_square(square):
            return (p, isqrt(square))
Example #2
0
from utility import up_to

def fibonacci():
    a = 0
    b = 1
    yield a
    while True:
        yield b
        a, b = b, a + b

print(sum(f for f in up_to(4000000, fibonacci()) if f % 2 == 0))
Example #3
0
    for c in coefficients:
        value = value * x + c
    return value

def prime_sequence_length(coefficients):
    """
    Find the length of the prime sequence generated by a polynomial.

    >>> prime_sequence_length((1, 1, 41))
    40
    >>> prime_sequence_length((1, -79, 1601))
    80
    """
    for n in itertools.count():
        if not is_prime(eval_polynomial(coefficients, n)):
            return n

# We only need to check prime b, because we evaluate an n = 0, where the value == b.
MAX_NUM = 1000 - 1
max_poly = None
max_length = 0
for a in range(-MAX_NUM, MAX_NUM + 1):
    for b in up_to(MAX_NUM, primes()):
        poly = (1, a, b)
        l = prime_sequence_length(poly)
        if l > max_length:
            max_poly = poly
            max_length = l
_, max_a, max_b = max_poly
print(max_a * max_b)
Example #4
0
from utility import primes, digits_of, up_to

# In order to maximize the totient, we need to look for "sharp" numbers, having
# few prime factors, with those factors being large.

limit = 10**7

solutions = []
seen_primes = []
for a in up_to(limit // 2, primes()):
    for b in seen_primes:
        n = a * b
        if n > limit:
            break
        t = n
        t -= t // a
        if a != b:
            t -= t // b
        if sorted(digits_of(n)) == sorted(digits_of(t)):
            solutions.append((n, n / t))
    seen_primes.append(a)

solutions.sort(key=lambda x: x[1])
print(min(solutions, key=lambda x: x[1])[0])
Example #5
0
from utility import up_to, figurate_numbers

def letter_value(c):
    """
    >>> [letter_value(c) for c in 'abcdefghijklmnopqrstuvwxyz']
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
    """
    c = c.lower()
    return ord(c) - ord('a') + 1

def word_value(word):
    """
    >>> word_value('sky')
    55
    """
    return sum(letter_value(c) for c in word)

with open('data/words.txt') as f:
    word_data = f.read()

words = [w.strip('"').lower() for w in word_data.split(',')]

t_nums = set(up_to(30 * 26, figurate_numbers(3)))
print(sum(1 for w in words if word_value(w) in t_nums))

Example #6
0
def circular_primes(limit):
    prime_set = set(up_to(limit, primes()))
    for p in prime_set:
        if all((r in prime_set) for r in rotations(p)):
            yield p