GCD Calculator
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: it is 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, follow the following steps:
-
Find the prime factors of all the numbers in your set. For instance, if we seek the GCD of 40 and 60, then their prime factors are 2 and 5 (for 40) and 2, 3, 5 (for 60).
-
For every prime number dividing all the numbers in your set, find its largest power that divides all the numbers in your set. In our case, the only prime factors dividing both 40 and 60 are 2 and 5. The largest power of 2 dividing both 40 and 60 is 4 = 2², while the largest power of 5 dividing 40 and 60 is 5 itself.
-
Multiply these prime powers together to get the GCD. In our case, this will be 20 = 4 × 5. If our numbers have no common prime divisors (i.e., they are coprime/relatively prime), then the GCD is 1.
Via this method it is easy to see that the GCD of a set of prime numbers is 1.
Calculate the GCD with the Euclidean algorithm
The original Euclidean algorithm for the GCD requires just a bunch of subtractions. The process is simple. Suppose you want to find the GCD of two natural numbers and and suppose that is no greater than .
-
Start by subtracting the smaller number from the larger .
-
Substitute the larger number with the result , so that you know consider the pair instead of .
-
Repeat step 1, subtracting the smaller of from the larger one. Depending on the original choice of numbers, you may have to subtract either the same number again or the new number .
-
Substitute the larger number with the result.
-
Repeat the algorithm until you reach two equal numbers: this is the GCD.
For instance, starting with 60 and 40, we first subtract 60 - 40 to get 20 and then replace 60 with 20. This gives us the pair 40 and 20. Subtracting 20 from 40, we get 20, with which we then replace 40. This leaves us with two same numbers, 20 and 20, and this is precisely the GCD of 40 and 60.
Modified Euclidean algorithm
The Euclidean algorithm using subtraction can become pretty lengthy (in particular, if the two numbers differ 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 0. The GCD is then the smaller of the two numbers from the previous step.
For our previous example of 60 and 40, the modified Euclidean algorithm will not differ much from its original version, so we instead consider a different example: 60 and 9:
-
60 is congruent to 6 modulo 9, and so we replace 60 with 6. Thus, our new pair is 9 and 6.
(If we wanted to reach this step using the original Euclidean algorithm, we would have to subtract 9 from 60 six times before we would reach the pair 60 and 9. With the modified Euclidean algorithm, we simplify it to one calculation.)
-
9 is congruent to 3 modulo 6. We thus replace 9 with 3, giving us the pair 6 and 3.
-
Now, 6 is congruent to 0 mod 3, and so following the last step of the algorithm, we take the GCD to be 3, the smaller of the two numbers 6, 3 from the last step.
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 division. 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 only 1 divides all the numbers.
To find the GCD, multiply all the divisors. Note that this is nothing but the prime factorization method, only computed in "real-time" and selecting the factors that divide all the numbers right away.
For instance, suppose we want to compute the GCD of 60 and 40 again:
- 2 divides both numbers, so we divide out 60/2 = 30 and 40/2 = 20.
- 2 divides both 30 and 20, so 30/2 = 15 and 20/2 = 10.
- Neither 2 nor 3 divides 15 and 10, but the next prime factor 5 does, so we divide 15/5 = 3 and 10/5 = 2.
- 3 and 2 are coprime (i.e., they have no common factors other than 1), and so we halt. The GCD is the product of divisors used in the previous steps: 2 × 2 × 5 = 20.
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 iteratively 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.
Tiling: GCD in real life
Greatest common divisors are very useful in real life: here is one reason why. Suppose that you want to cover a rectangular wall with square tiles without breaking any tiles. How big can the tiles be? If the wall has dimensions , then the side length of your tile has to divide both the lengths; otherwise, the tiles will not fit.
In other words, has to be a common divisor of both and . The largest admissible side length of your tile is, therefore, . So, if you want to decorate your bathroom walls, better know how to calculate the greatest common factor!
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:
FAQ
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.