# Computational complexity of mathematical operations

Graphs of functions commonly used in the analysis of algorithms, showing the number of operations ${\displaystyle N}$ versus input size ${\displaystyle n}$ for each function

The following tables list the computational complexity of various algorithms for common mathematical operations.

Here, complexity refers to the time complexity of performing computations on a multitape Turing machine.[1] See big O notation for an explanation of the notation used.

Note: Due to the variety of multiplication algorithms, ${\displaystyle M(n)}$ below stands in for the complexity of the chosen multiplication algorithm.

## Arithmetic functions

Operation Input Output Algorithm Complexity
Addition Two ${\displaystyle n}$-digit numbers, ${\displaystyle N}$ and ${\displaystyle N}$ One ${\displaystyle n+1}$-digit number Schoolbook addition with carry ${\displaystyle \Theta (n),\Theta (\log(N))}$
Subtraction Two ${\displaystyle n}$-digit numbers, ${\displaystyle N}$ and ${\displaystyle N}$ One ${\displaystyle n+1}$-digit number Schoolbook subtraction with borrow ${\displaystyle \Theta (n),\Theta (\log(N))}$
Multiplication Two ${\displaystyle n}$-digit numbers
One ${\displaystyle 2n}$-digit number Schoolbook long multiplication ${\displaystyle O(n^{2})}$
Karatsuba algorithm ${\displaystyle O(n^{1.585})}$
3-way Toom–Cook multiplication ${\displaystyle O(n^{1.465})}$
${\displaystyle k}$-way Toom–Cook multiplication ${\displaystyle O(n^{\frac {\log 2k-1}{\log k}})}$
Mixed-level Toom–Cook (Knuth 4.3.3-T)[2] ${\displaystyle O(n\,2^{\sqrt {2\log n}}\,\log n)}$
Schönhage–Strassen algorithm ${\displaystyle O(n\log n\log \log n)}$
Fürer's algorithm[3] ${\displaystyle O(n\log n\,2^{2\log ^{*}n})}$
Harvey-Hoeven algorithm[4] [5] ${\displaystyle O(n\log n)}$
Division Two ${\displaystyle n}$-digit numbers One ${\displaystyle n}$-digit number Schoolbook long division ${\displaystyle O(n^{2})}$
Burnikel-Ziegler Divide-and-Conquer Division [6] ${\displaystyle O(M(n)\log n)}$
Newton–Raphson division ${\displaystyle O(M(n))}$
Square root One ${\displaystyle n}$-digit number One ${\displaystyle n}$-digit number Newton's method ${\displaystyle O(M(n))}$
Modular exponentiation Two ${\displaystyle n}$-digit integers and a ${\displaystyle k}$-bit exponent One ${\displaystyle n}$-digit integer Repeated multiplication and reduction ${\displaystyle O(M(n)\,2^{k})}$
Exponentiation by squaring ${\displaystyle O(M(n)\,k)}$
Exponentiation with Montgomery reduction ${\displaystyle O(M(n)\,k)}$

## Algebraic functions

Operation Input Output Algorithm Complexity
Polynomial evaluation One polynomial of degree ${\displaystyle n}$ with fixed-size coefficients One fixed-size number Direct evaluation ${\displaystyle \Theta (n)}$
Horner's method ${\displaystyle \Theta (n)}$
Polynomial gcd (over ${\displaystyle \mathbb {Z} [x]}$ or ${\displaystyle F[x]}$) Two polynomials of degree ${\displaystyle n}$ with fixed-size integer coefficients One polynomial of degree at most ${\displaystyle n}$ Euclidean algorithm ${\displaystyle O(n^{2})}$
Fast Euclidean algorithm (Lehmer)[7] ${\displaystyle O(M(n)\log n)}$

## Special functions

Many of the methods in this section are given in Borwein & Borwein.[8]

### Elementary functions

The elementary functions are constructed by composing arithmetic operations, the exponential function (${\displaystyle \exp }$), the natural logarithm (${\displaystyle \log }$), trigonometric functions (${\displaystyle \sin ,\cos }$), and their inverses. The complexity of an elementary function is equivalent to that of its inverse, since all elementary functions are analytic and hence invertible by means of Newton's method. In particular, if either ${\displaystyle \exp }$ or ${\displaystyle \log }$ in the complex domain can be computed with some complexity, then that complexity is attainable for all other elementary functions.

