A quick solution I came out with, no stirling numbers and had tried to avoid large integer multiplication as much as possible.<br><br>import math<br><br>for i in range(int(raw_input())):<br>    n, k, l = [int(i) for i in raw_input().split()]<br>

    e = sum(math.log10(i) for i in range(1, n+1))<br>    frac_e = e - math.floor(e)<br>    a = str(10**frac_e * 10**(k - 1)).split('.')[0]<br>    <br>    b  = 1<br>    for i in range(n, 0, -1):<br>        b = (b * i) % 10**l<br>

    lb = len(str(b))<br>    if lb < l:<br>        b = str(b) + '0'* (l - lb)<br>    <br>    print a, b<br><br>~l0nwlf<br><br><div class="gmail_quote">On Sat, Feb 20, 2010 at 6:14 PM, Mark Dickinson <span dir="ltr"><<a href="mailto:dickinsm@gmail.com">dickinsm@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">On Feb 20, 11:17 am, mukesh tiwari <<a href="mailto:mukeshtiwari.ii...@gmail.com">mukeshtiwari.ii...@gmail.com</a>><br>


wrote:<br>
<div class="im">> Hello everyone. I think it is  related to the precision with double<br>
> arithmetic so i posted here.I am trying with this problem (<a href="https://www.spoj.pl/problems/CALCULAT" target="_blank">https://www.spoj.pl/problems/CALCULAT</a>) and the problem say that "Note : for<br>
> all test cases whose N>=100, its K<=15." I know precision of doubles<br>
> in c is 16 digits. Could some one please help me with this precision<br>
> issue.I used stirling (<a href="http://en.wikipedia.org/wiki/" target="_blank">http://en.wikipedia.org/wiki/</a><br>
> Stirling's_approximation) to calculate the first k digits of N.<br>
> Thank you.<br>
<br>
</div>If I understand you correctly, you're trying to compute the first k<br>
digits in the decimal expansion of N!, with bounds of k <= 15 and 100<br>
<= N < 10**8.  Is that right?<br>
<br>
Python's floats simply don't give you enough precision for this:<br>
you'd need to find the fractional part of log10(N!) with >= 15 digits<br>
of precision.  But the integral part needs ~ log10(log10(N!)) digits,<br>
so you end up needing to compute log10(N!) with at least 15 +<br>
log10(log10(N!)) ~ 15 + log10(N) + log10(log10(N)) digits (plus a few<br>
extra digits for safety);  so that's at least 25 digits needed for N<br>
close to 10**8.<br>
<br>
The decimal module would get you the results you need (are you allowed<br>
imports?).  Or you could roll your own log implementation based on<br>
integer arithmetic.<br>
<br>
--<br>
<font color="#888888">Mark<br>
</font><div><div></div><div class="h5">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br>