0

I was solving $\gcd$ algorithm problem.

The problem was to get a $\gcd$ from two special numbers.

Followings are conditions.

1) User can input number $a$ and $b$

(maximum ciphers of a,b is 19. so type is must be unsinged long long

2) Two special numbers made by repetition of "1" as much as a,b.

ex) $a=4$, $b=3$ -> special number : 1111, 111

3) Finally, have to get a $\gcd$ of those two special numbers.

But I found specific relation between them.

Let special numbers by $a$, $b$ call $s_a$, $s_b$. then we can preset $s_a$, $s_b$ like below: $$s_a=10^{a-1}+10^{a-2}+\dots+10^0\quad \text{and} \quad s_b=10^{b-1}+10^{b-2}+\dots+10^0.$$

From this, my foundation is below. $$\gcd(s_a,s_b) = 10^{\gcd(a,b)-1}+10^{\gcd(a,b)-2}+\dots+10^0$$ but I can't prove why it works. Can you prove about this?

Sorry for my poor English. I'm korean..

Robert Z
  • 145,942
BAE HA RAM
  • 103
  • 5

1 Answers1

1

Notice that for any positive integer $n$, $$(10-1)(10^{n-1}+10^{n-2}+\cdots+1)=10^n-1$$ and therefore the problem is equivalent to $$\gcd(10^{a}-1, 10^{b}-1)=10^{\gcd(a,b)}-1.$$ Moreover, if $a\geq b$ and $a=qb+r$ with $q\geq 1$, $0\leq r<b$, then $$\begin{align} \gcd(10^{a}-1, 10^{b}-1)&=\gcd(10^{qb+r}-10^r+10^r-1, 10^{b}-1)\\&=\gcd(10^r(10^{qb}-1)+10^r-1, 10^{b}-1)\\&=\gcd(10^r-1, 10^{b}-1) \end{align}$$ where in the last step we used the fact that $10^{b}-1$ divides $10^{qb}-1$. Now recall that the Euclid's algorithm computes the greatest common divisor.

Can you take it from here?

A more general statement holds: for $n\geq 2$ and $a,b\geq 1$, $$\gcd(n^{a}-1, n^{b}-1)=n^{\gcd(a,b)}-1.$$ see Prove that $\gcd(a^n - 1, a^m - 1) = a^{\gcd(n, m)} - 1$ .

Robert Z
  • 145,942