Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: "yehudak ." Newsgroups: comp.lang.python Subject: Re: Trailing zeros of 100! Date: Sat, 2 Jan 2016 22:02:17 +0200 Lines: 131 Message-ID: References: <52ccbc4b-616b-4186-8802-97aaa5b0d9af@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de Pneue3pGctr8pzu4KY2+BgNVDj93DdneVuKqE41ZLQrg== Return-Path: 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; '>>>': 0.15; 'explicitly': 0.15; 'instead.': 0.15; 'intermediate': 0.15; 'skip:f 30': 0.15; '"0"': 0.16; '->': 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; 'string': 0.17; 'integer': 0.18; 'module,': 0.18; 'removed.': 0.18; 'string,': 0.18; '>': 0.18; 'email addr:gmail.com>': 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; '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; 'hi,': 0.38; 'end': 0.39; 'google': 0.39; 'reach': 0.61; '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=fep86mkNlKr6jgEZudRE7eIOzQGvvK1Hc/wHt1z9Qrc=; b=rXu9LXPfjvbM3Tv23oPCQNW2b2ZdDmgwbSRcPXutRfddQtGG6tyl+cToj32smWfjlb WW1aXt3kKRR81X0dguqDuLF9+Vl5cYBOIRgjOjscP366WLI8/2xQ4yN++iak5YpGgQoL nuonIzkQF8g2IfLQoNlBD57q3icKrC1Jqj0wGv6phCTl4cgXeayjWNX0a3e7Wx9G06V7 hN8NQ90vJExyc6JqSuLwfO6vFaW9utarqDKB5cF/nO2rjtWuxJIKOQ6nijZFATxyopAZ 9S4eu1DD2VMwMbJch/8rKkuZvCVuJ5RJlx9WrAqkJi7YmmLoR4AT4nUx+SN1XxBpbBOo 4EVw== X-Received: by 10.140.237.74 with SMTP id i71mr48386615qhc.55.1451764937405; Sat, 02 Jan 2016 12:02:17 -0800 (PST) In-Reply-To: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:101169 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 wrote: > 2016-01-02 18:34 GMT+01:00 yehudak . : > [partly edited for bottom posting] > > On Sat, Jan 2, 2016 at 5:24 PM, Vlastimil Brom > > > wrote: > >> > >> 2016-01-02 14:14 GMT+01:00 yehudak . : > >> > > [...]>> > On Sat, Jan 2, 2016 at 2:44 PM, Vlastimil Brom > >> > > >> > wrote: > >> >> > >> >> 2016-01-02 12:49 GMT+01:00 : > >> >> > 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 >