Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101152

Re: Trailing zeros of 100!

From Vlastimil Brom <vlastimil.brom@gmail.com>
Newsgroups comp.lang.python
Subject Re: Trailing zeros of 100!
Date 2016-01-02 16:24 +0100
Message-ID <mailman.168.1451748283.11925.python-list@python.org> (permalink)
References <52ccbc4b-616b-4186-8802-97aaa5b0d9af@googlegroups.com> <CAHzaPEOzP+DZDHOV7s1tDA0AUNhFdBUJKZWCJ5W9F8ec6aH-JA@mail.gmail.com> <CAE3ie42VkmUKOMzp18aHntL26hon6spNOXxJKQQGUsB9XuV2-g@mail.gmail.com>

Show all headers | View raw


2016-01-02 14:14 GMT+01:00 yehudak . <katye2007@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@gmail.com>
> wrote:
>>
>> 2016-01-02 12:49 GMT+01:00  <katye2007@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Trailing zeros of 100! katye2007@gmail.com - 2016-01-02 03:49 -0800
  Re: Trailing zeros of 100! David <bouncingcats@gmail.com> - 2016-01-02 23:19 +1100
  Re: Trailing zeros of 100! katye2007@gmail.com - 2016-01-02 04:34 -0800
  Re: Trailing zeros of 100! Vlastimil Brom <vlastimil.brom@gmail.com> - 2016-01-02 13:44 +0100
  Re: Trailing zeros of 100! "yehudak ." <katye2007@gmail.com> - 2016-01-02 15:14 +0200
    Re: Trailing zeros of 100! Robin Koch <robin.koch@t-online.de> - 2016-01-02 16:57 +0100
      Re: Trailing zeros of 100! Tony van der Hoff <tony@vanderhoff.org> - 2016-01-02 17:09 +0100
        Re: Trailing zeros of 100! Robin Koch <robin.koch@t-online.de> - 2016-01-02 17:56 +0100
          Re: Trailing zeros of 100! Bernardo Sulzbach <mafagafogigante@gmail.com> - 2016-01-02 15:24 -0200
          Re: Trailing zeros of 100! Chris Angelico <rosuav@gmail.com> - 2016-01-03 08:57 +1100
            Re: Trailing zeros of 100! Robin Koch <robin.koch@t-online.de> - 2016-01-03 01:02 +0100
              Re: Trailing zeros of 100! Ben Finney <ben+python@benfinney.id.au> - 2016-01-03 11:20 +1100
              Re: Trailing zeros of 100! srinivas devaki <mr.eightnoteight@gmail.com> - 2016-01-03 07:16 +0530
          Re: Trailing zeros of 100! Tony van der Hoff <tony@vanderhoff.org> - 2016-01-03 11:53 +0100
  Re: Trailing zeros of 100! Joel Goldstick <joel.goldstick@gmail.com> - 2016-01-02 09:57 -0500
  Re: Trailing zeros of 100! Vlastimil Brom <vlastimil.brom@gmail.com> - 2016-01-02 16:24 +0100
  Re: Trailing zeros of 100! Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-01-02 15:30 +0000
  Re: Trailing zeros of 100! Robin Koch <robin.koch@t-online.de> - 2016-01-02 16:54 +0100
    Re: Trailing zeros of 100! Peter Otten <__peter__@web.de> - 2016-01-02 18:18 +0100
  Re: Trailing zeros of 100! Tim Chase <python.list@tim.thechases.com> - 2016-01-02 10:33 -0600
  Re: Trailing zeros of 100! "yehudak ." <katye2007@gmail.com> - 2016-01-02 19:34 +0200
  Re: Trailing zeros of 100! Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-01-02 17:44 +0000
    Re: Trailing zeros of 100! Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-03 00:36 +0100
  Re: Trailing zeros of 100! Serhiy Storchaka <storchaka@gmail.com> - 2016-01-02 20:28 +0200
  Re: Trailing zeros of 100! Vlastimil Brom <vlastimil.brom@gmail.com> - 2016-01-02 19:29 +0100
  Re: Trailing zeros of 100! "yehudak ." <katye2007@gmail.com> - 2016-01-02 22:02 +0200
  Re: Trailing zeros of 100! "yehudak ." <katye2007@gmail.com> - 2016-01-02 22:09 +0200
  Re: Trailing zeros of 100! Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-01-02 21:25 +0000
  Re: Trailing zeros of 100! Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-01-02 21:26 +0000

csiph-web