given a non-negative fraction q, return a pair (a,b) such that q = a * a * b
        where b is a square-free integer.

        if q is a perfect square, a is its square root and b is one.

def fsqrt(q):
    ''' given a non-negative fraction q, return a pair (a,b) such that q = a * a * b
        where b is a square-free integer.

        if q is a perfect square, a is its square root and b is one.
    '''

    if q == 0:
        return q, 1
    if q < 0:
        raise ValueError('math domain error %s' % q)

    a, b = isqrt(q.numerator)
    c, d = isqrt(q.denominator)

    # q == (a/c)**2 * (b/d) == (a/(c*d))**2 * b*d
    return Fraction(a, c * d), b * d