Why is squaring a number faster than multiplying two random numbers?

前端 未结 14 2093
终归单人心
终归单人心 2021-01-30 07:18

Multiplying two binary numbers takes n^2 time, yet squaring a number can be done more efficiently somehow. (with n being the number of bits) How could that be?

Or is i

14条回答
  •  花落未央
    2021-01-30 07:47

    Suppose you want to expand out the multiplication (a+b)×(c+d). It splits up into four individual multiplications: a×c + a×d + b×c + b×d.

    But if you want to expand out (a+b)², then it only needs three multiplications (and a doubling): a² + 2ab + b².

    (Note also that two of the multiplications are themselves squares.)

    Hopefully this just begins to give an insight into some of the speedups that are possible when performing a square over a regular multiplication.

提交回复
热议问题