Time complexity of modulo scenario

233 Views Asked by At

Something theoretical here.

Say if I have two natural numbers $x$ and $y$. Both these numbers are upper-bounded by a third number $z$. ($O$($z$))

Now let's say I have a recursive modulo function that in order to calculate $x$ mod $y$ simply does $x_{i+1} = x_{i} - y$ on every iteration (if $x_{i} \geqslant y$).

Since our upper bound on both numbers is $O(z)$ could we say that the total number of iterations we have to make is $\frac{O(z)}{O(z)}$ which would be constant time wrt. our number $z$, or is this breaking so many rules of rational thought it belongs in Alice and wonderland?

1

There are 1 best solutions below

0
On BEST ANSWER

An example worst case would be $x=10$ and $y=1$.

$\quad10\%1$

$=9\%1$

$=8\%1$

$=7\%1$

$=6\%1$

$=5\%1$

$=4\%1$

$=3\%1$

$=2\%1$

$=1\%1$

$=0\%1$

$=0$

In this case, the time complexity is actually $O(z)$.

The best case scenario is trivially $O(1)$.

The average case scenario is left to the reader as an exercise.