This article aims to determine the fastest method for computing y = x^2 for bigints expressed as dynamic arrays of unsigned DWORDs.
Given the representation of a bigint x as an array of DWORDs:
DWORD x[n 1] = { LSW, ......, MSW };
where:
Find the value of y = x^2 as quickly as possible without losing precision.
Assumptions:
The naive approach involves multiplying x by itself, which takes O(n^2) time. This can be expressed as:
y = x * x y = (x0 x1 x2 ...xn)*(x0 x1 x2 ...xn)
Expanding the product, we get:
y0 = x0*x0 y1 = x1*x0 x0*x1 y2 = x2*x0 x1*x1 x0*x2 y3 = x3*x0 x2*x1 x1*x2 ... y(2n-3) = xn(n-2)*x(n ) x(n-1)*x(n-1) x(n )*x(n-2) y(2n-2) = xn(n-1)*x(n ) x(n )*x(n-1) y(2n-1) = xn(n )*x(n )
The Karatsuba algorithm can be used to speed up multiplication to O(n^log2(3)). While it appears promising, the recursive nature of the algorithm can introduce a significant performance overhead for large numbers.
The Schönhage-Strassen algorithm offers even faster multiplication at O(nlog(n)(log(log(n)))) using a divide-and-conquer approach. However, this algorithm has practical limitations due to overflow issues and the need for modular arithmetic on unsigned integers.
For smaller numbers, the simple O(n^2) multiplication approach is the most efficient. For larger numbers, the Karatsuba multiplication algorithm is recommended. Further optimizations can be explored to improve performance, such as using FFT (Fast Fourier Transform) or NTT (Number Theoretic Transform).
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3