Euclid
InterviewTime limit2sMemory limit512 MB
Read two positive integers up to 32767 and print their greatest common divisor.
- Level
Easy2 of 10
- Topics
- Number theory
- Solved
- No attempts yet
Problem
The famous Euclidean algorithm is found in Book VII of the Elements. The Elements was written in 300 B.C. by the Greek mathematician Euclid. It is rumored that King Ptolemy, having looked through the Elements, hopefully asked Euclid if there were not a shorter way to geometry, and that Euclid answered sternly: "In geometry there is no royal road!" Probably we should not blame the King for looking for short cuts, because there are thirteen books in the Elements. The books consist mainly of the mathematical knowledge Euclid amassed, and possibly some discoveries of his own. His great achievement is the beautifully systematic presentation of that material as an organic whole. The Elements remained a standard work for over two thousand years.
The original Euclidean algorithm uses subtraction to find the greatest common divisor (gcd) of two positive integers and . It is based on the observation that a common divisor of and is also a common divisor of and . So the gcd of and is found like this.
- If , the gcd is and the algorithm ends.
- Replace by , and by . Go to step 1.
With the original Euclidean algorithm or otherwise, find the gcd of two positive integers.
For and , the original Euclidean algorithm walks through these values.
- ,
- ,
- ,
- ,
That is, .
Input
The input contains one line. The line contains two positive integers separated by a space, each not larger than 32767.
Output
Print one integer, the gcd of the two given positive integers.