Integer cube root

后端 未结 6 1918
一生所求
一生所求 2021-02-05 07:18

I\'m looking for fast code for 64-bit (unsigned) cube roots. (I\'m using C and compiling with gcc, but I imagine most of the work required will be language- and compiler-agnost

6条回答
  •  半阙折子戏
    2021-02-05 07:57

    I would research how to do it by hand, and then translate that into a computer algorithm, working in base 2 rather than base 10.

    We end up with an algorithm something like (pseudocode):

    Find the largest n such that (1 << 3n) < input.
    result = 1 << n.
    For i in (n-1)..0:
        if ((result | 1 << i)**3) < input:
            result |= 1 << i.
    

    We can optimize the calculation of (result | 1 << i)**3 by observing that the bitwise-or is equivalent to addition, refactoring to result**3 + 3 * i * result ** 2 + 3 * i ** 2 * result + i ** 3, caching the values of result**3 and result**2 between iterations, and using shifts instead of multiplication.

提交回复
热议问题