The easiest way to find the greatest common denominator of two integers with a computer program is to use the Euclidean algorithm. Of the most popular methods of finding the GCD of two numbers, the Euclidean algorithm does it with the least amount of work and requires the least amount of code.
In order to understand the Euclidean algorithm, you'll need to know a few division terms:
A is the dividend, B is the divisor, C is the quotient, and D is the remainder.
The Euclidean algorithm works like this:
If you still don't get it, try looking at the Euclidean algorithm in action:
Find the GCD of 84 and 18.
You should now have a good grasp of how the Euclidean algorithm works. Now we need to turn it into code. We'll need three variables, all of them integers:
int divisor, dividend, remainder;
The purpose of the variables is self-explanatory. Next, we need to make a few decisions. We need to decide if the dividend or the divisor is 0. If that test is passed, then we need to decide if the dividend or the divisor is 1. If that test is passed, then we need make sure that dividend is larger than divisor.
if(dividend 1) {
printf("The GCD is 1.\n");
}
// Make sure the dividend is greater than the divisor.
if(divisor > dividend) {
remainder = dividend;
dividend = divisor;
divisor = remainder;
}
// Calculate the GCD.
while(remainder != 0) {
remainder = dividend % divisor;
dividend = divisor;
divisor = remainder;
}
// Display the answer to the user.
printf("The GCD is %i.\n", dividend);
}
And the GCD lived happily ever after. The end.
If you're interested only in solving the problem, and not in performance, then the simplest way is to simply start by trying to divide one number by the other. If they divide evenly, then that number is the GCF, else subtract it by one and keep trying.
public static int GCF(int a, int b) {
// current will start off as the lesser of the two numbers.
// after each attempt to find a common factor, decrement current
int current = Math.min(a,b);
// loop until current is 1. if this happens, all other possibilities have been
// eliminated and thus a and b share no common factors other than 1
while( current > 1) {
// check factors
if( (a%current) 0) { // check if current is a factor of b
// if current is a factor of both a and b, then current
// is the GCF and we can break out of the loop
break;
}
}
--current;
}
return current;
}
// or for a slightly more concise version...
public static int GCF(int a, int b) {
for(int current = Math.min(a,b); current > 1; --current) {
if( (a%current)==0 && (b%current==0) ) {
return current;
}
}
return 1;
}
Again, both of these algorithms perform in O(n) time, which can be quite terrible for large numbers and/or slow machines.
Just use the GCF(greatest common factor
GCD stands for the Greatest Common Denominator. No it doesn't. GCD is the greatest common divisor, also known as the greatest common factor. The greatest common Denominator dne.
I assume numerator and denominator will be in two different variables, Just divide both of them by the greatest common factor.Here is a method to calculate the greatest common factor; it is based on the fact that, for example, the greatest common factor of 14 and 10 is the same as the greatest common factor of 10 and 4, where 4 is calculated as 14 - 10 (or faster, to avoid repeated subtractions, 14 % 10).public static void main(String[] args) {System.out.println(gcf(300L, 200L));}static long gcf(long a, long b){long c;while (true){c = a % b;if (c == 0) break;a = b;b = c;}return b;}
For the greatest common factor, you can use the following to your advantage. As an example, take the numbers 14 and 10 as input.The greatest common factor of 14 and 10 is the same as the greatest common factor of 10 and 4, where 4 has been obtained by subtracting 14 - 10 (or, faster, to avoid repeated subtraction, take the remainder of a division: 14 % 10).If you divide 10 % 4 (or subtract 4 twice, from 10), you get a remainder of 2, so the new set of numbers is 4 and 2.Next step: 4 % 2 = 0. Once you get a remainder of zero, the previous number is the answer - the number that you should return. In this case, the 2.For the least common multiple, use the property that (using a numeric example) 14 x 10 = 2 x 70 (14 and 10 are the two parameters, 2 and 70 are the greatest common factor and the least common multiple, respectively).For the greatest common factor, you can use the following to your advantage. As an example, take the numbers 14 and 10 as input.The greatest common factor of 14 and 10 is the same as the greatest common factor of 10 and 4, where 4 has been obtained by subtracting 14 - 10 (or, faster, to avoid repeated subtraction, take the remainder of a division: 14 % 10).If you divide 10 % 4 (or subtract 4 twice, from 10), you get a remainder of 2, so the new set of numbers is 4 and 2.Next step: 4 % 2 = 0. Once you get a remainder of zero, the previous number is the answer - the number that you should return. In this case, the 2.For the least common multiple, use the property that (using a numeric example) 14 x 10 = 2 x 70 (14 and 10 are the two parameters, 2 and 70 are the greatest common factor and the least common multiple, respectively).For the greatest common factor, you can use the following to your advantage. As an example, take the numbers 14 and 10 as input.The greatest common factor of 14 and 10 is the same as the greatest common factor of 10 and 4, where 4 has been obtained by subtracting 14 - 10 (or, faster, to avoid repeated subtraction, take the remainder of a division: 14 % 10).If you divide 10 % 4 (or subtract 4 twice, from 10), you get a remainder of 2, so the new set of numbers is 4 and 2.Next step: 4 % 2 = 0. Once you get a remainder of zero, the previous number is the answer - the number that you should return. In this case, the 2.For the least common multiple, use the property that (using a numeric example) 14 x 10 = 2 x 70 (14 and 10 are the two parameters, 2 and 70 are the greatest common factor and the least common multiple, respectively).For the greatest common factor, you can use the following to your advantage. As an example, take the numbers 14 and 10 as input.The greatest common factor of 14 and 10 is the same as the greatest common factor of 10 and 4, where 4 has been obtained by subtracting 14 - 10 (or, faster, to avoid repeated subtraction, take the remainder of a division: 14 % 10).If you divide 10 % 4 (or subtract 4 twice, from 10), you get a remainder of 2, so the new set of numbers is 4 and 2.Next step: 4 % 2 = 0. Once you get a remainder of zero, the previous number is the answer - the number that you should return. In this case, the 2.For the least common multiple, use the property that (using a numeric example) 14 x 10 = 2 x 70 (14 and 10 are the two parameters, 2 and 70 are the greatest common factor and the least common multiple, respectively).
I suggest you use the property that lcm(a, b) * gcf(a, b) = a * b. Solving for the least common multiple: lcm(a, b) = a * b / gcf(a, b). The greatest common factor can be obtained with Euclid's algorithm. For example, the gcf of 14 and 10 is the same as the gcf of 10 and 4, where 4 is the remainder of dividing 14 by 10. In most programming languages, this remainder is calculated with the percent sign: 14 % 10, or rather, a % b. Repeat until you get a zero remainder.
All integers have factors.Some integers have some of the same factors as other integers. These are known as common factors. The largest of these is the greatest common factor, or GCF.
That's the greatest common factor, or GCF.
The Greatest Common Factor (GCF) is: 9 .The greatest common multiple of any set of integers is infinite.
The greatest common factor of any two consecutive integers is 1.
The least common factor of any set of integers is 1.
The Greatest Common Factor (GCF) is: 4 .The greatest common multiple of any set of integers is infinite.
The Greatest Common Factor (GCF) is: 4 .The greatest common multiple of any set of integers is infinite.
The Greatest Common Factor (GCF) is: 9 .The greatest common multiple of any set of integers is infinite.
The least common factor of any set of integers is 1.
The Greatest Common Factor (GCF) is: 1 .The greatest common multiple of any set of integers is infinite.
The Greatest Common Factor (GCF) is: 1 .The greatest common multiple of any set of integers is infinite.
The greatest common factor of any set of integers is infinite.