w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Big number factorial modulo big prime number

This is because your implementation uses recursion. For small numbers it works fine, but for large numbers it overflows the stack.

This line

if(nr)return modulo(nr * fact(nr - 1));

creates nr stack frames. Since the stack space is very limited, entering a large number causes stack overflows.

Change your implementation to use iterative calculation of the factorial to avoid the crash.

Once you are done fixing the crash, deal with the numeric overflow. Rather than computing the modulo after the factorial has been calculated, keep applying modulo at each step of the calculation.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.