conditions = lambda n: n % 5 == 2 and n % 7 == 3 and n % 9 == 4 residues = [n for n in range(315) if conditions(n)] assert len(residues) == 1 above = next(n for n in range(1000, 1315) if conditions(n)) result = {"first": residues[0], "period": 315, "first_at_least_1000": above} import json print(json.dumps(result, ensure_ascii=False, sort_keys=True))