1

It seems there is no closed form for the greatest common divisor of any two given integers.

Why is there no such formula?

Does the only way to compute the gcd is essentially to recursively apply the Euclidean algorithm?

Dr Potato
  • 1,013
  • 3
    Well, there certainly is a closed form if you have the prime factorizations. And, of course, that gives an alternative method for finding the gcd...though, for large numbers, factorization can be quite difficult so the Euclidean algorithm is to be preferred. – lulu Jul 04 '19 at 20:52
  • Note that $n>1$ is prime iff $\gcd(n,(n-1)!)=1$ – Hagen von Eitzen Jul 04 '19 at 22:33
  • For computing, there are also "binary" algorithms, and optimizations for very large numbers, like this one. – metamorphy Jul 20 '19 at 07:44
  • 1
    What is a "closed formula" and why should there be one? If $mn$ is a "closed formula" for the product, and if $\log n$ is a "closed formula" for the logarithm, then why isn't $\gcd(m,n)$ a "closed formula" for the greatest common divisor? – user14111 Jan 30 '25 at 23:29

3 Answers3

2

Does this count? Using this result gives

$$\text{gcd}^2(m,n) = \frac{24mn}{\pi^3}\Big[-\frac{\pi \ln^2(2)}{2} + \int_{0}^{\frac{\pi}{2}} \ln |\sin(mx)| \cdot \ln |\sin(nx)| \text{ }\mathrm dx \Big]$$

There are many more integrals whose closed forms contain the GCD. (See this post) It is definitely more effective to calculate the $\gcd$ using the euclidean algorithm or the prime factorization. :)

  • +1 Amazing! But I didn't know "closed formulas" could include integral signs. And I think the Euclidean algorithm is easier than prime factorization. – user14111 Jan 30 '25 at 23:35
  • Using prime factorization for the gcd is a terrible idea. Euclidean algorithm is waaay more efficient. – jjagmath Jan 30 '25 at 23:56
  • 1
    Wait WHAT? how in hell they obtained this? Let me check it out but yes, indeed seems more analytic and in "closed form" than the Euclidean algorithm, which is more number-theoritic-ish. It is very unlikely to obtain an expression in terms of basic functions, and we still hope to find an expression in terms of special functions, avoiding integrals, of course, until somebody someday proved the latter was impossible, just in case. But we hope is not impossible, right? – Dr Potato Jan 31 '25 at 03:43
  • @DrPotato the integral is a super crazy result :D Here is one without the integral. For odd $a \geq 1$,

    ${\displaystyle \gcd(a,b)=\log _{2}\prod _{k=0}^{a-1}(1+e^{-2i\pi kb/a})}$

    – Maxime Jaccon Jan 31 '25 at 04:05
  • The finite-product is useless since it has complexity $O(a)$ and thus the Euclidean algorithm is faster. – Dr Potato Feb 01 '25 at 01:12
2

Does this count?

$$(m,n)=n-2\sum_{k=1}^n\left\{\frac{k\ m}{n}\right\}$$

jjagmath
  • 22,582
  • What is the meaning of $\big{\frac{km}{n}\big}$? – Qdzyx Jan 31 '25 at 01:21
  • @Qdzyx ${x}$ is a pretty common notation for the fractional part of $x$: $${x} = x - \lfloor x \rfloor$$ – jjagmath Jan 31 '25 at 02:19
  • No, since its complexity is $O(\min(n,m))$. Even the Euclidean algorithm is faster. Give me a good reason not to down-vote this answer :) – Dr Potato Feb 01 '25 at 01:11
  • 2
    @DrPotato The Euclidean algorithm is an algorithm, not a formula. This is (in some sense) a formula. Feel free to downvote or upvote if you want, I don't need anyone's approval :) – jjagmath Feb 01 '25 at 01:17
0

There certainly is a closed formula, if you have the prime factorizations.

Let $$m=\prod p_i^{m_i}\quad \& \quad n=\prod p_i^{n_i}$$

Where the product is taken over the primes $\{p_i\}$ and it is understood that only finitely many of the $\{m_i\}$ and $\{n_i\}$ are non-zero.

Then $$\gcd(m,n)=\prod p_i^{\min(m_i,n_i)}$$

This also gives an alternative to the Euclidean Algorithm. It's not all that helpful, though, because it is, in general, extremely difficult to produce the prime factorization. By contrast, the Euclidean algorithm is easy to use, even for large numbers.

lulu
  • 76,951
  • 2
    That would be a closed formula if you give a general non-recursive expression of the factorization of any natural number. – Dr Potato Jul 04 '19 at 21:16
  • Sure. As I say, though, at least it gives an alternate method for finding the gcd, albeit a method that's generally far from optimal. – lulu Jul 04 '19 at 22:13
  • 1
    This is not an answer for the question. – Dr Potato Jul 10 '19 at 04:47