Finding direct product decompositions in polynomial time