Rational Irrationals
Time limit2sMemory limit512 MB
For each prime p and bound n, output the two fractions with numerator and denominator at most n that bracket sqrt(p) most tightly, in lowest terms.
- Level
Medium5 of 10
- Topics
- Math, Number theory, Sorting, Brute force
- Solved
- No attempts yet
Problem
A rational number is a number that can be written as a ratio of two integers. For a prime number , one of the elementary theorems in number theory is that no rational number equals . Such numbers are called irrational numbers. It is also known that rational numbers come arbitrarily close to .
Given a positive integer , define the set of all rational numbers that can be written as a ratio of two positive integers, both less than or equal to . For example, is the set of 11 rational numbers {1/1, 1/2, 1/3, 1/4, 2/1, 2/3, 3/1, 3/2, 3/4, 4/1, 4/3}. 2/2, 2/4, 3/3, 4/2, and 4/4 are not included because they equal 1/1, 1/2, 1/1, 2/1, and 1/1, respectively.
Your job is to write a program that reads two integers and and outputs two rational numbers and such that and no other element of lies between and . When is greater than , such a pair of rational numbers always exists.
Input
The input consists of lines, each containing a prime number and an integer , two positive integers in the following format.
p n
The two numbers are separated by a space. You can assume that and are less than 10000 and that is greater than . The end of the input is indicated by a line consisting of two zeros.
Output
For each input line, output one line containing the two rational numbers and () separated by a space in the following format.
x/y u/v
They must be irreducible. For example, 6/14 and 15/3 are not accepted. They must be reduced to 3/7 and 5/1, respectively.