Divide an integer by two bytes

I am working on an embedded project where I have to write the timeout value in two byte registers of some microchips.

A timeout is defined as:

   timeout = REG_a * (REG_b +1)

I want to program these registers using an integer in the range from 256 to 60,000. I am looking for an algorithm that, given the timeout value, computes REG_a and REG_b.

If an exact solution is not possible, I would like to get the next possible larger timeout value.

What i have done so far:

My current solution calculates:

   temp = integer_square_root (timeout) +1;
   REG_a = temp;
   REG_b = temp-1;

This leads to values ​​that work well in practice. However, I would like you to come up with a better solution.

Oh, and I'm limited by memory, so large tables are out of the question. Running time is also important, so I can’t just reinstall the solution.

+5
2

, , .. ?, .

n = timeout 
initial_n = n
num_factors = 1;
for (i = 2; i * i <= initial_n; ++i) // for each number i up until the square root of the given number
{
    power = 0; // suppose the power i appears at is 0
    while (n % i == 0) // while we can divide n by i
    {
        n = n / i // divide it, thus ensuring we'll only check prime factors
        ++power // increase the power i appears at
    }
    num_factors = num_factors * (power + 1) // apply the formula
}

if (n > 1) // will happen for example for 14 = 2 * 7
{
    num_factors = num_factors * 2 // n is prime, and its power can only be 1, so multiply the number of factors by 2
}
REG_A = num_factor

REG_A, , -.

for (i=2; i*num_factors != timeout;i++);
REG_B = i-1
+2

, !

, , Reg_a, Reg_b : Reg_b = ((timeout + Reg_a-1) / Reg_a) -1.

, , ? , Reg_a, ? - .

, , .

, , , ,

>

:

-, Reg_a? (timeout + 255) / 256;

Reg_b, .

, , .

+1

All Articles