finding power of a number

…衆ロ難τιáo~ 提交于 2019-12-25 04:24:56

问题


I have a very big number which is a product of several small primes. I know the number and also I know the prime factors but I don't know their powers. for example:

(2^a)x(3^b)x(5^c)x(7^d)x(11^e)x .. = 2310

Now I want to recover the exponents in a very fast and efficient manner. I want to implement it in an FPGA.

Regards,


回答1:


The issue is that you are doing a linear search for the right power when you should be doing a binary search. Below is an example showing how to the case where the power of p is 10 (p^10). This method finds the power in O(log N) divisions rather than O(N).

First find the upper limit by increasing the power quickly until it's too high, which happens at step 5. Then it uses a binary search to find the actual power.

  1. Check divisibility by p. Works.
  2. Check divisibility by p^2. Works.
  3. Check divisibility by p^4. Works.
  4. Check divisibility by p^8. Works.
  5. Check divisibility by p^16. Doesn't work. Undo/ignore this one.
  6. Check divisibility by p^((8+16)/2)=p^12. Doesn't work. Undo/ignore this one.
  7. Check divisibility by p^((8+12)/2)=p^10. Works, but might be too low.
  8. Check divisibility by p^((10+12)/2)=p^11. Doesn't work. Undo/ignore this one.
  9. Since ((10+11)/2)=10.5 is not an integer, the power most be the low end, which is 10.

Note, there is a method where you actually divide by p, and at step 4, you've actually divided the number by p^(1+2+4+8)=p^15, but it's a bit more difficult to explain the binary search part. However, the size of the number being divided gets smaller, so division operations are faster.



来源:https://stackoverflow.com/questions/22124483/finding-power-of-a-number

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