Digital Calculator: spoj.pl/problems/CALCULAT/
I am trying to solve it with C, and I am having wrong answer for some of the input. The error is in calculating first K digits. For example,
Input: 93731344 9 31
gives: 379453425 0000000000000000000000000000000
instead of: 379453458 0000000000000000000000000000000
I am using Stirling approximation formula for log10(n!):
[bbone=text,236]
stir = (n * logl(n) - n + 0.5 * logl(2 * PI * n)) * log10l(E);
x = modfl(stir, &integ);
result = powl(10, x + K - 1);
printf("%.0Lf", floorl(result));
[/bbone]
(all variables are long double)
Also,
long double PI=3.14159265358979323846264338327950288419716939937510
long double E = 2.71828182845904523536028747135266249775724709369995
Should I use some other form of Stirling formula? I also tried Python 2.7 with the same expressions, it gives different result but wrong again. Thanks in advance.