How to Find the Greatest Common Divisor of Two Integers - Using the Divisor Algorithm Drop any negative signs. Know your vocabulary: Identify the larger of the two numbers. Write out this algorithm: Put the larger number in the spot for dividend, and the smaller number as the divisor.
The greatest common factor (GCF) of a set of numbers is the largest number that’s a factor of every number in that set. Finding the GCF is helpful when you want to reduce a fraction to its lowest terms.
You can find the GCF in two ways. The first option is to list all the factor pairs of the numbers and choose the largest factor that appears in both (or all) the lists.
The other method uses prime factors, which I discuss in the preceding section. Here’s how to find the GCF:
Sample questions
Practice questions
Following are the answers to the practice questions:
How To Find The Greatest Common Divisor In Matlab
Active3 years, 10 months ago
$begingroup$
I wrote a program to find the greatest common divisor between two numbers. How do I improve this program?
user56102
How To Find The Greatest Common Divisor Of A Fraction$endgroup$5 Answers$begingroup$
A more elegant implementation of the
gcd function:
To ensure that the arguments are positive, it's easiest to use the
abs(...) function.
Please also indent your code nicely to make it more readable.
And put spaces around operators. For example, instead of this:
Finding Highest Common Factor
Write like this:
Putting it together, the implementation of the entire program can become short and sweet:
You can play with this on ideone.
janosjanos
101k1313 gold badges135135 silver badges357357 bronze badges
$endgroup$$begingroup$
Community♦
Quaxton HaleQuaxton Hale
2,28411 gold badge3030 silver badges5151 bronze badges
$endgroup$$begingroup$
Firstly, as this is meant to be for code reviews, I will immediately point out that you are putting everything into main and you really should put the algorithm into its own function. Your main can look at its argument list and possibly support having the parameters in there. For simplistic terms you can use them if they are there and prompt if not.
For an implementation of the function, it can be a simple free-function. There is no need to use a class for it.
without recursion
For a simple example, gcd of 987 and 1491
The complexity of this algorithm: well the worst case scenario is actually 2 consecutive Fibonacci numbers. The answer will be 1 but if this is the nth and (n+1)th Fibonacci number it will take n iterations to unravel.
Thus it's
O(log N) complexity where N is the higher of the two numbers.
CashCowCashCow
$endgroup$$begingroup$
I'm going to address your original algorithm, as I think that there are some useful lessons that can be learned there. Note that EngieOP's algorithm is going to be faster than this though.
When doing something like this, think about the direction that you want the for loop to go. You want the greatest common divisor, but your for loop starts with the smallest possible divisor. Turn it around and go in the other direction.
Also note that you know that the greatest common divisor is going to be at most the smaller of the two numbers. So calculate the smaller number.
Note how we can stop as soon as we find the first match because we went largest to smallest. This saves us loop iterations, a variable declaration, and an if statement.
Community♦
BrythanBrythan
6,75933 gold badges1616 silver badges3737 bronze badges
$endgroup$
Greatest Common Divisor Algorithm$begingroup$
First and foremost, you don't want to write a program to calculate
gcd . You want to write a function to calculate it, and a program to test the function. Don't put everything in main .
Second, Euclid spelled out the algorithm really well; there's (almost) no point to deviate from it unless you are seriously into the number theory; check out a Stein's approach.
How To Find The Greatest Common Divisor Of Two Natural Numbers
The rest is well addressed in the answers above.
vnpvnp
43.6k22 gold badges3737 silver badges112112 bronze badges
$endgroup$
protected by Jamal♦Dec 8 '15 at 3:15
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead? Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |