Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: srinivas devaki Newsgroups: comp.lang.python Subject: Re: Trailing zeros of 100! Date: Sun, 3 Jan 2016 07:16:43 +0530 Lines: 103 Message-ID: References: <52ccbc4b-616b-4186-8802-97aaa5b0d9af@googlegroups.com> <85twmvqzrv.fsf@benfinney.id.au> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de EUVthQQK0E7J7to9Qd23vQn2WOW0Tiu2xPPlbtjRzbTg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'newbie': 0.05; '__name__': 0.07; 'trailing': 0.07; 'cc:addr:python-list': 0.09; 'answering': 0.09; 'survive': 0.09; 'work"': 0.09; 'zeros': 0.09; 'python': 0.10; 'jan': 0.11; 'def': 0.13; 'mathematics': 0.15; '"should': 0.16; "'__main__':": 0.16; '1):': 0.16; '2016': 0.16; '24,': 0.16; 'assumptions': 0.16; 'count,': 0.16; "didn't,": 0.16; 'failed.': 0.16; 'loops': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sure.': 0.16; 'test".': 0.16; '{3:': 0.16; 'wrote:': 0.16; 'implementing': 0.18; 'module,': 0.18; '>>>': 0.20; 'math': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'trying': 0.22; 'cc:no real name:2**0': 0.22; 'am,': 0.23; 'help.': 0.23; 'import': 0.24; 'url:view': 0.24; 'header:In-Reply-To:1': 0.24; 'testing': 0.25; 'chris': 0.26; 'message-id:@mail.gmail.com': 0.27; '14,': 0.27; 'mathematical': 0.27; 'this.': 0.28; 'actual': 0.28; '(maybe': 0.29; 'built.': 0.29; 'wasting': 0.29; 'program,': 0.29; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.30; 'normally': 0.30; 'ideal': 0.32; 'run': 0.33; 'url:python': 0.33; 'mass': 0.33; 'url:listinfo': 0.34; 'worked': 0.34; 'running': 0.34; 'that,': 0.34; 'received:google.com': 0.35; 'so,': 0.35; 'attempt': 0.35; 'court': 0.35; 'but': 0.36; 'should': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'expect': 0.37; 'received:209.85.213': 0.37; 'thought': 0.37; 'received:209': 0.38; 'log': 0.38; 'thank': 0.38; 'hi,': 0.38; 'end': 0.39; 'means': 0.39; 'why': 0.39; 'test': 0.39; 'does': 0.39; "didn't": 0.39; 'url:mail': 0.40; 'protection': 0.60; 'your': 0.60; 'real': 0.62; 'further': 0.62; 'course': 0.62; 'our': 0.64; 'trusted': 0.64; 'judge': 0.66; 'internet': 0.70; 'computers': 0.70; 'sound': 0.72; 'url:jsp': 0.72; 'physical': 0.72; '"yes,': 0.84; '_o__)': 0.84; 'angelico:': 0.84; 'deserves': 0.84; 'governmental': 0.84; 'here!': 0.84; 'hypothesis': 0.84; 'limitations.': 0.84; 'speech': 0.84; 'approach.': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=3+6KD9xK883p3epQucK22Z2QtxNAXXjc9AI67Fgqmx8=; b=ooTDS3slaOfC2qwDLjWubRLLQPD++FToqcL9zDJD/CpGZpF3ppPUIGDuq8v171Zn5A P8VtY41bjv6cDKC6Y7SCF2Pc1Q0YGyBJzRWo2CqnpFMBFRe9hZbV7CJZEIKbfZ3LJ2/I ueTtJ93EaSBoLuF5/qkufCWwudE9x6jc7tuhXOZCpH/2Vvxcz3iLiHJyPOFI+0zoCZUb i83fR6/XPbyhld2LfX3T605Mf4Ex0hKuNXyN4ZAHIBBRaTalISniDWBsYXEKjqFCgf1e xtue0n2QbSiQO6sn5wmSdD1CxRXO+tE0OV3QcgWEvD38UtbpIKEq8170eFPfjNUUoSW8 SFlA== X-Received: by 10.50.73.9 with SMTP id h9mr68920592igv.41.1451785643228; Sat, 02 Jan 2016 17:47:23 -0800 (PST) In-Reply-To: <85twmvqzrv.fsf@benfinney.id.au> 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:101183 let's put an end to this. from math import log # simple one to understand. complexity: O(n*log(n)) def countzeros_va(n): count =3D 0 for x in xrange(1, n + 1): while x % 5 =3D=3D 0: count +=3D 1 x //=3D 5 return count # better approach. complexity: O(log(n)) def countzeros_vb(n): count, c5 =3D 0, 5 while c5 <=3D n: count +=3D (n // c5) c5 *=3D 5 return count # this is same as before, its just that while loops irk me def countzeros_vc(n): return sum(n // (5**x) for x in range(1, int(log(n, 5) + 3))) # adding +3 to be sure. never trust approximations. def run_sample_tests(): precal =3D {3: 0, 60: 14, 100: 24, 1024: 253, 23456: 5861, 8735373: 218= 3837} for x in precal: assert precal[x] =3D=3D countzeros_va(x) =3D=3D countzeros_vb(x) = =3D=3D countzeros_vc(x) if __name__ =3D=3D '__main__': run_sample_tests() Although the code is deterministic, it can be further tested from http://www.wolframalpha.com/widgets/view.jsp?id=3D54da27e6e09dc404890a57873= 5b9f7d8 http://www.spoj.com/problems/FCTRL/ On Jan 2, 2016 5:22 PM, wrote: > > 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 On Sun, Jan 3, 2016 at 5:50 AM, Ben Finney wro= te: > Robin Koch writes: > >> Am 02.01.2016 um 22:57 schrieb Chris Angelico: >> >>> But did you actually test it? >> >> >> >> Yes, should work for n >=3D 1. > > By =E2=80=9Ctest it=E2=80=9D, Chris of course means test it *by implement= ing it in a > program and running that program in Python*. > >> >> Why do you ask? >> > >> > Your "should work" does not sound good as a response to "actually >> > test". Normally I would expect the response to be "Yes, and it >> > worked for me" (maybe with a log of an interactive session). >> >> Well, honestly, I trusted my math and didn't thought much about the >> technical limitations. > > That's why it's good to actually test the hypothesis in a real computer > program, run on the actual computer system you're going to use. > > Computers are physical systems, with technical compromises to the > physical constraints under which they were built. > > They are not perfect implementations of our ideal mathematics, and > testing the mathematics is no guarantee the mathematical assumptions > will survive your program unscathed. > > So, a request =E2=80=9CDid you actually test it?=E2=80=9D is both a polit= e reminder to > do that, and an attempt to get you to do so if you didn't. > > If you didn't, then answering =E2=80=9Cyes=E2=80=9D is wasting everyone's= time. > > -- > \ =E2=80=9CAs the most participatory form of mass speech yet devel= oped, | > `\ the Internet deserves the highest protection from governmental | > _o__) intrusion.=E2=80=9D =E2=80=94U.S. District Court = Judge Dalzell | > Ben Finney > > -- > https://mail.python.org/mailman/listinfo/python-list