calculating factorial of large numbers in 0.5 second with python
<p>I've been trying to find a super fast code that can calculate the factorial of a big number like 70000 in 0.5 second,My own code could do it in 10 seconds.I've searched everywhere, every code I find has memory error problem or is not as fast as I want. Can anyone help me with this?</p>

<pre><code>enter code here
import math
num =int(raw_input())
usefrm=0
if len(str(num)) &gt; 2:
  if int(str(num)[-2]) % 2 == 0:
        usefrm = 'even'
  else:
        usefrm = 'odd'
else:
  if num % 2 == 0:
        usefrm = 'even1'
  else:
        usefrm = 'odd1'

def picknumber(num):
  s = str(math.factorial(num))
  l = []
  for n in s:
        if int(n) != 0:
              l.append(int(n))
  return l[-1]

  def picknumber1(num):
  s = str(num)
  l = []
  for n in s:
        if int(n) != 0:
              l.append(int(n))
  return l[-1]
  if usefrm == 'even':
     e=picknumber1(6*picknumber(int(num/5))*picknumber(int(str(num)[-1])))
  if usefrm == 'odd':
     e=picknumber1(4*picknumber(int(num/5))*picknumber(int(str(num)[-1]))) 
  else:
  e=picknumber1(math.factorial(num))
  print e
</code></pre>

<p>If you don't want a perfect precision, you can use the Stirling's approximation
<a href="https://en.wikipedia.org/wiki/Stirling&#39;s_approximation" rel="nofollow">https://en.wikipedia.org/wiki/Stirling's_approximation</a></p>

<pre><code>import np
n! ~ np.sqrt(2*np.pi*n)*(n/np.e)**n
</code></pre>

<p>for large n values. This calculation is literally instantaneous.</p>

python factorial