0

Given two numbers $a$ and $b$, assume $a \leq b$, we perform the following operation.

  1. If $a = 0$, break else goto step 2
  2. Assign to $a$ the remainder of $b$ when divided by a, i.e. $$a :=(b \mod a)$$

I want to find the upper bound for the number of iteration given any numbers with some finite digits. Lets assume both have maximum 10 digits. Even better if we find the bound for any given input with any number of digits(generalized upper bound).

We know that the remainder always decreases by at least 1 so this is a trivial upper bound O(a). Lets find something more smart.

Note: This can be also defined as recurrence relation shown below: $$ f(n)=\begin{cases} 0, &\text{ if }n = 0\\ f(a\mod n)+1,&\text{ if }n > 0 \end{cases} $$

We need to find the maximum value possible for the function $f$ for any given value $a$ over all possible $n$.

ukh
  • 378
  • Note: I have created a random test myself through coding and found that for any two random numbers a and b, the remainder is always reduced by at least half on average. I ran 10 million random tests and it gave me an average where the remainder was halving every step on average. Note the on average, it may not always halve on every iteration, but the average definitely does halve. So some kind of amortized analysis is necessary. – ukh Nov 28 '22 at 13:29
  • 1
    The Euclidean Algorithm is well documented. See, for example, this question. – lulu Nov 28 '22 at 13:29
  • 2
    Should say, writing, e.g., $a\equiv b \pmod a$ is confusing. I expect you mean $a_{n+1}\equiv b\pmod {a_n}$ and that you are just reusing the same variable name. – lulu Nov 28 '22 at 13:30
  • @lulu This is not Euclidean algorithm, please check more carefully – ukh Nov 28 '22 at 13:33
  • A simple case is of 7, 19. It proceeds like this 7, 5, 4, 3, 1, 0. So, averaging the remainder drop ((2/7+1/5+1/4+2/3+1)*100/5 = 48.08%, so on average remainder dropped by 48%)

    @lulu

    – ukh Nov 28 '22 at 13:39
  • @lulu Added := to clarify assignment operation. – ukh Nov 28 '22 at 13:55
  • Nope, I already know the limits of Euclidean algorithm, this is different in subtle way. – ukh Nov 28 '22 at 14:55
  • Euclidean algorithm is easy to prove because we see the values always decrease, but in this case we fix one value, but are trying to find how quickly the another value drops until 0. We're only changing one value every step the other one stays constant throughout. – ukh Nov 28 '22 at 14:57
  • @EthanBolker I added a recurrence relation as well, we want to find the maximum value for the recurrence relation for a given a and n. I am trying my best to find the upper bound. – ukh Nov 28 '22 at 15:00

1 Answers1

-1

The trivial upper bound $f(a)\le a$ can in fact be attained for every $a\ge2$: consider $b=a!-1$.

Greg Martin
  • 78,820
  • Can you help prove that the worst case scenario takes far lesser? Like twice the sum of digits or something. I want to decrease the upper bound. i.e. f(a) is at most X – ukh Nov 28 '22 at 16:48
  • Okay, I understood your answer. You submitted a nice case where every remainder from 0 to a occurs. This is nice but the a! part gets quite juge so lets hope there's a similar worse case but where b is far smaller. I'll give a nice upvote. – ukh Nov 28 '22 at 16:55
  • Heyy Greg, do you mind accepting my edit? I am not able to upvote unless the answer is edited. – ukh Nov 28 '22 at 16:57
  • Heyy Greg, I wanted to accept the answer but I've found even smaller worse cases for e.g a=9518565644931 b=13352286610864. But you surely deserve an upvote for the smart answer. Another one, a=9436724018003 b=12631585534019, it takes 64 steps for this one to convert to 0. – ukh Nov 28 '22 at 17:03
  • I have ran some random tests and found out the maximum iterations is at most twice the number of digits in a and b. Maybe this is just a sampling bias. – ukh Nov 28 '22 at 17:07
  • Greg, this is acceptable to me. Although I wanted to find some similar cases where b is not so huge in comparison to a. – ukh Nov 28 '22 at 17:11
  • Sorry if I commented much, but I am trying to craft a test case where even visiting sqrt(a) remainders is enough because that is also very worse as far as computations is concerned. – ukh Nov 28 '22 at 17:32
  • I definitely think Chinese Remainder Theorem can be used to build a very bad edge case. – ukh Nov 28 '22 at 17:35