For the problem Small Factorials I wrote the following C Code. It is working on my PC. I crosschecked the answer for 100! with a Python Script so answer is correct. I don't have any idea why this is giving wrong answer. I am using an integer array to store the digits. 4 digits at each array index. Lowest 4 digits at ans[0], next 4 at ans1 and so on. What's wrong here?
asked 24 Jun '13, 19:09

Good one, your code returns for 67 364711109181886852882498590966054644271676353140495245937016284100267962436943872000000000000000 while correct answer is 36471110918188685288249859096605464427167635314049524593701628500267962436943872000000000000000 That's the only mistake for all 100 factorials possible as input... answered 24 Jun '13, 19:45
How did you even manage to find that out?
(24 Jun '13, 20:10)
There are only 100 possibilities, so I tried all of them ;)
(24 Jun '13, 20:13)
By hand or scripting? How? It was frustrating enough to crosscheck the answer of 100! by hand (even when I had the actual answer)
(24 Jun '13, 20:15)
1
I generated test case to get result for all, then I used your code to generate results (http://ideone.com/53vqA0), mine (http://ideone.com/d6loVk) and compared with diff (http://en.wikipedia.org/wiki/Diff) ;) Of course I first tried some "special" values...
(24 Jun '13, 20:26)
Thanks. Now I am left scratching my head looking at the code and thinking where it went wrong. ;)
(24 Jun '13, 20:38)
Found it. Got AC :)
(24 Jun '13, 20:51)
temp = ans[j] * i; ans[j] = temp % LIMIT; ans[j + 1] += temp/LIMIT; After the last line ans[j+1] could become more than LIMIT, which will result in WA
(30 Jul '13, 22:30)
showing 5 of 7
show all

@vjozic...factorials achieve large values...double can store these values...BUT there is a loss of precision...so u need to use an array of integers to store such high numbers...u can store few digits of the factorial in each cell like....
it is like making your own datatype....hope this helps...:) answered 30 Jul '13, 19:05
