Problem:
Problem A: Rational Irrationals

Rational numbers are numbers represented by ratios of two integers. For a prime number p, one of the elementary theorems in the number theory is that there is no rational number equal to √p. Such numbers are called irrational numbers. It is also known that there are rational numbers arbitrarily close to √p

Now, given a positive integer n, we define a set Qn of all rational numbers whose elements are represented by ratios of two positive integers both of which are less than or equal to n. For example, Q4 is a 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 here because they are equal to 1/1, 1/2, 1/1, 2/1 and 1/1, respectively.

Your job is to write a program that reads two integers p and n and reports two rational numbers x / y and u / v, where u / v < √p < x / y and there are no other elements of Qn between u/v and x/y. When n is greater than √p, such a pair of rational numbers always exists.

Input

The input consists of lines each of which contains two positive integers, a prime number p and an integer n in the following format.

p n

They are separated by a space character. You can assume that p and n are less than 10000, and that n is greater than √p. The end of the input is indicated by a line consisting of two zeros.

Output

For each input line, your program should output a line consisting of the two rational numbers x / y and u / v (x / y > u / v) separated by a space character in the following format.

x/y u/v

They should be irreducible. For example, 6/14 and 15/3 are not accepted. They should be reduced to 3/7 and 5/1, respectively.

Sample Input

2 5
3 10
5 100
0 0

Output for the Sample Input

3/2 4/3
7/4 5/3
85/38 38/17

Solution in JAVA (judge verdict: Memory Limit Exceeded)

import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {
    void run() {
        Scanner sc = new Scanner(System.in);
        while (true) {
            int p = sc.nextInt();
            int n = sc.nextInt();
            if ((n | p) == 0)
                break;
            Set<Rational> set = new HashSet<Rational>();
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    int a = i;
                    int b = j;
                    int d = gcd(a, b);
                    a /= d;
                    b /= d;
                    set.add(new Rational(a, b));

                }
            }
            Rational max = new Rational(1, n);
            Rational min = new Rational(n, 1);
            Rational[] rs = set.toArray(new Rational[set.size()]);
            double rp = Math.sqrt(p);
            for (int i = 0; i < rs.length; i++) {
                if (rp > rs[i].v && max.compareTo(rs[i]) < 0) {
                    max = rs[i];
                    continue;
                }
                if (rp < rs[i].v && min.compareTo(rs[i]) > 0) {
                    min = rs[i];
                    continue;
                }
            }
            System.out.println(min + " " + max);
        }
    }

    int gcd(int a, int b) {
        if (a < b) {
            a = a ^ b;
            b = a ^ b;
            a = a ^ b;
        }
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }

    public static void main(String[] args) throws Exception {
        new Main().run();
    }
}

class Rational {
    int a;
    int b;
    double v;
    static double EPS = 1.0e-10;

    Rational(int a, int b) {
        this.a = a;
        this.b = b;
        this.v = 1.0 * a / b;
    }

    public boolean equals(Object o) {
        Rational r = (Rational) o;
        return r.a == a && r.b == b;
    }

    public int hashCode() {
        return Integer.parseInt(a + "0" + b);
    }

    public int compareTo(Rational r) {
        if (Math.abs(v - r.v) < EPS)
            return 0;
        else if (v - r.v > 0) {
            return 1;
        } else
            return -1;
    }

    public String toString() {
        return a + "/" + b;
    }
}