Below, the size ${\displaystyle n}$ refers to the number of digits of precision at which the function is to be evaluated.

Algorithm Applicability Complexity
Taylor series; repeated argument reduction (e.g. ${\displaystyle \exp(2x)=[\exp(x)]^{2}}$) and direct summation ${\displaystyle \exp ,\log ,\sin ,\cos ,\arctan }$ ${\displaystyle O(M(n)n^{1/2})}$
Taylor series; FFT-based acceleration ${\displaystyle \exp ,\log ,\sin ,\cos ,\arctan }$ ${\displaystyle O(M(n)n^{1/3}(\log n)^{2})}$
Taylor series; binary splitting + bit-burst algorithm[9] ${\displaystyle \exp ,\log ,\sin ,\cos ,\arctan }$ ${\displaystyle O(M(n)(\log n)^{2})}$
Arithmetic–geometric mean iteration[10] ${\displaystyle \exp ,\log ,\sin ,\cos ,\arctan }$ ${\displaystyle O(M(n)\log n)}$

It is not known whether ${\displaystyle O(M(n)\log n)}$ is the optimal complexity for elementary functions. The best known lower bound is the trivial bound ${\displaystyle \Omega }$${\displaystyle (M(n))}$.

### Non-elementary functions

Function Input Algorithm Complexity
Gamma function ${\displaystyle n}$-digit number Series approximation of the incomplete gamma function ${\displaystyle O(M(n)n^{1/2}(\log n)^{2})}$
Fixed rational number Hypergeometric series ${\displaystyle O(M(n)(\log n)^{2})}$
${\displaystyle m/24}$, for ${\displaystyle m}$ integer. Arithmetic-geometric mean iteration ${\displaystyle O(M(n)\log n)}$
Hypergeometric function ${\displaystyle {}_{p}\!F_{q}}$ ${\displaystyle n}$-digit number (As described in Borwein & Borwein) ${\displaystyle O(M(n)n^{1/2}(\log n)^{2})}$
Fixed rational number Hypergeometric series ${\displaystyle O(M(n)(\log n)^{2})}$

### Mathematical constants

This table gives the complexity of computing approximations to the given constants to ${\displaystyle n}$ correct digits.

Constant Algorithm Complexity
Golden ratio, ${\displaystyle \phi }$ Newton's method ${\displaystyle O(M(n))}$
Square root of 2, ${\displaystyle {\sqrt {2}}}$ Newton's method ${\displaystyle O(M(n))}$
Euler's number, ${\displaystyle e}$ Binary splitting of the Taylor series for the exponential function ${\displaystyle O(M(n)\log n)}$
Newton inversion of the natural logarithm ${\displaystyle O(M(n)\log n)}$
Pi, ${\displaystyle \pi }$ Binary splitting of the arctan series in Machin's formula ${\displaystyle O(M(n)(\log n)^{2})}$[11]
Gauss–Legendre algorithm ${\displaystyle O(M(n)\log n)}$[11]
Euler's constant, ${\displaystyle \gamma }$ Sweeney's method (approximation in terms of the exponential integral) ${\displaystyle O(M(n)(\log n)^{2})}$

## Number theory

Algorithms for number theoretical calculations are studied in computational number theory.

