Utente:AlbertoZanna/Sandbox

Long integer cubes

modifica

In 2010 Alberto Zanoni proposed a new method to compute the cube of a long integer[1], faster with respect to the "natural cube algorithm" (NCA) suggested by the definition n3 = n2 × n. The idea is based on a divide and conquer approach (splitting in two) and the use of a modified unbalanced Toom-Cook multiplication method.

Consider a long integer n and split it into its high and low part. General formulas together with an example follow.

n(x) = ax+b 123456 = 123 × 103 + 456 (with x = 103)
n(x)3 = (ax+b)3 1234563 =
= (a3x3 + 3a2bx2 + 3ab2x + b3)         = 1860867 × 109 + 6898824 × 106 + 25576128 × 103 + 94818816 = 1881640295202816

NCA in the above setting implies first computing the square:

n(x)2 = (ax+b)2 123 4562
= a2x2 + 2abx + b2           15 129 × 106 + 112 176 × 103 + 207 936 = 15 241 383 936

The polynomial n(x)2 can be represented as follows (in the example: a3 = 15; a2 = 241; a1 = 383; a0 = 936)

n(x)2 = a3x3 + a2x2 + a1x + a0

so that the final product becomes

n(x)3 = (a3x3 + a2x2 + a1x + a0)(ax + b) = n4x4 + n3x3 + n2x2 + n1x + n0

If n2 is computed by using Karatsuba method (3 squares) and the product by using unbalanced Toom-3 method (5 products), 8 quadratic operations are needed in total. Zanoni's algorithm reduces them to 7 (one square less), by considering slightly different formulas.

From the polynomial point of view, consider the following product:

G'(x) = G1(x)G2(x) = (a2x2 + 3b2)(ax + 3b) = a3x3 + 3a2bx2 + 3ab2x + 9b3

differing by n(x)3 only in the constant term. The (extra) division by 9 (a fixed, "short" integer) needed to recover n(x)3 is a linear operation, which substitutes the 2ab product computation. Unfortunately, this cannot be directly applied to long integer cubing, as the final "recomposition" (in the example, setting x=103 and perform the final sums) is incompatible with G'(x) management. Indicating the first factor of G(x) as

G1(x) = g3x3 + g2x2 + g1x + g0

the correct multiplication to be done is

G(x) = (g3x3 + g2x2 + 3g1x + 27g0)(ax + 3b) = n4x4 + n3x3 + n2x2 + 9n1x + 81n0

It is then sufficient to divide the last two coefficients by 9 and 81, respectively, to obtain n(x)3 and perform the "recomposition". Actually, with a small modification of the unbalanced Toom-3 method inversion phase, it is possible to transform the division by 9 into a faster multiply-by-9-and-subtract operation.

Zanoni's method is effective only when the number of digits of n lies inside a range (depending on software and hardware environments), as using faster methods (higher Toom-Cook or Schönhage–Strassen algorithm) in other ranges revealed experimentally to be more effective.

  1. ^ Alberto Zanoni, Another sugar cube, please ! or sweetening third powers computation, Centro "Vito Volterra". Università di Roma "Tor Vergata", Technical Report n. 632, January 2010. http://bodrato.it/papers/zanoni.html#CIVV2010

modifica