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

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

    I would want to solve the problem by N bit multiplication for a number

    A the bits be A(n-1)A(n-2)........A(1)A(0).

    B the bits be B(n-1)B(n-2)........B(1)B(0).

    for the square of number A the unique multiplication bits generated will be for A(0)->A(0)....A(n-1) A(1)->A(1)....A(n-1) and so on so the total operations will be

    OP = n + n-1 + n-2 ....... + 1 Therefore OP = n^2+n/2; so the Asymptotic notation will be O(n^2)

    and for multiplication of A and B n^2 unique multiplications will be generated so the Asymptotic notation will be O(n^2)

提交回复
热议问题