Guest User

last-non-zero-factorial-digit

a guest
Apr 5th, 2013
353
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×