def sum_of_four_squares(n):
 if (n == 0):
  return (0, 0, 0, 0)
 v = multiplicity(4, n)
 n //= (4 ** v)
 if ((n % 8) == 7):
  d = 2
  n = (n - 4)
 elif (((n % 8) == 6) or ((n % 8) == 2)):
  d = 1
  n = (n - 1)
 else:
  d = 0
 (x, y, z) = sum_of_three_squares(n)
 return _sorted_tuple(((2 ** v) * d), ((2 ** v) * x), ((2 ** v) * y), ((2 ** v) * z))

def sum_of_four_squares ( n ) : if ( n == 0 ) : return ( 0 , 0 , 0 , 0 ) v = multiplicity ( 4 , n ) n //= ( 4 ** v ) if ( ( n % 8 ) == 7 ) : d = 2 n = ( n - 4 ) elif ( ( ( n % 8 ) == 6 ) or ( ( n % 8 ) == 2 ) ) : d = 1 n = ( n - 1 ) else : d = 0 ( x , y , z ) = sum_of_three_squares ( n ) return _sorted_tuple ( ( ( 2 ** v ) * d ) , ( ( 2 ** v ) * x ) , ( ( 2 ** v ) * y ) , ( ( 2 ** v ) * z ) )
returns a 4-tuple such that a^2 + b^2 + c^2 + d^2 = n .