Operation Input Output Algorithm Complexity
Greatest common divisor Two ${\displaystyle n}$-digit integers One integer with at most ${\displaystyle n}$ digits Euclidean algorithm ${\displaystyle O(n^{2})}$
Binary GCD algorithm ${\displaystyle O(n^{2})}$
Left/right k-ary binary GCD algorithm[12] ${\displaystyle O(n^{2}\log n)}$
Stehlé–Zimmermann algorithm[13] ${\displaystyle O(M(n)\log n)}$
Schönhage controlled Euclidean descent algorithm[14] ${\displaystyle O(M(n)\log n)}$
Jacobi symbol Two ${\displaystyle n}$-digit integers ${\displaystyle 0}$, ${\displaystyle -1}$ or ${\displaystyle 1}$ Schönhage controlled Euclidean descent algorithm[15] ${\displaystyle O(M(n)\log n)}$
Stehlé–Zimmermann algorithm[16] ${\displaystyle O(M(n)\log n)}$
Factorial A positive integer less than ${\displaystyle m}$ One ${\displaystyle O(m\log m)}$-digit integer Bottom-up multiplication ${\displaystyle O(M(m^{2})\log m)}$
Binary splitting ${\displaystyle O(M(m\log m)\log m)}$
Exponentiation of the prime factors of ${\displaystyle m}$ ${\displaystyle O(M(m\log m)\log \log m)}$,[17]
${\displaystyle O(M(m\log m))}$[1]
Primality test A ${\displaystyle n}$-digit integer True or false AKS primality test ${\displaystyle O(n^{6})}$[18] [19] or ${\displaystyle O(n^{6+\varepsilon })}$[20][21],
${\displaystyle O(n^{3})}$, assuming Agrawal's conjecture
Elliptic curve primality proving ${\displaystyle O(n^{4+\varepsilon })}$ heuristically[22]
Baillie-PSW primality test ${\displaystyle O(n^{2+\varepsilon })}$[23][24]
Miller–Rabin primality test ${\displaystyle O(kn^{2+\varepsilon })}$[25]
Solovay–Strassen primality test ${\displaystyle O(kn^{2+\varepsilon })}$[25]
Integer factorization A ${\displaystyle b}$-bit input integer A set of factors General number field sieve ${\displaystyle O((1+\varepsilon )^{b})}$[nb 1]
Shor's algorithm ${\displaystyle O(b^{3})}$, on a quantum computer

## Matrix algebra

The following complexity figures assume that arithmetic with individual elements has complexity O(1), as is the case with fixed-precision floating-point arithmetic or operations on a finite field.

Operation Input Output Algorithm Complexity
Matrix multiplication Two ${\displaystyle n\times n}$ matrices One ${\displaystyle n\times n}$ matrix Schoolbook matrix multiplication ${\displaystyle O(n^{3})}$
Strassen algorithm ${\displaystyle O(n^{2.807})}$
Coppersmith–Winograd algorithm ${\displaystyle O(n^{2.376})}$
Optimized CW-like algorithms[26][27][28] ${\displaystyle O(n^{2.373})}$
Matrix multiplication One ${\displaystyle n\times m}$ matrix &

one ${\displaystyle m\times p}$ matrix

One ${\displaystyle n\times p}$ matrix Schoolbook matrix multiplication ${\displaystyle O(nmp)}$
Matrix multiplication One ${\displaystyle n\times \lceil n\rceil }$ matrix &

one ${\displaystyle \lceil n\rceil \times n}$ matrix[clarification needed]

One ${\displaystyle n\times n}$ matrix Algorithms given in [29] ${\displaystyle O(n^{\omega (k)+\epsilon })}$, where upper bounds on ${\displaystyle \omega (k)}$ are given in [29]
Matrix inversion* One ${\displaystyle n\times n}$ matrix One ${\displaystyle n\times n}$ matrix Gauss–Jordan elimination ${\displaystyle O(n^{3})}$
Strassen algorithm ${\displaystyle O(n^{2.807})}$
Coppersmith–Winograd algorithm ${\displaystyle O(n^{2.376})}$
Optimized CW-like algorithms ${\displaystyle O(n^{2.373})}$
Singular value decomposition One ${\displaystyle m\times n}$ matrix One ${\displaystyle m\times m}$ matrix,
one ${\displaystyle m\times n}$ matrix, &
one ${\displaystyle n\times n}$ matrix
Bidiagonalization and QR algorithm ${\displaystyle O(mn^{2}+m^{2}n)}$
(${\displaystyle m\geq n}$)
One ${\displaystyle m\times n}$ matrix,
one ${\displaystyle n\times n}$ matrix, &
one ${\displaystyle n\times n}$ matrix
Bidiagonalization and QR algorithm ${\displaystyle O(mn^{2})}$
(${\displaystyle m\geq n}$)
Determinant One ${\displaystyle n\times n}$ matrix One number Laplace expansion ${\displaystyle O(n!)}$
Division-free algorithm[30] ${\displaystyle O(n^{4})}$
LU decomposition ${\displaystyle O(n^{3})}$
Bareiss algorithm ${\displaystyle O(n^{3})}$
Fast matrix multiplication[31] ${\displaystyle O(n^{2.373})}$
Back substitution Triangular matrix ${\displaystyle n}$ solutions Back substitution[32] ${\displaystyle O(n^{2})}$

