Advertisement
Guest User

last-non-zero-factorial-digit

a guest
Apr 5th, 2013
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.53 KB | None | 0 0
  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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement