Пример #1
0
def main(verbose=False):
    for n in range(10):
        if not check_formula(n):
            raise Exception("Proposed formula for M(2**k + 1) incorrect.")

    modulus = 7 ** 9
    p_2 = 4 * modular_exponentiate(2, 10, 18, modulus) - 2
    p_3 = 3 * modular_exponentiate(3, 10, 18, modulus) - 1
    p_4 = 4 * modular_exponentiate(4, 10, 18, modulus) - 1

    return (p_4 * inverse_mod_n(3, modulus) -
            p_3 * inverse_mod_n(2, modulus) + p_2) % (modulus)
Пример #2
0
def main(verbose=False):
    PRIMES = sieve(10 ** 7)
    running_sum = 0
    for prime in PRIMES:
        if prime not in [2, 5]:
            running_sum += inverse_mod_n(10, prime)
    return running_sum
Пример #3
0
def main(verbose=False):
    PRIMES = sieve(10 ** 6 + 3)  # 10**6 + 3 is the final value of p_2

    running_sum = 0
    for index in range(2, len(PRIMES) - 1):
        p_1 = PRIMES[index]
        p_2 = PRIMES[index + 1]
        ten_inverse = inverse_mod_n(10, p_2)
        digits = len(str(p_1))
        k = (ten_inverse ** digits) * (p_2 - p_1) % p_2
        running_sum += int('%s%s' % (k, p_1))
    return running_sum
Пример #4
0
def main(verbose=False):
    p_3, c, P_2 = sequence('UDDDUdddDDUDDddDdDddDDUDDdUUDd', 0, 0, 1)
    # Here a_1 = s**(-1)(y) = ((3**(p_3))*y + c)/P_2
    # Since we need a_1 > 10**15, (3**(p_3))*y > (10**15)*P_2 - c
    min_y = ((10 ** 15) * P_2 - c) / (3 ** p_3)

    # We also need (3**(p_3))*y == -c mod P_2
    y_residue = inverse_mod_n(3 ** (p_3), P_2) * (-c) % P_2

    # integer division intended to find nearest multiple of P_2
    y = P_2 * (min_y / P_2) + y_residue
    if y < min_y:
        y += P_2

    return ((3 ** p_3) * y + c) / P_2
Пример #5
0
def last5(n):
    if n < 8:
        # The remaining part is always divisible by
        # 2**5 for n > 7, these are special cases
        solutions = {0: 1,
                     1: 1,
                     2: 2,
                     3: 6,
                     4: 24,
                     5: 12,
                     6: 72,
                     7: 504}
        return solutions[n]

    if n <= 5 ** 5:
        residues = {}
        for i in range(1, n + 1):
            to_add = robust_divide(i, 5)
            # if to_add is not in residues, sets to 1
            # (default 0 returned by get)
            residues[to_add] = residues.get(to_add, 0) + 1
    else:
        residues = {}
        for residue in range(1, 5 ** 5):
            if residue % 5 != 0:
                residues[residue] = (n - residue) / (5 ** 5) + 1
        max_power = int(floor(log(n) / log(5)))
        for power in range(1, max_power + 1):
            biggest_quotient = n / (5 ** power)
            for residue in range(1, 5 ** 5):
                if residue % 5 != 0:
                    residues[residue] += ((biggest_quotient - residue) /
                                          (5 ** 5) + 1)

    product = 1
    for residue, power in residues.items():
        power_apply = power % (4 * (5 ** 4))  # PHI(5**5)
        product = (product * (residue ** power_apply)) % (5 ** 5)
    fives = num_factors_fact(n, 5) % (4 * (5 ** 4))  # PHI(5**5)
    inverse = inverse_mod_n(2, 5 ** 5)
    product = (product * (inverse ** fives)) % (5 ** 5)

    return (product * unit_a_zero_b(5 ** 5, 2 ** 5)) % 10 ** 5