Calculating the GCD goes from being a no-brainer to being a painstaking effort. Use our GCD calculator to make all your problems part of the first of these two possibilities. Keep reading our short article to learn:
- What is the greatest common divisor of a set of numbers;
- How to calculate the GCD; and
- Five algorithms to calculate the GCD.
What is the GCD?
The GCD (short for greater common divisor) is a useful mathematical concept, the largest number that divides exactly all the numbers in a set. The GCD is defined for every number: reals, negatives, etc. However, we meet it most often when dealing with natural numbers.
The GCD has many applications, and in most of them we don't even notice it, but we apply it as we do with many, easier, mathematical operations, for example when we simplify fractions. However, as you will learn, this simple operation has many properties, and it can entertain most mathematicians with various algorithms, more or less complex. Let's discover them!
How to calculate the GCD: intuitive algorithms
To calculate the GCD we can use different intuitive methods:
- Prime factorization;
- Euclidean algorithm; and
- Modified Euclidean algorithm.
Let's see them one by one.
Calculate the GCVD using prime factorization
To calculate the GCD with prime factorization, find the prime factors of all numbers of a set, and select the largest common prime factor, choosing as exponent the highest possible that appears in all the factorizations. That's the greater common divisor. This method intuitively tells you that the GCD of a set of prime numbers is .
Calculate the GCD with the Euclidean algorithm
The original Euclidean algorithm for the GCD requires just a bunch of subtractions. The process is simple. Consider a couple of integers for which you want to find the GCD.
- Start by subtracting the smaller number from the larger one.
- Substitute the largest number with the result.
- Repeat step 1, subtracting the smallest number of the pair from the largest number. You may have to subtract the same number again, depending on the original choice of numbers.
- Substitute the largest number with the result.
- Rinse and repeat.
- The algorithm ends when the two numbers are equal: the last result is also the GCD.
Modified Euclidean algorithm
The Euclidean algorithm using subtraction can become pretty lengthy (in particular if the two number differs by much at the beginning). Luckily, we can apply similar reasoning using a different operation: the modulo. In this case, we perform the following steps:
- We calculate the remainder of the division of the larger number by the smaller one (using the modulo operation).
- We substitute the larger number with the remainder.
- We repeat the modulo operation on the new pair of numbers.
- The algorithm proceeds until the result of the modulo operation is
Alternative algorithms to calculate the GCD
We can find the GCD in other ways, one more creative and one involving more reasoning.
Upside-down division GCD algorithm
This method is an alternative way to calculate the GCD based solely on divisions. It works wonders in most cases! The procedure is simple:
- Find the smallest prime number that divides exactly all the numbers in the desired set.
- Divide all the numbers by these prime factors.
- Repeat the steps above.
- The algorithm ends when you can't find a number different than that divides all the numbers.
To find the GCD, multiply all the divisor.
Note how this is nothing but the prime factorization method, only computed in "real-time" and selecting the factors that divide all the numbers right away.
Binary algorithm for the GCD
The binary algorithm for the GCD uses a clever combination of properties of this operation to find the result without ever computing this quantity directly. The properties used are:
- if is odd; and
- if both and are odd.
By tentatively applying these identities, you can reach the first identity and consequently the GCD. However, we must follow an iterative approach to implement the algorithm effectively. You can discover it with our GCD calculator!
An example of how to use our GCD calculator
To use our GCD calculator simply start inserting the desired numbers in the field at the top of our tool. If you need more numbers, don't worry: the fields will appear!
We will immediately show you the result, so that if you only need quick help with math, you're good to go. However, if you are curious, you can select one of the algorithms we detailed above and see the steps we used to reach the solution.
Other number theory calculators
Calculating the greatest common divisor is a simple mathematical operation, but we can observe it from many different sides. Try them at our related tools:
What is the GCD of 12, 45, 21, and 15?
The answer is 3. To calculate the greatest common divisor of 12, 45, 21, and 15:
- Find the prime factorization of all your numbers:
- 12 = 22 × 3;
- 45 = 32 × 5;
- 21 = 3 × 7; and
- 15 = 3 × 5.
- Identify the prime factors that appear in all the factorizations. In our case, it's only 3.
- Choose the highest possible exponent of the factor above that appears in all the factorizations. In our case, it's 1.
- The GCD is: 31 = 3.
How do I calculate the GCD of 180 and 210 with the upside-down method?
To calculate the greatest common divisor of 180 and 210 with the upside-down method, follow these steps:
- Divide 180 and 210 by the smallest possible prime number that divides them exactly (2):
- 180/2 = 90;
- 210/2 = 105.
- Repeat the step above, but note that now we must divide by 3:
- 90/3 = 30;
- 105/3 = 35.
- This time, do it with 5:
- 30/5 = 6;
- 35/5 = 7.
- The only prime number that divides 6 and 7 is 1.
- Calculate the GCD by multiplying the divisor mentioned above:
GCD(180,210) = 2 × 3 × 5= 30.
What are the identities used in the binary algorithm for the GCD?
To calculate the greatest common divisor with the binary algorithm, we use four identities:
- GCD(0,a) = a;
- GCD(2 · a,2 · v)= 2 · GCD(a,b);
- GCD(2 · a,b) = GCD(a,b), if b is odd; and
- GCD(a,b) = GCD(|a-b|,min(a,b)), if both a and b are odd.
Use them iteratively to reduce the problem of finding GCD(a,b) to the first case, with which you can find the desired result.