Determining the final remainder.

54 Views Asked by At

what is the best way to determine the remainder left after repeatedly dividing the quotients of that number by a fixed number? for example:

n = 15;
i = 3;
q = 15/3 = 5;
q = 5/3  = 1 (quotients only)
q = 1/3  = 0;
therefore, remainer = 1%3 = 1;

here

 n = the number
 i = fixed number by which I want to divide
 q = quotient that I am getting everytime
 r = remainder left when finally quotient is zero

Since repeated division is one of the way to achieve this is there any other way to do this. (better way)

1

There are 1 best solutions below

0
On

If you have sufficiently high precision real arithmetic, you can calculate it as follows.

First get $\log_in=\frac{\ln n}{\ln i}$. Then get its fractional part, $f=\log_in-\lfloor\log_in\rfloor$. Then $\left\lfloor i^f\right\rfloor$ is the most significant digit of $n$ in its base $i$ representation. (In base $i$ ‘scientific notation’ $n=\left\lfloor i^f\right\rfloor\times i^{\lfloor\log_in\rfloor}$.)