# Efficiently producing certain kinds of examples of the application of Euclid's algorithm

Is there some efficient way to churn out pairs of integers $n,m$ such that

• $\gcd(n,m)=1$;
• $n,m$ both have fairly large numbers of fairly small prime factors; and
• Euclid's algorithm applied to $n,m$ has a moderately large number of steps; e.g. a number is replaced by its remainder at least four or five or six times?
• I am wondering about the possible pedagogical utility of looking at the way in which the two sets of all divisors change at each step in the algorithm. Having found $n$ and $m$ as above, one might multiple them both by something like $741=3\times13\times19$ and have a couple of eight-or-ten-or-so-digit numbers with gcd $741$, and one can then look at the sets of divisors of $n$ and $m$, and then the sets of divisors of $n$ and $m\bmod n$, and so on. Then ones they have in common will remain the same at every step. Then an exercise could be to figure out what that happens. ${}\qquad{}$ – Michael Hardy Feb 21 '15 at 23:19
• $\gcd(27258,11781)=231$. The first of these numbers has 32 divisors and the second has 24. Eight of them are shared in common. $\gcd(27258-11781,11781)$ is of course also $231$. The first of these numbers has only 16 divisors. Again they share those same eight common divisors. Take it another step: $(27258-2(11781),\ 11781)=(3696,11781)$. This time the list of divisors of the first number grows all the way to 40, and of course the common divisors are still a list of only eight. The lists of divisors change a lot at each step, but those eight always remain. – Michael Hardy Feb 24 '15 at 1:26
• $\ldots$ and the example of $\gcd(27258,11781)=231$ was chosen because $27258/11781 = 118/51$ and when Euclid's algorithm is applied to that pair we get $(118,51) \mapsto (16,51) \mapsto (16,3) \mapsto (1,3) \mapsto (1,0)$, so it goes on for a fairly large number of steps. ${}\qquad{}$ – Michael Hardy Feb 24 '15 at 1:30
• A number with a large number of small factors is called a "smooth" number. – DanielV Feb 24 '15 at 2:02
• Are you willing to do a bit of Monte Carlo? You could easily run Euclid's algorithm 'in reverse' (i.e., compute a few finite continued fractions) with coefficients randomly chosen in the range [1..8], say, until you find some numbers that meet your criteria... – Steven Stadnicki Feb 24 '15 at 2:04

1. Choose $n = {p_1}^{e_1}\cdots{p_k}^{e_k}$ for some small primes $p_i$ and arbitrary exponents.

2. Let $a = {p_{k+1}}^{e_k+1}\cdots{p_l}^{e_l}$ using different small primes. Choose arbitrary exponents while ensuring that $a \ll n$.

3. Let $p^\star$ be the smallest prime greater than $\dfrac{\phi n}{a}$ where $\phi = \dfrac{1+\sqrt5}{2}$.

4. Let $m = ap^\star$.

Then $m$ and $n$ both have many small prime factors ($n$ has exactly one large prime factor, $p^\star$), and $m \approx a \dfrac{\phi n}{a} = \phi n$. Because $m$ and $n$ have a ratio of approximately $\phi$, the Euclidean algorithm will take many steps to run.

Example:

1. Choose $n = 2^4 \cdot 3^3 \cdot 5^4 \cdot 19^2 \cdot 61 = 5945670000$.
2. Choose $a = 7 \cdot 11 \cdot 13 \cdot 17 \cdot 23 = 391391$.
3. $p^\star = 24593$, the smallest prime greater than $\dfrac{\phi n}{a} = 24579.75\ldots\,$.
4. $m = ap^\star = 9625478863$.

Running Euclid on $(n,m)$ takes 24 steps. Here's a list of the intermediate values along with their ratios. Note that the ratio stays near $\phi$ for the first five or six steps:

