Jump to content

Chinese remainder theorem: Difference between revisions

m
Remove unused variables from Crystal & Ruby implementations
No edit summary
m (Remove unused variables from Crystal & Ruby implementations)
Line 518:
<lang crystal>def extended_gcd(a, b)
last_remainder, remainder = a.abs, b.abs
x, last_x, y, last_y = 0, 1, 1, 0
 
until remainder == 0
Line 525:
last_remainder = tmp
x, last_x = last_x - quotient * x, x
y, last_y = last_y - quotient * y, y
end
 
return last_remainder, last_x * (a < 0 ? -1 : 1)
end
Line 541:
 
def chinese_remainder(mods, remainders)
max = mods.reduce { |acc, i| acc * i }product
series = remainders.zip(mods).map { |r, m| r * max * invmod(max // m, m) // m }
return series.sum % max
Line 2,373:
def extended_gcd(a, b)
last_remainder, remainder = a.abs, b.abs
x, last_x, y, last_y = 0, 1, 1, 0
while remainder != 0
last_remainder, (quotient, remainder) = remainder, last_remainder.divmod(remainder)
x, last_x = last_x - quotient*x, x
y, last_y = last_y - quotient*y, y
end
return last_remainder, last_x * (a < 0 ? -1 : 1)
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.