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


Groups > comp.lang.python > #101170

Re: Trailing zeros of 100!

Path csiph.com!feeder.erje.net!1.eu.feeder.erje.net!fu-berlin.de!uni-berlin.de!not-for-mail
From "yehudak ." <katye2007@gmail.com>
Newsgroups comp.lang.python
Subject Re: Trailing zeros of 100!
Date Sat, 2 Jan 2016 22:09:04 +0200
Lines 140
Message-ID <mailman.181.1451768963.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> <CAHzaPEPouiP_RiVfgur4YVY1JnbqJy9y+cJji6HqU+omkFHoQA@mail.gmail.com> <CAE3ie40jA1GapR_2yWyedAQLqkaR2bQFmEt3Oppnk_HXFd2FxQ@mail.gmail.com> <CAHzaPENEc8cVWse2SvrcLnq6Nm2bkdYWJCTAHySCkkV1GswevA@mail.gmail.com> <CAE3ie41KG8F7nB0Q2=EEff7+r8gjLp3vkpd=7r0y7yW3-13H9Q@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de +JXclw/5ClBN+c+o4zajZQICfgE2ewGtOKOrsIsxiGig==
Return-Path <katye2007@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'string.': 0.04; 'newbie': 0.05; 'none,': 0.05; 'assignment': 0.07; 'counting': 0.07; 'expressions': 0.07; 'trailing': 0.07; 'cc:addr:python-list': 0.09; 'interpreter,': 0.09; 'matched': 0.09; 'zeros': 0.09; 'python': 0.10; 'jan': 0.11; '&gt;&gt;&gt;': 0.15; 'explicitly': 0.15; 'instead.': 0.15; 'intermediate': 0.15; 'skip:f 30': 0.15; '"0"': 0.16; '-&gt;': 0.16; '2016': 0.16; 'afterwards': 0.16; 'algorithmic': 0.16; 'cc:name:python': 0.16; 'earlier.': 0.16; 'failed.': 0.16; 'looping': 0.16; 'pop()': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'reversed': 0.16; 'reversing': 0.16; 'wrote:': 0.16; 'looked': 0.16; 'string': 0.17; 'integer': 0.18; 'module,': 0.18; 'removed.': 0.18; 'string,': 0.18; '&gt;': 0.18; 'email addr:gmail.com&gt;': 0.18; '>>>': 0.20; 'math': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'suggested': 0.20; 'posted': 0.21; '(the': 0.22; 'converted': 0.22; 'default,': 0.22; 'noted': 0.22; 'sorry,': 0.22; 'stopping': 0.22; 'trying': 0.22; 'code,': 0.23; 'code.': 0.23; 'bit': 0.23; 'help.': 0.23; 'seems': 0.23; '(you': 0.23; 'needed.': 0.23; 'sat,': 0.23; 'skip:l 40': 0.23; 'this:': 0.23; 'tried': 0.24; 'import': 0.24; 'tim': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; '(e.g.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'converting': 0.27; 'regular': 0.29; 'about.': 0.29; 'calculated': 0.29; 'chase': 0.29; 'consisting': 0.29; 'context,': 0.29; 'str': 0.29; 'url:activestate': 0.29; 'whitespace': 0.29; 'character': 0.29; 'convert': 0.29; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.30; 'compared': 0.30; 'e.g.': 0.30; 'skip:[ 10': 0.31; 'supposed': 0.31; 'probably': 0.31; 'another': 0.32; "can't": 0.32; 'computing': 0.32; 'useful': 0.33; 'url:python': 0.33; 'ones,': 0.33; 'smart': 0.33; 'url:listinfo': 0.34; 'url:code': 0.34; 'previous': 0.34; 'info': 0.34; 'list': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'could': 0.35; 'programming.': 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'too': 0.36; 'should': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; '(and': 0.36; 'beginning': 0.36; "wasn't": 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'skip:& 10': 0.37; 'method': 0.37; 'wanted': 0.37; 'itself': 0.38; 'version': 0.38; 'received:209': 0.38; 'stuff': 0.38; 'skip:p 20': 0.38; 'thank': 0.38; 'received:209.85.220': 0.38; 'hi,': 0.38; 'show': 0.62; 'sample': 0.63; 'viewing': 0.64; 'soon': 0.65; 'equals': 0.66; 'reverse': 0.66; 'dr.': 0.69; 'finally': 0.70; 'approaches': 0.72; 'explanations': 0.72; 'url:htm': 0.75; 'believe,': 0.84; 'elementary': 0.84; 'here!': 0.84; 'learn.': 0.84; 'newcomer': 0.84; 'non-zero': 0.84; 'url:recipes': 0.84; 'approach.': 0.91; 'task,': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=Cmmjcbu+5zA2Na/hBQh2H0hBk54Lw09uAR7m0DYKmGY=; b=fpRfw3fr6W8F94ayQQnGaPx7gJs5bBncPb09jOWPDLq6xph/daujWGspqeO8h1GK8E oZ8k5guPM+etRhcSSXr5o1iR4WKIWMn2MJWYc3nYfmAxtKTBxMtUnI3hERGPvwvks90f bq0blmu4fIQN1YfJ9W9C0ygvn8FU7dma/hQAZsrLhnEhIjje9BaQ/EXuytGQXFLAXsu4 IF71MeHJkG8lyPdDfXrJHTI1ysz7Uknk23nXjVapcD4FJMDA0Nq/Y8xj9Ac8Ra/PwGmR szCUj5qvZ3Y7y8I9yin8zszGqUTc3JWO1jbKN1q0cyTt4/O3HcebbbYgL8TvYfdB3LZd owoQ==
X-Received by 10.55.77.206 with SMTP id a197mr104485865qkb.43.1451765344174; Sat, 02 Jan 2016 12:09:04 -0800 (PST)
In-Reply-To <CAE3ie41KG8F7nB0Q2=EEff7+r8gjLp3vkpd=7r0y7yW3-13H9Q@mail.gmail.com>
X-Mailman-Approved-At Sat, 02 Jan 2016 16:09:21 -0500
X-Content-Filtered-By Mailman/MimeDel 2.1.20+
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:101170

Show key headers only | View raw


Hi again,
I looked a little deeper at your code. Smart solution and kudos.

Yehuda

On Sat, Jan 2, 2016 at 10:02 PM, yehudak . <katye2007@gmail.com> wrote:

> Hello vbr,
> That's EXACTLY what I needed. rstrip is new for me so I'm going to Dr.
> Google to learn.
>
> On my efforts I was struggling with .pop() but wasn't very successful...
>
> Thank you so much,
> Yehuda
>
> On Sat, Jan 2, 2016 at 8:29 PM, Vlastimil Brom <vlastimil.brom@gmail.com>
> wrote:
>
>> 2016-01-02 18:34 GMT+01:00 yehudak . <katye2007@gmail.com>:
>> [partly edited for bottom posting]
>> > On Sat, Jan 2, 2016 at 5:24 PM, Vlastimil Brom <
>> vlastimil.brom@gmail.com>
>> > wrote:
>> >>
>> >> 2016-01-02 14:14 GMT+01:00 yehudak . <katye2007@gmail.com>:
>> >> >
>> [...]>> > 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,
>> >> 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
>> >
>> >
>> > 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
>> >
>> Hi,
>> reversing the string would be useful for directly looping over the
>> string (the interesting zeros would be at the beginning of the
>> reversed string.
>> If you use pop() on a list of the digits, the items are taken from the
>> end of the list by default, hence no reversing is needed.
>> What problems do you have with this route? (you will need to convert
>> from the integer result to string, then to list and use pop() and
>> count the steps until you reach a non-zero digit)
>>
>> If you need this kind of soulution (computing the factorial,
>> converting to string, counting the trailing zero digits), I believe,
>> the most easily comprehensible version was posted by Tim Chase a bit
>> earlier.
>> In the interactive interpreter, with some intermediate steps added, it
>> can look like this:
>>
>> >>> from math import factorial
>> >>> fact100_int = factorial(100)
>> >>> fact100_string = str(fact100_int)
>> >>> fact100_string_without_trailing_zeros = fact100_string.rstrip("0")
>> >>> len(fact100_string) - len(fact100_string_without_trailing_zeros)
>> 24
>> >>>
>>
>> [aditional info on the rstrip method of any string ("abcd" used for
>> illustration here): ]
>> >>> print("abcd".rstrip.__doc__)
>> S.rstrip([chars]) -> str
>>
>> Return a copy of the string S with trailing whitespace removed.
>> If chars is given and not None, remove characters in chars instead.
>> >>>
>>
>> It should be noted that the approaches which involve computing of the
>> factorial itself have much lower limits on the size compared to the
>> algorithmic ones, but for the given case both are sufficient.
>>
>> 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