A crude way of doing it is to keep going until the result is less than what you got before. When I tried it on a 32-bit signed integer, I got that:
Code:
i= 1 i!= 1
i= 2 i!= 2
i= 3 i!= 6
i= 4 i!= 24
i= 5 i!= 120
i= 6 i!= 720
i= 7 i!= 5040
i= 8 i!= 40320
i= 9 i!= 362880
i= 10 i!= 3628800
i= 11 i!= 39916800
i= 12 i!= 479001600
i= 13 i!= 1932053504
i= 14 i!= 1278945280
i= 15 i!= 2004310016
i= 16 i!= 2004189184
i= 17 i!= -288522240
Now you might think that the train went off the rails with 17! because it's negative. But actually the problem is with 14!, for it is less than 13!.