9625478863 5945670000  ratio: 1.618
5945670000 3679808863  ratio: 1.615
3679808863 2265861137  ratio: 1.624
2265861137 1413947726  ratio: 1.602
1413947726 851913411  ratio: 1.659
851913411 562034315  ratio: 1.515
562034315 289879096  ratio: 1.938
289879096 272155219  ratio: 1.065
272155219 17723877  ratio: 15.355
17723877 6297064  ratio: 2.814
6297064 5129749  ratio: 1.227
5129749 1167315  ratio: 4.394
1167315 460489  ratio: 2.534
460489 246337  ratio: 1.869
246337 214152  ratio: 1.15
214152 32185  ratio: 6.653
32185 21042  ratio: 1.529
21042 11143  ratio: 1.888
11143 9899  ratio: 1.125
9899 1244  ratio: 7.957
1244 1191  ratio: 1.044
1191 53  ratio: 22.471
53 25  ratio: 2.12
25 3  ratio: 8.333
3 1  ratio: 3.0


$$3\cdot7\cdot13\cdot23\cdot31\cdot41=6737367$$

$$2\cdot5\cdot11\cdot17\cdot29\cdot37=2376770$$

$$6737367, 2376770, 1983827, 392943, 19112, 10703, 8409, 2294, 1527, 767, 760, 7, 4, 3, 1$$

### Example #1: Neighboring Fibonacci Numbers

By reverse induction and $F_n + F_{n-1} = F_{n+1}$

$(F_n, F_{n+1}) = (F_n, F_{n-1} + F_n) = (F_n, F_{n-1}) = \dots = (F_1, F_0) = 1$

How to prove gcd of consecutive Fibonacci numbers is 1?

### Example #2: Advanced Discussion on arXiv

just this week A short proof that the number of division steps in the Euclidean algorithm is normally distributed was posted. The average number of Euclidean algorithm steps is $\frac{12}{\pi^2} \log 2 \log n$. If $n = 10^6$ that number is about $2 \times 6 = 12$.

Here with some Python I compute random gcd's of numbers up to $10^6$. Notice it takes no more than 30 steps (on average about $12$ in agreement with above). In these examples GCD will be $1$ about two-thirds of the time. My code is on GitHub.

### Example #3: Connection to Yves Daoust solution

Divide the set $P$ of primes less than 100 and split into two sets $P_1, P_2$. If you randomly multiply only within $P_1$ or $P_2$ you are guaranteed to be relatively prime and smooth. If these two products are less than $10^N$ the expected number of steps is around $2*N$.

• If I'm not mistaken, neighboring Fibonacci nunmbers are the worst case, in the sense that they require more divisions than any other pairs of numbers of comparable size. But I'd rather avoid examples that might make it appear to students that the quotient will always be $1$. ${}\qquad{}$ – Michael Hardy Feb 28 '15 at 14:55
• @MichaelHardy I am working on it :-) Notice gcd = 1 with probability $\frac{6}{\pi^2} \approx \frac{2}{3}$ for two random numbers! Unfortunately, these may have large factors, so they don't follow your smoothness condition. – cactus314 Feb 28 '15 at 14:57

I would try working the Euclidean algorithm backwards: $r_{n-2}=k\cdot r_{n-1}+r_n$, looking for $k$'s that gave me the small factors I wanted at each step. Since you want $\gcd(m,n)=1$, the last two remainders would be $0$ (last), and $1$ (second to last).

For instance, the (reverse) sequence of remainders could be something like:

$0$

$1$

$6\cdot 1+0=6$

$4\cdot 6+1=25$

$2\cdot 25+6=56$

$1\cdot 56+25=81$

$4\cdot 81+56=380$

$3\cdot 380+81=1221$

Giving you a problem where you start with taking the gcd of $380=2^2\cdot 5\cdot 19$ and $1221=3\cdot11\cdot 37$

(Of course you can play with the multipliers going from one remainder to the next to (try to) get initial numbers that have suitable factorizations.)

Then, as you discuss in your comments, you would multiply each by some common amount to get the problem you wanted.

• How do you assure that you get $2,5,19$ and $3,11,37$ as the two sets of prime factors (or any other two disjoint sets of small primes) by choosing quotients and remainders? ${}\qquad{}$ – Michael Hardy Mar 3 '15 at 15:05
• @MichaelHardy: I don't have a predetermined set of small primes in mind. But as you can choose the multiplier at each step, you can get a fair amount of control over what you get. For instance, in the example I give in my answer, when the two previous remainders were $81$ and $56$, the next remainder will be of the form $56+81k$, giving possible values of $137, 218, 299, 380$. $380$ looked nice to me ($2^2\cdot 5\cdot 19$), so I grabbed it. – paw88789 Mar 3 '15 at 15:10