Multiplication of very long integers

后端 未结 7 1747
日久生厌
日久生厌 2021-02-09 05:57

Is there an algorithm for accurately multiplying two arbitrarily long integers together? The language I am working with is limited to 64-bit unsigned integer length (maximum int

7条回答
  •  借酒劲吻你
    2021-02-09 06:32

    The simplest way would be to use the schoolbook mechanism, splitting your arbitrarily sized numbers into chunks of 32-bit each.

    Given A B C D * E F G H (each chunk 32-bit, for a total 128 bit)
    You need an output array 9 dwords wide. Set Out[0..8] to 0

    You'd start by doing: H * D + out[8] => 64 bit result.
    Store the low 32-bits in out[8] and take the high 32-bits as carry
    Next: (H * C) + out[7] + carry
    Again, store low 32-bit in out[7], use the high 32-bits as carry
    after doing H*A + out[4] + carry, you need to continue looping until you have no carry.

    Then repeat with G, F, E.
    For G, you'd start at out[7] instead of out[8], and so forth.

    Finally, walk through and convert the large integer into digits (which will require a "divide large number by a single word" routine)

提交回复
热议问题