def is_algebraic(p):
 if p.is_Rational:
  return True
 elif p.is_Atom:
  return False
 elif (is_sqrt(p) or (p.is_Pow and p.exp.is_Integer)):
  return is_algebraic(p.base)
 elif (p.is_Add or p.is_Mul):
  return all((is_algebraic(x) for x in p.args))
 else:
  return False

def is_algebraic ( p ) : if p . is_Rational : return True elif p . is_Atom : return False elif ( is_sqrt ( p ) or ( p . is_Pow and p . exp . is_Integer ) ) : return is_algebraic ( p . base ) elif ( p . is_Add or p . is_Mul ) : return all ( ( is_algebraic ( x ) for x in p . args ) ) else : return False
return true if p is comprised of only rationals or square roots of rationals and algebraic operations .