Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

last-non-zero-factorial-digit

By: a guest on Apr 5th, 2013  |  syntax: Python  |  size: 0.53 KB  |  hits: 120  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. def lznd_trailing_zeros (n):
  2.     "Return the number of trailing zeros of N!"
  3.     r = 0
  4.     for i in range(5, n+1, 5):
  5.         while i%5 == 0:
  6.             r += 1
  7.             i = i / 5
  8.     return r
  9.  
  10. def lznd_fact (n):
  11.     "Return the last non-zero digit of N!"
  12.     f2 = f5 = lznd_trailing_zeros(n)
  13.     r = 1
  14.     for i in range(1,n+1):
  15.         while f2 > 0 and i%2 == 0:
  16.             f2 -= 1
  17.             i /= 2
  18.         while f5 > 0 and i%5 == 0:
  19.             f5 -= 1
  20.             i /= 5
  21.         r = (i*r) % 10
  22.     return r