How large? Even long long data types have limits. Whenever you see this sort of problem, usually the issue is overflow. There is probably a mathematical solution that they want you to discover. Off the top of my head, given k = 15746, if pre%k = 5 and cur %k = 11, pre+cur%k will be 16. Does it matter how large pre and cur are, given the resulting modulus? So why are you bothering to store that info? What would happen if you set fib = (pre+cur)%k? I think that's likely your solution, but if it's not I expect it's something along those lines.