Not off the top of my head, but two strategies do occur to me. Though actually, they're really variations of the same general idea...
I think that giving you a direct answer would defeat the point of the exercise.
Instead, you should work a few samples (with pencil and paper) and see what steps you have to take. That should allow you to identify the required algorithm. Then it's "just" a matter ot turning into C++.
By the way: the two variations of my strategy are (a) work out everything need you do first and then do the final calculation, or (b) to the calculation as you go along.
Andy
P.S. Have you already done a "finding prime numbers" exercise?