利用分治算法来进行大数乘法

大数乘法

function multiply(x, y)

Input: n-bit positive integers x and y
Output: Their product

if n == 1: return xy

let x_L, x_R = ceil[n/2], floor[n/2] bits of x
let y_L, y_R = ceil[n/2], floor[n/2] bits of y

let P1 = multiply(x_L, y_L)
let P2 = multiply(x_R, y_R)
let P3 = multiply(x_L+x_R, y_L+y_R)

return P1 * power(2, 2*floor[n/2]) + (P3-P2-P1) * power(2, floor[n/2]) + P2

你可能感兴趣的:(算法)