Why does this function give me correct values up to a certain range of inputs only? [duplicate]

Solution 1:

Although formally the behaviour on overflowing an int is undefined, modulo arithmetic on a number that's a power of 2 is a common treatment.

A number like 50! has as its many factors a large power of 2. This accounts for the zeros you are getting as output.

The solution is to use an arbitrary precision integer library. There's a good one in Boost.