Trailing zeros of 100!

Tim Chase python.list at tim.thechases.com
Sat Jan 2 11:33:01 EST 2016


On 2016-01-02 03:49, katye2007 at gmail.com wrote:
> I'm trying to write a python program to find how many trailing
> zeros are in 100! (factorial of 100). I used factorial from the
> math module, but my efforts to continue failed. Please help.

Pretty easy to do with strings:

  from math import factorial
  n = factorial(100)
  s = str(n)
  print(len(s) - len(s.rstrip('0')))

or count them:

  from itertools import takewhile
  sum(1 for _ in takewhile(lambda c: c == '0', reversed(s)))

or mathematically:

  sum(1 for _ in itertools.takewhile(
    lambda x: n % (10**x) == 0,
    itertools.count(1)))

-tkc







More information about the Python-list mailing list