If p = price, q = quantity, and TC = total cost, then:
P = Profit = Revenue - Cost
= (price)(quantity) - cost
= (184-4q)(q) - (q3-21q2+160q+40)
= -q3 + 17q2 +24q - 40
P' = -3q2 + 34q + 24
= (-3q - 2)(q - 12) = 0 when q = -2/3 or q = 12
Since q can't be negative, q = 12.
When 0<q<12, P'>0. So P is increasing.
When q>12, P'<0. So P is decreasing
Therefore, profit is maximized when q = 12.