所以我一直在尝试计算Bezout系数。我见过的所有解决方案都可以同时计算GCD。我已经有解决方法了。
所以这是我尝试过的代码的不同部分。
computeCoeffs :: Int -> Int -> (Int, Int)
computeCoeffs :: Int -> Int -> (Int, Int)
computeCoeffs a b =
let bq = a - r; (q, r) = a `quotRem` b
in if b == 0
then (1, 0)
else ((v, (u - q * v)) = computeCoeffs (a, b))
where a * v + b * (u - q * v) = gcd (a, b)
或代替最后一行
let (q, r) = a `quotRem` b
let r = a - bq
| b == 0 = (1, 0)
| otherwise (v, (u-qv)) = computeCoeffs (a, b)
where a * v + b(u - q * v) = gcd (a, b)
我尝试过切换位置,删除或添加防护装置,但仍然会引发错误。
答案 0 :(得分:0)
类似的东西应该起作用,需要处理“零除”错误的边缘情况
computeCoeff a b = go a b 1 0 0 1
where go a b s0 s1 t0 t1 | r==0 = (s1,t1)
| otherwise = go b r s1 s t1 t
where (q,r) = quotRem a b
(s,t) = (s0-s1*q, t0-t1*q)
> computeCoeff 240 46
(-9,47)