Algorithm Sanjoy Dasgupta中提供的除法算法
我正在读Sanjoy Dasgupta的《算法》一书中的除法算法。下面提到除法算法Algorithm Sanjoy Dasgupta中提供的除法算法,algorithm,division,Algorithm,Division,我正在读Sanjoy Dasgupta的《算法》一书中的除法算法。下面提到除法算法 function divide(x,y) Input: Two n-bit integers x and y, where y ≥ 1 Output: The quotient and remainder of x divided by y if x = 0: return (q,r) = (0,0) (q,r) = divide(x/2,y) q = 2·q, r = 2·r if x is odd: r
function divide(x,y)
Input: Two n-bit integers x and y, where y ≥ 1
Output: The quotient and remainder of x divided by y
if x = 0: return (q,r) = (0,0)
(q,r) = divide(x/2,y)
q = 2·q, r = 2·r
if x is odd: r = r + 1
if r ≥ y: r = r−y, q = q + 1
return (q,r)
我对上述算法的问题如下:
感谢乘以2以补偿之前除以2的结果。
如何编写递归公式
我觉得所介绍的过程是递归的-您能详细说明一下使用递归
得到的结果吗<代码>为什么dong q=2.q和r=2.r(我们在做什么?)正如在q=2.q,r=2.r
的目的是什么?