Trailing zeros of 100!

yehudak . katye2007 at gmail.com
Sat Jan 2 12:34:19 EST 2016


vbr,
I tried using .pop() but could not get what I wanted .Also, I can't see an
advantage in reversing the number.
Would you care to write explicitly the program for me (and probably for
other too)?
Brute Force is the style I'm thinking about.

Sorry, but I learn most from viewing the code.

Appreciated,
Yehuda

On Sat, Jan 2, 2016 at 5:24 PM, Vlastimil Brom <vlastimil.brom at gmail.com>
wrote:

> 2016-01-02 14:14 GMT+01:00 yehudak . <katye2007 at gmail.com>:
> > Vlastimil,
> > Thank you so much, but...
> > All that is Chinese for me.
> > Can you show a 'normal' Python code for me?
> >
> > Yehuda
> >
> > On Sat, Jan 2, 2016 at 2:44 PM, Vlastimil Brom <vlastimil.brom at gmail.com
> >
> > wrote:
> >>
> >> 2016-01-02 12:49 GMT+01:00  <katye2007 at gmail.com>:
> >> > Hi, newbie here!
> >> > 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.
> >> >
> >> > Thank you,
> >> > Yehuda
> >> > --
> >> > https://mail.python.org/mailman/listinfo/python-list
> >>
> >> Hi,
> >> rather an illustration of the available tools in python, than a
> >> (submittable) solution:
> >>
> >> >>> import re, math
> >> >>> len(re.search(r"0*$", str(math.factorial(100))).group())
> >> 24
> >> [or the same code on more lines with some indentation - if it is
> >> preserved via e-mail]
> >> >>> len(
> >> ...     re.search(
> >> ...         r"0*$",
> >> ...         str(
> >> ...             math.factorial(100)
> >> ...             )
> >> ...         ).group()
> >> ...     )
> >> 24
> >> >>>
> >>
> >> I.e. You need the length of the string resulting as the match of the
> >> regular expression search for a pattern representing zero or more "0"
> >> at the end of the input text, which is the string version of 100!
> >>
> >> Of course, there are other ways to get this result :-)
> >>
> >> regards,
> >>     vbr
> >
> >
> Hi,
> If you eventually have this as an assignment or other kind of
> (self)learning task, you would want to approach this with the methods
> you know, or are supposed to use.
> For the math context, you may find this explanations useful:
> http://www.purplemath.com/modules/factzero.htm
> a rather straightforward python implementation of this seems to be
> e.g. this recipe:
>
> http://code.activestate.com/recipes/577844-calculate-trailing-zeroes-in-a-factorial/
> Note, that you don't need to calculate the value of the factorial
> itself using this way.
> If you have problems with following or understanding the code, you may
> show your own attempts and tell what problems you encounter with your
> approach.
>
> My previous code sample is based on another - "brute-force" approach,
> the factorial is calculated (e.g. via the math module as you have
> found), then the integer is converted to a string, afterwards the part
> of the result consisting only of zeros - at the end of the string is
> matched with a regular expression and finally the length of it is
> determined.
>
> Regular expressions might be handy, but are not necesarilly elementary
> stuff for a newcomer in python programming.
> You can count the trailing zeros in other ways too - as was suggested
> - you can reverse the string and count from the beginning then,
> stopping before the first non-zero digit.
> The most straightforward way could be to loop (characterwise) through
> the (reversed) string, check each character whether it equals to "0"
> and stop as soon as there is another digit.
>
> hth,
>    vbr
>



More information about the Python-list mailing list