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

前端 未结 14 2060
终归单人心
终归单人心 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

    If you have a binary number A, it can (always, proof left to the eager reader) be expressed as (2^n + B), this can be squared as 2^2n + 2^(n+1)B + B^2. We can then repeat the expansion, until such a point that B equals zero. I haven't looked too hard at it, but intuitively, it feels as if you should be able to make a squaring function take fewer algorithmical steps than a general-purpose multiplication.

    0 讨论(0)
  • 2021-01-30 07:50

    I have it!

    2 * 2
    

    is more expensive than

    2 << 1
    

    (The caveat being it only works for one case.)

    0 讨论(0)
提交回复
热议问题