Lesson goal: The GCD and reducing fractions to lowest terms

Previous: Finding the Greatest Common Divisor (GCD) | Home | Next: A better GCD calculator

In this last lesson, we developed a function that will find the GCD of two numbers. It turns out when you "reduce a fraction to lowest terms," you are really dividing both the numerator and denominator by the GCD. So we'll do that here: program a simple "fraction reducer" using the GCD function from the last lesson.

But, there's a small issue. Remember in the gcd(a,b) function that the first argument (a) must be less than the second argument (b). To be sure our fraction reducer will always work, let's write a "wrapper function" called get_gcd that will ensure that the main gcd function gets called with the lower number first.

Now you try. Fix the if statement to compare a and b, and the two gcd function calls to ensure that gcd(a,b) always gets called with $a< b$.

Type your code here:


See your results here: