How to divide large numbers and what algorithm to use

╄→гoц情女王★ 提交于 2019-12-01 09:41:52
Spektre

These are my favorite algorithms I use:

  1. Binary division
    Look here: http://courses.cs.vt.edu/~cs1104/BuildingBlocks/divide.030.html
    This is what you should start with. It's not that slow and it is simple. Do not forget to properly test your +, -, <<, >> operations before you start. They should work flawlessly on any given input

  2. Division by half the bits arithmetics
    Look here: https://stackoverflow.com/a/19381045/2521214
    With little tweaking you can adapt it to arrays. Uses +, -, *, /, %. If you code it properly should be much faster then Binary division.

  3. approximation of division
    Look here: https://stackoverflow.com/a/18398246/2521214
    Or for some speed up of x^2, x*y here: Fast bignum square computation
    This is more suited for floating/fixed point division. It's a little bit harder to understand, but the speed and accuracy is worth the while. Also, there are many other approximation algorithms out there, So google!

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