Ooh, a fun one! I'm probably answering this long after it was useful in your class, but I'll write an answer for posterity, maybe.
Ok, here's the logic of the solution: For a given n we want to know its smallest prime factor, so that we can accumulate (p1-1)^(e_1-1) into an accumulator variable (which is initialized at 1 so that it can accumulate via multiplication). Now that we have used up the factor p1 in n, we want to "throw it away and move to the next". Therefore we divide n by p_1^e_1 to eliminate all factors of p_1 from n.
Repeat until the variable tracking what remains of n becomes 1.