Check out the Pastebin Gadgets Shop. We have thousands of fun, geeky & affordable gadgets on sale :-)Want more features on Pastebin? Sign Up, it's FREE!
tweet

# last-non-zero-factorial-digit

By: a guest on Apr 5th, 2013  |  syntax: Python  |  size: 0.53 KB  |  views: 171  |  expires: Never
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
clone this paste RAW Paste Data
Top