(n)
return (2**v*x, 2**v*y, 2**v*z)
Returns a 3-tuple `(a, b, c)` such that `a^2 + b^2 + c^2 = n` and
    `a, b, c \geq 0`.

    Returns (None, None, None) if `n = 4^a(8m + 7)` for some `a, m \in Z`. See
    [1]_ for more details.

    Usage
    =====

    ``sum_of_three_squares(n)``: Here ``n`` is a non-negative integer.

    Examples
    ========

    >>> from sympy.solvers.diophantine import sum_of_three_squares
    >>> sum_of_three_squares(44542)
    (207, 37, 18)

    References
    ==========

    .. [1] Representing a number as a sum of three squares, [online],
        Available: http://www.schorn.ch/howto.html
Returns a 3-tuple `(a, b, c)` such that `a^2 + b^2 + c^2 = n` and
    `a, b, c \geq 0`.
Returns a 3 - tuple ( a b c ) such that a^2 + b^2 + c^2 = n and a b c \ geq 0 .
def sum_of_three_squares(n):
    """
    Returns a 3-tuple `(a, b, c)` such that `a^2 + b^2 + c^2 = n` and
    `a, b, c \geq 0`.

    Returns (None, None, None) if `n = 4^a(8m + 7)` for some `a, m \in Z`. See
    [1]_ for more details.

    Usage
    =====

    ``sum_of_three_squares(n)``: Here ``n`` is a non-negative integer.

    Examples
    ========

    >>> from sympy.solvers.diophantine import sum_of_three_squares
    >>> sum_of_three_squares(44542)
    (207, 37, 18)

    References
    ==========

    .. [1] Representing a number as a sum of three squares, [online],
        Available: http://www.schorn.ch/howto.html
    """
    special = {1:(1, 0, 0), 2:(1, 1, 0), 3:(1, 1, 1), 10: (1, 3, 0), 34: (3, 3, 4), 58:(3, 7, 0),
        85:(6, 7, 0), 130:(3, 11, 0), 214:(3, 6, 13), 226:(8, 9, 9), 370:(8, 9, 15),
        526:(6, 7, 21), 706:(15, 15, 16), 730:(1, 27, 0), 1414:(6, 17, 33), 1906:(13, 21, 36),
        2986: (21, 32, 39), 9634: (56, 57, 57)}

    v = 0

    if n == 0:
        return (0, 0, 0)

    while n % 4 == 0:
        v = v + 1
        n = n // 4

    if n % 8 == 7:
        return (None, None, None)

    if n in special.keys():
        x, y, z = special[n]
        return (2**v*x, 2**v*y, 2**v*z)

    l = int(sqrt(n))

    if n == l**2:
        return (2**v*l, 0, 0)

    x = None

    if n % 8 == 3:
        l = l if l % 2 else l - 1

        for i in xrange(l, -1, -2):
            if isprime((n - i**2) // 2):
                x = i
                break

        y, z = prime_as_sum_of_two_squares((n - x**2) // 2)
        return (2**v*x, 2**v*(y + z), 2**v*abs(y - z))

    if n % 8 == 2 or n % 8 == 6:
        l = l if l % 2 else l - 1
    else:
        l = l - 1 if l % 2 else l

    for i in xrange(l, -1, -2):
        if isprime(n - i**2):
            x = i
            break

    y, z = prime_as_sum_of_two_squares(n - x**2)
    return (2**v*x, 2**v*y, 2**v*z)
def sum_of_three_squares ( n ) : special = { 1 : ( 1 , 0 , 0 ) , 2 : ( 1 , 1 , 0 ) , 3 : ( 1 , 1 , 1 ) , 10 : ( 1 , 3 , 0 ) , 34 : ( 3 , 3 , 4 ) , 58 : ( 3 , 7 , 0 ) , 85 : ( 6 , 7 , 0 ) , 130 : ( 3 , 11 , 0 ) , 214 : ( 3 , 6 , 13 ) , 226 : ( 8 , 9 , 9 ) , 370 : ( 8 , 9 , 15 ) , 526 : ( 6 , 7 , 21 ) , 706 : ( 15 , 15 , 16 ) , 730 : ( 1 , 27 , 0 ) , 1414 : ( 6 , 17 , 33 ) , 1906 : ( 13 , 21 , 36 ) , 2986 : ( 21 , 32 , 39 ) , 9634 : ( 56 , 57 , 57 ) } v = 0 if n == 0 : return ( 0 , 0 , 0 ) while n % 4 == 0 : v = v + 1 n = n // 4 if n % 8 == 7 : return ( None , None , None ) if n in special . keys ( ) : x , y , z = special [ n ] return ( 2 ** v * x , 2 ** v * y , 2 ** v * z ) l = int ( sqrt ( n ) ) if n == l ** 2 : return ( 2 ** v * l , 0 , 0 ) x = None if n % 8 == 3 : l = l if l % 2 else l - 1 for i in xrange ( l , - 1 , - 2 ) : if isprime ( ( n - i ** 2 ) // 2 ) : x = i break y , z = prime_as_sum_of_two_squares ( ( n - x ** 2 ) // 2 ) return ( 2 ** v * x , 2 ** v * ( y + z ) , 2 ** v * abs ( y - z ) ) if n % 8 == 2 or n % 8 == 6 : l = l if l % 2 else l - 1 else : l = l - 1 if l % 2 else l for i in xrange ( l , - 1 , - 2 ) : if isprime ( n - i ** 2 ) : x = i break y , z = prime_as_sum_of_two_squares ( n - x ** 2 ) return ( 2 ** v * x , 2 ** v * y , 2 ** v * z )