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


Groups > comp.lang.python > #101183

Re: Trailing zeros of 100!

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From srinivas devaki <mr.eightnoteight@gmail.com>
Newsgroups comp.lang.python
Subject Re: Trailing zeros of 100!
Date Sun, 3 Jan 2016 07:16:43 +0530
Lines 103
Message-ID <mailman.189.1451785646.11925.python-list@python.org> (permalink)
References <52ccbc4b-616b-4186-8802-97aaa5b0d9af@googlegroups.com> <CAHzaPEOzP+DZDHOV7s1tDA0AUNhFdBUJKZWCJ5W9F8ec6aH-JA@mail.gmail.com> <mailman.162.1451745109.11925.python-list@python.org> <n68s1t$ke9$1@news.albasani.net> <mailman.172.1451753180.11925.python-list@python.org> <n68vgc$qi6$1@news.albasani.net> <mailman.184.1451771860.11925.python-list@python.org> <n69oev$uv8$1@news.albasani.net> <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 <mr.eightnoteight@gmail.com>
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 <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:101183

Show key headers only | View raw


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 = 0
    for x in xrange(1, n + 1):
        while x % 5 == 0:
            count += 1
            x //= 5
    return count

# better approach. complexity: O(log(n))
def countzeros_vb(n):
    count, c5 = 0, 5
    while c5 <= n:
        count += (n // c5)
        c5 *= 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 = {3: 0, 60: 14, 100: 24, 1024: 253, 23456: 5861, 8735373: 2183837}
    for x in precal:
        assert precal[x] == countzeros_va(x) == countzeros_vb(x) ==
countzeros_vc(x)

if __name__ == '__main__':
    run_sample_tests()

Although the code is deterministic, it can be further tested from
http://www.wolframalpha.com/widgets/view.jsp?id=54da27e6e09dc404890a578735b9f7d8
http://www.spoj.com/problems/FCTRL/

On Jan 2, 2016 5:22 PM, <katye2007@gmail.com> 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 <ben+python@benfinney.id.au> wrote:
> Robin Koch <robin.koch@t-online.de> writes:
>
>> Am 02.01.2016 um 22:57 schrieb Chris Angelico:
>> >>> But did you actually test it?
>> >>
>> >> Yes, should work for n >= 1.
>
> By “test it”, Chris of course means test it *by implementing 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 “Did you actually test it?” is both a polite reminder to
> do that, and an attempt to get you to do so if you didn't.
>
> If you didn't, then answering “yes” is wasting everyone's time.
>
> --
>  \       “As the most participatory form of mass speech yet developed, |
>   `\    the Internet deserves the highest protection from governmental |
> _o__)                   intrusion.” —U.S. District Court Judge Dalzell |
> Ben Finney
>
> --
> https://mail.python.org/mailman/listinfo/python-list

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