Open the context
Choose the project folder and name the files or behavior involved.
Merge up to eight congruences with residues within ±10^9 and pairwise-coprime moduli from 2 to 10^6. Test negative residues and empty input, rejecting Boolean components or shared factors while returning the least nonnegative solution and combined period.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
import math
def merge_congruences(pairs):
if type(pairs) is not list or len(pairs) > 8:
raise ValueError("crt.input")
answer, period = 0, 1
for pair in pairs:
if type(pair) not in (list, tuple) or len(pair) != 2 or any(type(x) is not int for x in pair):
raise ValueError("crt.pair")
residue, modulus = pair
if abs(residue) > 10**9 or not 2 <= modulus <= 10**6 or math.gcd(period, modulus) != 1:
raise ValueError("crt.modulus")
step = ((residue-answer)*pow(period, -1, modulus)) % modulus
answer += period*step
period *= modulus
answer %= period
return answer, period
The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.