Exemplo n.º 1
0
from utils import primes_below
from fractions import gcd

TOP = 100
primes = primes_below(TOP)

candidates = map(lambda x: x + 1, primes)
candidate_set = set(candidates)

values = []

TESTS = [(2, 5, 11), (2, 11, 47), (5, 11, 23), (5, 17, 53), (7, 11, 17),
         (7, 23, 71), (11, 23, 47), (17, 23, 31), (17, 41, 97), (31, 47, 71),
         (71, 83, 97)]

for (i, a) in enumerate(candidates):
    print(a)
    for b in candidates[(i + 1):]:
        if ((b * b) % a) != 0:
            continue

        candidate = (b * b) // a
        if candidate >= TOP:
            break

        if candidate in candidate_set:
            values.append((a - 1, b - 1, candidate - 1))

print(sum(map(sum, values)))
Exemplo n.º 2
0
from utils import primes_below
from memoizers import key_memoized

TOP = 1000000000

primes = primes_below(100)

@key_memoized
def count_multiplicities(prime_index, top):
    global primes

    if prime_index >= len(primes):
        return 0

    p = primes[prime_index]

    if p > top:
        return 0

    v1 = count_multiplicities(prime_index + 1, top)
    v2 = count_multiplicities(prime_index, top // p)

    return 1 + v1 + v2

print(count_multiplicities(0, TOP)) + 1
Exemplo n.º 3
0
from fractions import Fraction
from utils import primes_below
# index: (position, jumps_remaining) => probability of completing the sequence

FIELD = 500
STEPS = 15

primes = set(primes_below(FIELD + 1))

sequence = 'PPPPNNPPPNPPNPN'


def to_letter(n):
    return 'P' if n in primes else 'N'


probability_matrix = [[0 for x in range(FIELD + 1)] for y in range(STEPS)]
probability_matrix[0] = [Fraction(1, FIELD)] * (FIELD + 1)
probability_matrix[0][0] = 0

for jump in range(1, STEPS):
    probability_matrix[jump][2] = probability_matrix[jump - 1][1]
    probability_matrix[jump][FIELD - 1] = probability_matrix[jump - 1][FIELD]
    for j in range(2, 500):
        probability_matrix[jump][j - 1] += Fraction(
            1, 2) * (probability_matrix[jump - 1][j])
        probability_matrix[jump][j + 1] += Fraction(
            1, 2) * (probability_matrix[jump - 1][j])

hit_all_ps = 1
Exemplo n.º 4
0
        to_turn_off = len(prev_str) - len(p_str)

        m = sum([len(segs[int(i)]) for i in prev_str[:to_turn_off]])

        for d in range(len(p_str)):
            d_prev = int(prev_str[d + to_turn_off])
            d_p = int(p_str[d])

            m += transitions[d_prev][d_p]

    if (p >= 10):
        root = sum(map(int, p_str))
        next_m, next_s = clock_counts(root, prev=p)
        m, s = m + next_m, s + next_s
    else:
        m += sum([len(segs[int(i)]) for i in str(p)])

    return m, s


for p in primes_below(2 * (10**7)):
    if p < 10**7:
        continue

    m, s = clock_counts(p)
    max_count += m
    sam_count += s

print(sam_count - max_count)
Exemplo n.º 5
0
#!/usr/bin/env python3
from utils import primes_below

TOP = 100000000

total = 0

primes = primes_below(TOP * 1.6)
prime_set = set(primes)

vals = [True] * (TOP + 1)

for p in primes:
    if 2 * p > TOP:
        break

    print(p)
    vals[p] = False
    for i in range(2, (TOP // p) + 1):
        if i + p not in prime_set:
            vals[i * p] = False

v = set([i for i in range(1, TOP + 1) if vals[i]])
v2 = set(map(lambda x: x - 1, prime_set))

print(sum(v & v2))