C++ Large Number Arithmetic

这一生的挚爱 提交于 2019-11-30 16:48:20

Lets start with simple multiplication(Long multiplication):

112 * 301

          1     1     2
          3     0     1
          ______________
          1     1     2
     0    0     0
 3   3    6
 _______________________
 3   3    7     1     2

So, this needs N by N matrix as rows to be added with shifting-n-times.

Where are you doing this addition and where is shifting?

For your question, it would need 500 x 500 multiplications and 500 x 500 additions. O(N*N)

Pro: each digit-multiplication can be done in a single byte so you can change the structure of digits that your compiler can vectorize the code and multiply 16 to 32 digits at once(unrolls quite good).

Con: too many computing(nearly 25-40 iteration per 500 digits-num)

Note: GPU-powered calculus could give it roughly 40x more speed. Such as OpenCL or Cuda.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!