Problem 136

completed January 17, 2021

Comments

I used the same solution for this problem as I did for problem 135. Just changed the inputs. Runs in about a minute.

Code

def solve(n, m):
    solutions = {}
    for i in range(3, 5*m//4+1):
        for j in range(4*i//5, i//2, -1):
            key = 4*i*j - 5*j**2
            if key >= m:
                break
            solutions[key] = solutions.get(key, 0) + 1
    count = 0
    for val in solutions.values():
        count += val == n
    return count

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

Tests

import pytest
from problem import solve

_test_solve = (
        (1, 100, 25),
        (2, 100, 8),
        (3, 100, 8),
        (4, 100, 2),
        (5, 100, 0),
        (1, 200, 44),
        (2, 200, 20),
        (3, 200, 16),
        (4, 200, 5),
        (5, 200, 2),
        (6, 200, 0),
)

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