Question

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,

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top