Hard Pre Calculus Math Problem