from fractions import Fraction solutions = [] for low_kg in range(21): high_kg = 20 - low_kg salt = Fraction(low_kg, 10) + Fraction(3 * high_kg, 10) if salt == Fraction(18, 100) * 20: solutions.append((low_kg, high_kg, salt)) assert len(solutions) == 1 x, y, salt = solutions[0] result = {"low_kg": str(x), "high_kg": str(y), "final_salt_fraction": str(salt / 18)} import json print(json.dumps(result, ensure_ascii=False, sort_keys=True))