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 Q n 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, Q 4 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 Q n 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 (Python):
import math
while(1):
    [p,n]=[int(x) for x in raw_input().split()]
    if p==0:
        break
    else:
       sqrtp=math.sqrt(p)
       smaller=[math.floor(sqrtp),int(math.floor(sqrtp)),1]
       larger=[math.ceil(sqrtp),int(math.ceil(sqrtp)),1]
       for numerator in range(int(math.floor(sqrtp))+1,n+1):
           denom_smaller=math.ceil(numerator/sqrtp)
           denom_larger=math.floor(numerator/sqrtp)
           if numerator/denom_smaller>smaller[0]:
               if smaller[1]*denom_smaller/smaller[2]!=numerator:
                   smaller=[numerator/denom_smaller,numerator,int(denom_smaller)]
           if numerator/denom_larger<larger[0]:
               if larger[1]*denom_larger/larger[2]!=numerator:
                   larger=[numerator/denom_larger,numerator,int(denom_larger)]
       print str(larger[1])+"/"+str(larger[2])+" "+str(smaller[1])+"/"+str(smaller[2])

Solution (Python):
import sys
readline = sys.stdin.readline
write = sys.stdout.write

def stern_brocot(p, n):
    la = 0; lb = 1
    ra = 1; rb = 0
    lu = ru = 1
    lx = 0; ly = 1
    rx = 1; ry = 0
    while lu or ru:
        ma = la + ra; mb = lb + rb
        if p * mb**2 < ma**2:
            ra = ma; rb = mb
            if ma <= n and mb <= n:
                rx = ma; ry = mb
            else:
                lu = 0
        else:
            la = ma; lb = mb
            if ma <= n and mb <= n:
                lx = ma; ly = mb
            else:
                ru = 0
    return lx, ly, rx, ry

while 1:
    p, n = map(int, readline().split())
    if p == 0:
        break
    lx, ly, rx, ry = stern_brocot(p, n)
    write("%d/%d %d/%d\n" % (rx, ry, lx, ly))

Solution (Python):

import sys
import math

def rational(p, n):
    rt = math.sqrt(p)
    k = math.floor(rt)
    mi = n
    mx = n
    mikk = 0
    mibb = 0
    mxkk = 0
    mxbb = 0
    for i in range(1, n+1):
        a = (rt - k) * i
        b = math.floor(a)
        if mi > a - b and k * i + b <= n:
            mi = a - b
            mikk = i
            mibb = b
        if mx > b + 1 - a and k * i + (b + 1) <= n:
            mx = b + 1 - a
            mxkk = i
            mxbb = b
    print("{}/{} {}/{}".format(k * mxkk + (mxbb + 1), mxkk, k * mikk + mibb, mikk))
    
while True:
    line = input().split()
    p, n = map(int, list(line))
    if p == 0 and n == 0:
        break
    rational(p, n)