Problem 130

completed January 18, 2021

Comments

I got the answer to this one pretty quicking. I started with my code for problem 129 and just edited it enough to solve this one. Runs in under a second.

Code

def isprime(n):
    for p in range(7, int(n**0.5) + 1, 2):
        if n % p == 0:
            return False
    return True

def solve(m):
    n = 1
    total = count = 0
    while True:
        n += 2
        if n % 5 == 0:
            n += 2
        if isprime(n):
            continue
        k = r = 1
        while r != 0:
            r = (r * 10 + 1) % n
            k += 1
        if (n - 1) % k == 0:
            total += n
            count += 1
            if count == m:
                return total

if __name__ == '__main__':
    import sys
    n = eval(sys.argv[1])
    print(solve(n))

TESTS:

import pytest
from problem import solve

_test_solve = (
        (5, 91 + 259 + 451 + 481 + 703),
)

@pytest.mark.parametrize('n,expect', _test_solve)
def test_solve(n, expect):
    assert expect == solve(n)