In 2005, Henry Cohn, Robert Kleinberg, Balázs Szegedy, and Chris Umans showed that either of two different conjectures would imply that the exponent of matrix multiplication is 2.[33]

^* Because of the possibility of blockwise inverting a matrix, where an inversion of an ${\displaystyle n\times n}$ matrix requires inversion of two half-sized matrices and six multiplications between two half-sized matrices, and since matrix multiplication has a lower bound of ${\displaystyle \Omega }$(${\displaystyle n^{2}\log n}$) operations,[34] it can be shown that a divide and conquer algorithm that uses blockwise inversion to invert a matrix runs with the same time complexity as the matrix multiplication algorithm that is used internally.[35]

## Transforms

Algorithms for computing transforms of functions (particularly integral transforms) are widely used in all areas of mathematics, particularly analysis and signal processing.

Operation Input Output Algorithm Complexity
Discrete Fourier transform Finite data sequence of size ${\displaystyle n}$ Set of complex numbers Fast Fourier transform ${\displaystyle O(n\log n)}$

## Notes

1. ^ This form of sub-exponential time is valid for all ${\displaystyle \varepsilon >0}$. A more precise form of the complexity can be given as ${\displaystyle O\left(\exp {\sqrt[{3}]{{\frac {64}{9}}b(\log b)^{2}}}\right)}$

## References

