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: Accepted)

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
	
	public static int gcd(int x, int y){
		if(y == 0){
			return x;
		}else{
			return gcd(y, x % y);
		}
	}


	public static void main(String[] args) {
		final Scanner sc = new Scanner(System.in);

		while (true) {
			final int p = sc.nextInt();
			final int n = sc.nextInt();
			
			if(p == 0 && n == 0){
				break;
			}
			
			final double ans = Math.sqrt(p);
			double less = Double.MIN_VALUE;
			double more = Double.MAX_VALUE;
			int less_u = 1, less_v = 1, more_x = 1, more_y = 1;
			
			//bottom
			for(int b = 1; b <= n; b++){
				
				//top
				int high_a = n + 1;
				int low_a  = 1;
				while(high_a - low_a > 1){
					
					int mid_a = (high_a + low_a) / 2;
					
					double raito = (double) (mid_a) / b;
					
					if(ans < raito){
						high_a = mid_a;
					}else if(ans > raito){
						low_a = mid_a;
					}else{
						high_a = low_a = mid_a;
						break;
					}
				}

				double low_raito = (double) (low_a) / b;
				double high_raito = (double) (high_a) / b;

				//System.out.println(b + " " + high_a + " " + low_a);
				
				if(low_raito <= ans && low_raito >= less){
					final int gcd = gcd(low_a, b);
					
					//System.out.println("updated less " + b + " : " + less + " => " + low_raito);
					
					less   = low_raito;
					less_u = low_a / gcd;
					less_v = b / gcd;
				}
				
				if(high_a <= n && high_raito >= ans && high_raito <= more){
					final int gcd = gcd(high_a, b);
					
					///System.out.println("updated more " + b +" : " + more  + " => " + high_raito);
					
					more   = high_raito;
					more_x = high_a / gcd;
					more_y = b / gcd;
				}
				//System.out.println(more + " " + less);
			}
			
			System.out.println(more_x + "/" + more_y + " " + less_u + "/" + less_v);
			
		}
	}

}