Common Fraction
InterviewTime limit1sMemory limit128 MB
Reduce each of n fractions to lowest terms by dividing out the greatest common divisor.
- Level
Easy3 of 10
- Topics
- Number theory
- Solved
- No attempts yet
Problem
A common fraction, also called a simple fraction, is a rational number written as , where the integers and are the numerator and the denominator. The denominator cannot be zero.
Dividing the numerator and the denominator by the same non-zero number gives a fraction of equal value. That step is called reducing the fraction. A fraction whose numerator and denominator are coprime, meaning the only positive integer that divides both of them is 1, is in lowest terms. For example, is not in lowest terms because 3 and 9 are both divisible by 3. On the other hand, is in lowest terms, since the only positive integer that divides both 3 and 8 is 1.
Write a program that reduces the given fractions to lowest terms.
Input
The first line contains the number of fractions ().
Each of the next lines contains the numerator and the denominator of one fraction, separated by a space (, ).
Output
For each fraction, in the order it was given, print its lowest terms on one line. Separate the numerator and the denominator with a single space.