1. ^ a b A. Schönhage, A.F.W. Grotefeld, E. Vetter: Fast Algorithms—A Multitape Turing Machine Implementation, BI Wissenschafts-Verlag, Mannheim, 1994
2. ^ D. Knuth. The Art of Computer Programming, Volume 2. Third Edition, Addison-Wesley 1997.
3. ^ Martin Fürer. Faster Integer Multiplication [https://web.archive.org/web/20130425232048/http://www.cse.psu.edu/~furer/Papers/mult.pdf Archived 2013-04-25 at the Wayback Machine. Proceedings of the 39th Annual ACM Symposium on Theory of Computing, San Diego, California, USA, June 11–13, 2007, pp. 55–67.
4. ^ David Harvey, Joris van der Hoeven Integer multiplication in time O (n log n). (2019).
5. ^ Erica Klarreich. 2019. Multiplication hits the speed limit. Commun. ACM 63, 1 (December 2019), 11–13. doi:10.1145/3371387
6. ^ Christoph Burnikel and Joachim Ziegler Fast Recursive Division Im Stadtwald , D- Saarbrucken 1998.
7. ^ http://planetmath.org/fasteuclideanalgorithm
8. ^ J. Borwein & P. Borwein. Pi and the AGM: A Study in Analytic Number Theory and Computational Complexity. John Wiley 1987.
9. ^ David and Gregory Chudnovsky. Approximations and complex multiplication according to Ramanujan. Ramanujan revisited, Academic Press, 1988, pp 375–472.
10. ^ Richard P. Brent, Multiple-precision zero-finding methods and the complexity of elementary function evaluation, in: Analytic Computational Complexity (J. F. Traub, ed.), Academic Press, New York, 1975, 151–176.
11. ^ a b Richard P. Brent (2020), The Borwein Brothers, Pi and the AGM, Springer Proceedings in Mathematics & Statistics, 313, arXiv:1802.07558, doi:10.1007/978-3-030-36568-4, ISBN 978-3-030-36567-7
12. ^ J. Sorenson. (1994). "Two Fast GCD Algorithms". Journal of Algorithms. 16 (1): 110–144. doi:10.1006/jagm.1994.1006.
13. ^ R. Crandall & C. Pomerance. Prime Numbers – A Computational Perspective. Second Edition, Springer 2005.
14. ^ Möller N (2008). "On Schönhage's algorithm and subquadratic integer gcd computation" (PDF). Mathematics of Computation. 77 (261): 589–607. Bibcode:2008MaCom..77..589M. doi:10.1090/S0025-5718-07-02017-0.
15. ^
16. ^ Richard P. Brent; Paul Zimmermann (2010). "An ${\displaystyle O(M(n)\log n)}$ algorithm for the Jacobi symbol". arXiv:1004.2091 [cs.DS].
17. ^ Borwein, P. (1985). "On the complexity of calculating factorials". Journal of Algorithms. 6 (3): 376–380. doi:10.1016/0196-6774(85)90006-9.
18. ^ H. W. Lenstra Jr. and Carl Pomerance, "Primality testing with Gaussian periods", preliminary version July 20, 2005.
19. ^ H. W. Lenstra jr. and Carl Pomerance, "Primality testing with Gaussian periods Archived 2012-02-25 at the Wayback Machine", version of April 12, 2011.
20. ^ Tao, Terence (2010). "1.11 The AKS primality test". An epsilon of room, II: Pages from year three of a mathematical blog. Graduate Studies in Mathematics. 117. Providence, RI: American Mathematical Society. pp. 82–86. doi:10.1090/gsm/117. ISBN 978-0-8218-5280-4. MR 2780010.
21. ^ Lenstra, Jr., H.W.; Pomerance, Carl (December 11, 2016). "Primality testing with Gaussian periods" (PDF). Cite journal requires |journal= (help)
22. ^ Morain, F. (2007). "Implementing the asymptotically fast version of the elliptic curve primality proving algorithm". Mathematics of Computation. 76 (257): 493–505. arXiv:math/0502097. Bibcode:2007MaCom..76..493M. doi:10.1090/S0025-5718-06-01890-4. MR 2261033.
23. ^ Carl Pomerance; John L. Selfridge; Samuel S. Wagstaff, Jr. (July 1980). "The pseudoprimes to 25·109" (PDF). Mathematics of Computation. 35 (151): 1003–1026. doi:10.1090/S0025-5718-1980-0572872-7. JSTOR 2006210.
24. ^ Robert Baillie; Samuel S. Wagstaff, Jr. (October 1980). "Lucas Pseudoprimes" (PDF). Mathematics of Computation. 35 (152): 1391–1417. doi:10.1090/S0025-5718-1980-0583518-6. JSTOR 2006406. MR 0583518.
25. ^ a b Monier, Louis (1980). "Evaluation and comparison of two efficient probabilistic primality testing algorithms". Theoretical Computer Science. 12 (1): 97–108. doi:10.1016/0304-3975(80)90007-9. MR 0582244.
26. ^ Davie, A.M.; Stothers, A.J. (2013), "Improved bound for complexity of matrix multiplication", Proceedings of the Royal Society of Edinburgh, 143A (2): 351–370, doi:10.1017/S0308210511001648
27. ^
28. ^ Le Gall, François (2014), "Powers of tensors and fast matrix multiplication", Proceedings of the 39th International Symposium on Symbolic and Algebraic Computation (ISSAC 2014), arXiv:1401.7714, Bibcode:2014arXiv1401.7714L
29. ^ a b Le Gall, François; Urrutia, Floren (2018). "Improved Rectangular Matrix Multiplication using Powers of the Coppersmith-Winograd Tensor". In Czumaj, Artur (ed.). Proceedings of the Twenty-Ninth Annual ACM-SIAM Symposium on Discrete Algorithms. Society for Industrial and Applied Mathematics (SIAM). doi:10.1137/1.9781611975031.67. ISBN 978-1-61197-503-1.
30. ^ http://page.mi.fu-berlin.de/rote/Papers/pdf/Division-free+algorithms.pdf
31. ^ Aho, Alfred V.; Hopcroft, John E.; Ullman, Jeffrey D. (1974), The Design and Analysis of Computer Algorithms, Addison-Wesley, Theorem 6.6, p. 241
32. ^ J. B. Fraleigh and R. A. Beauregard, "Linear Algebra," Addison-Wesley Publishing Company, 1987, p 95.
33. ^ Henry Cohn, Robert Kleinberg, Balazs Szegedy, and Chris Umans. Group-theoretic Algorithms for Matrix Multiplication. arXiv:math.GR/0511460. Proceedings of the 46th Annual Symposium on Foundations of Computer Science, 23–25 October 2005, Pittsburgh, PA, IEEE Computer Society, pp. 379–388.
34. ^ Ran Raz. On the complexity of matrix product. In Proceedings of the thirty-fourth annual ACM symposium on Theory of computing. ACM Press, 2002. doi:10.1145/509907.509932.
35. ^ T.H. Cormen, C.E. Leiserson, R.L. Rivest, C. Stein, Introduction to Algorithms, 3rd ed., MIT Press, Cambridge, MA, 2009, § 28.2.