Let me start off by clarifying that(before you guys dismiss me), this is not a homework problem and I\'m not a university student. :)
EDIT Thanks to
This is a mathematical question rather than a programming one. Once you have a suitable algorithm, impelementing it shouldn't be too hard.
I suggest you google on Diophantine Equations.
I found an explanation for you.