I've managed to confuse myself. I think stage 8 should be easy, but I've come up with two totally different methods (one mathematical, one brute force) for it which come up with the same wrong answer.
Incidentally, the same right answers when I check them for other (although far smaller) values.
I should probably go to bed.
I'm not sure if you've ever played This game but it might help you out.