Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Tim Chase Newsgroups: comp.lang.python Subject: Re: Trailing zeros of 100! Date: Sat, 2 Jan 2016 10:33:01 -0600 Lines: 28 Message-ID: References: <52ccbc4b-616b-4186-8802-97aaa5b0d9af@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 8nbFMpt6YRRqCjGw97M7Mw3z8dMV0qZxSefmk8skkLVg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'trailing': 0.07; "'0',": 0.09; 'zeros': 0.09; 'python': 0.10; '-tkc': 0.16; 'failed.': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'itertools': 0.16; 'lambda': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'module,': 0.18; 'math': 0.20; 'trying': 0.22; 'help.': 0.23; 'import': 0.24; 'header:In- Reply-To:1': 0.24; "i'm": 0.30; 'received:184': 0.30; 'but': 0.36; 'skip:i 20': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'charset:us-ascii': 0.37; 'to:addr:python.org': 0.40; 'easy': 0.60; 'email addr:gmail.com': 0.62 X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-MC-Relay: Neutral X-MailChannels-SenderId: wwwh|x-authuser|tim@thechases.com X-MailChannels-Auth-Id: wwwh X-MC-Loop-Signature: 1451752510046:728422456 X-MC-Ingress-Time: 1451752510046 In-Reply-To: <52ccbc4b-616b-4186-8802-97aaa5b0d9af@googlegroups.com> X-Mailer: Claws Mail 3.11.1 (GTK+ 2.24.25; x86_64-pc-linux-gnu) X-AuthUser: tim@thechases.com 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:101159 On 2016-01-02 03:49, katye2007@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