answersLogoWhite

0


Best Answer

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:

  • The dividend is the number to be divided.
  • The divisor is the number being divided by.
  • The quotient is the number of times the divisor divides into the dividend.
  • The remainder is the amount "left over" when the divisor cannot go into the dividend an integral number of times.
18A divided by 12B gives a quotient of 1C and a remainder of 6D.

A is the dividend, B is the divisor, C is the quotient, and D is the remainder.

The Euclidean algorithm works like this:

  1. Check if either of the two integers is 0. If so, there is no solution (Ø), as a number cannot share a GCD with zero. Besides, division by zero is a big no-no.
  2. Check if either of the two integers is 1. If so, 1 is the GCD.
  3. Divide the larger of the two integers by the smaller.
  4. Divide the divisor of the previous division operation by the remainder of the previous operation.
  5. Repeat step four until the remainder equals zero. When the remainder equals zero, the divisor of the last operation is the GCD.

If you still don't get it, try looking at the Euclidean algorithm in action:

Find the GCD of 84 and 18.

  1. Check to see if either 84 or 18 is equal to 0. Nope. Continue on...
  2. Check to see if either 84 or 18 is equal to 1. Nope. Continue on...
  3. Since 84 is larger than 18, divide 84 by 18. Quotient is 4, remainder is 12.
  4. Take the divisor of the last operation (18) and divide it by the remainder of the last operation (12). Quotient is 1, remainder is 6.
  5. Take the divisor of the last operation (12) and divide it by the remainder of the last operation (6). Quotient is 2, remainder is 0.
  6. When the remainder is 0, the divisor of the last operation is the GCD. So the GCD in this case is 6.

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.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

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.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a Java program that finds the greatest common factor of two integers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you flowchart greatest common denominator?

Just use the GCF(greatest common factor


What is a GCD?

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.


Write a java program to find rational number in reduce form?

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;}


Write a program to find the L.C.M. and HCF of two number using function where numbers are passed as parameter use function with return value?

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).


How do you find the least common multiple in python program?

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.

Related questions

What are the relationships of factors common factor and greatest common factors?

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.


What is the greatest number that is a factor 0f 2 or more integers?

That's the greatest common factor, or GCF.


What is the gcm of 64 and 52?

The Greatest Common Factor (GCF) is: 9 .The greatest common multiple of any set of integers is infinite.


What is a Greatest Common Factor of 34 and 35?

The greatest common factor of any two consecutive integers is 1.


What is the greatest common factor of 1325 and 3498?

The least common factor of any set of integers is 1.


Gcm of 20 and 8?

The Greatest Common Factor (GCF) is: 4 .The greatest common multiple of any set of integers is infinite.


GCM of 100 and 24?

The Greatest Common Factor (GCF) is: 4 .The greatest common multiple of any set of integers is infinite.


GCM 27 AND 72?

The Greatest Common Factor (GCF) is: 9 .The greatest common multiple of any set of integers is infinite.


What is the least common factor of 54 and 9?

The least common factor of any set of integers is 1.


What is the GCM for 7 9 and 0?

The Greatest Common Factor (GCF) is: 1 .The greatest common multiple of any set of integers is infinite.


What is the gcm for 0 9 7?

The Greatest Common Factor (GCF) is: 1 .The greatest common multiple of any set of integers is infinite.


What is the most common factor of 28 and 84 and 91?

The greatest common factor of any set of integers is infinite.