Given two positive integers, find the maximum value of gcd(a, b) over all pairs formed by choosing one divisor of each number.
I'm bored with life
You are given two positive integers and .
Consider all possible pairs such that:
- is a positive divisor of ,
- is a positive divisor of .
For every such pair, compute . Your task is to output the maximum value that can be obtained.
Intuition
The answer depends only on the greatest common divisor of the two numbers, because any common divisor of and can be chosen as a divisor of each.
Input Format
The input consists of a single line containing two integers and .
Output Format
Print one integer — the maximum possible value of over all valid divisor pairs.
Constraints
- and are positive integers
Example 1
Input
10 12
Output
2
Explanation
Divisors of 10 are 1, 2, 5, 10 and divisors of 12 are 1, 2, 3, 4, 6, 12. The largest gcd obtainable is 2, for example with x = 2 and y = 2.
Example 2
Input
18 24
Output
6
Explanation
A common divisor of both numbers cannot exceed gcd(18, 24) = 6, and this value is achievable by choosing x = 6 and y = 6.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.