This page is still under construction.

Parts of this page are still being built. What you see may change.

Rational Irrationals

Time limit2sMemory limit512 MB

Summary
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 pp, one of the elementary theorems in number theory is that no rational number equals p\sqrt{p}. Such numbers are called irrational numbers. It is also known that rational numbers come arbitrarily close to p\sqrt{p}.

Given a positive integer nn, define the set QnQ_n of all rational numbers that can be written as a ratio of two positive integers, both less than or equal to nn. For example, Q4Q_4 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 pp and nn and outputs two rational numbers x/yx/y and u/vu/v such that u/v<p<x/yu/v < \sqrt{p} < x/y and no other element of QnQ_n lies between u/vu/v and x/yx/y. When nn is greater than p\sqrt{p}, such a pair of rational numbers always exists.

Input

The input consists of lines, each containing a prime number pp and an integer nn, two positive integers in the following format.

p n

The two numbers are separated by a space. You can assume that pp and nn are less than 10000 and that nn is greater than p\sqrt{p}. 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 x/yx/y and u/vu/v (x/y>u/vx/y > u/v) 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.

Examples1

  1. Example 1

    Input
    2 5
    3 10
    5 100
    0 0
    
    Expected output
    3/2 4/3
    7/4 5/3
    85/38 38/17