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


Groups > comp.lang.python > #101139 > unrolled thread

Trailing zeros of 100!

Started bykatye2007@gmail.com
First post2016-01-02 03:49 -0800
Last post2016-01-02 21:26 +0000
Articles 20 on this page of 29 — 16 participants

Back to article view | Back to comp.lang.python


Contents

  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

Page 1 of 2  [1] 2  Next page →


#101139 — Trailing zeros of 100!

Fromkatye2007@gmail.com
Date2016-01-02 03:49 -0800
SubjectTrailing zeros of 100!
Message-ID<52ccbc4b-616b-4186-8802-97aaa5b0d9af@googlegroups.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

[toc] | [next] | [standalone]


#101140

FromDavid <bouncingcats@gmail.com>
Date2016-01-02 23:19 +1100
Message-ID<mailman.160.1451737142.11925.python-list@python.org>
In reply to#101139
On 2 January 2016 at 22:49,  <katye2007@gmail.com> wrote:
> Hi, newbie here!

Hi Yehuda

> 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.

There is a special mailing list to help newbies write code:
https://mail.python.org/mailman/listinfo/tutor

Subscribe to that list and post whatever *code you have already
written* there, and explain exactly how you want it to be better.

[toc] | [prev] | [next] | [standalone]


#101141

Fromkatye2007@gmail.com
Date2016-01-02 04:34 -0800
Message-ID<b0a3e7de-5a7c-4e90-a19c-6e025bc91a68@googlegroups.com>
In reply to#101139
On Saturday, January 2, 2016 at 1:49:47 PM UTC+2, katy...@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

Thank ou, David, for your help and kindness.

[toc] | [prev] | [next] | [standalone]


#101143

FromVlastimil Brom <vlastimil.brom@gmail.com>
Date2016-01-02 13:44 +0100
Message-ID<mailman.161.1451738675.11925.python-list@python.org>
In reply to#101139
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,
rather an illustration of the available tools in python, than a
(submittable) solution:

>>> import re, math
>>> len(re.search(r"0*$", str(math.factorial(100))).group())
24
[or the same code on more lines with some indentation - if it is
preserved via e-mail]
>>> len(
...     re.search(
...         r"0*$",
...         str(
...             math.factorial(100)
...             )
...         ).group()
...     )
24
>>>

I.e. You need the length of the string resulting as the match of the
regular expression search for a pattern representing zero or more "0"
at the end of the input text, which is the string version of 100!

Of course, there are other ways to get this result :-)

regards,
    vbr

[toc] | [prev] | [next] | [standalone]


#101145

From"yehudak ." <katye2007@gmail.com>
Date2016-01-02 15:14 +0200
Message-ID<mailman.162.1451745109.11925.python-list@python.org>
In reply to#101139
Vlastimil,
Thank you so much, but...
All that is Chinese for me.
Can you show a 'normal' Python code for me?

Yehuda

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,
> rather an illustration of the available tools in python, than a
> (submittable) solution:
>
> >>> import re, math
> >>> len(re.search(r"0*$", str(math.factorial(100))).group())
> 24
> [or the same code on more lines with some indentation - if it is
> preserved via e-mail]
> >>> len(
> ...     re.search(
> ...         r"0*$",
> ...         str(
> ...             math.factorial(100)
> ...             )
> ...         ).group()
> ...     )
> 24
> >>>
>
> I.e. You need the length of the string resulting as the match of the
> regular expression search for a pattern representing zero or more "0"
> at the end of the input text, which is the string version of 100!
>
> Of course, there are other ways to get this result :-)
>
> regards,
>     vbr
>

[toc] | [prev] | [next] | [standalone]


#101157

FromRobin Koch <robin.koch@t-online.de>
Date2016-01-02 16:57 +0100
Message-ID<n68s1t$ke9$1@news.albasani.net>
In reply to#101145
Am 02.01.2016 um 14:14 schrieb yehudak .:

> Thank you so much, but...
> All that is Chinese for me.
> Can you show a 'normal' Python code for me?

How about:

  >>> from math import log
  >>> sum([int(0.2**k*n) for k in range(1, int(log(n, 5))+1)])

}:-)

(That implements my procedure in my other answer.)

-- 
Robin Koch

[toc] | [prev] | [next] | [standalone]


#101160

FromTony van der Hoff <tony@vanderhoff.org>
Date2016-01-02 17:09 +0100
Message-ID<mailman.172.1451753180.11925.python-list@python.org>
In reply to#101157
On 02/01/16 16:57, Robin Koch wrote:
> sum([int(0.2**k*n) for k in range(1, int(log(n, 5))+1)])

But did you actually test it?

-- 
Tony van der Hoff  | mailto:tony@vanderhoff.org
Ariège, France     |

[toc] | [prev] | [next] | [standalone]


#101161

FromRobin Koch <robin.koch@t-online.de>
Date2016-01-02 17:56 +0100
Message-ID<n68vgc$qi6$1@news.albasani.net>
In reply to#101160
Am 02.01.2016 um 17:09 schrieb Tony van der Hoff:
> On 02/01/16 16:57, Robin Koch wrote:
>> sum([int(0.2**k*n) for k in range(1, int(log(n, 5))+1)])
>
> But did you actually test it?

Yes, should work for n >= 1.

Why do you ask?

-- 
Robin Koch

[toc] | [prev] | [next] | [standalone]


#101163

FromBernardo Sulzbach <mafagafogigante@gmail.com>
Date2016-01-02 15:24 -0200
Message-ID<mailman.174.1451755511.11925.python-list@python.org>
In reply to#101161
On Sat, Jan 2, 2016 at 2:56 PM, Robin Koch <robin.koch@t-online.de> wrote:
>
> Yes, should work for n >= 1.
>

The first power of 10 for which it fails in my machine is 10 ^ 17,
which is not that much for modern computers. Discrete math should not
meet floating points.

I would post the "canonical" solution here if Peter Otten hadn't just
posted it. You can use Wolfram Alpha - or Otten's solution - to see
that your solution fails for inputs equal to and larger than 10^17
(there likely is a lower bound, but I won't do binary search to find
it).

-- 
Bernardo Sulzbach

[toc] | [prev] | [next] | [standalone]


#101173

FromChris Angelico <rosuav@gmail.com>
Date2016-01-03 08:57 +1100
Message-ID<mailman.184.1451771860.11925.python-list@python.org>
In reply to#101161
On Sun, Jan 3, 2016 at 3:56 AM, Robin Koch <robin.koch@t-online.de> wrote:
> Am 02.01.2016 um 17:09 schrieb Tony van der Hoff:
>>
>> On 02/01/16 16:57, Robin Koch wrote:
>>>
>>> sum([int(0.2**k*n) for k in range(1, int(log(n, 5))+1)])
>>
>>
>> But did you actually test it?
>
>
> Yes, should work for n >= 1.
>
> 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). Floating point
can't represent every integer, and above 2**53 you end up able to
represent only those which are multiples of ever-increasing powers of
two; 100! is between 2**524 and 2**525, so any float operations are
going to be rounding off to the nearest 2**471 or thereabouts.
That's... a lot of rounding. That's like trying to calculate whether
pi is rational, but basing your calculations on the approximation
3.14. :)

ChrisA

[toc] | [prev] | [next] | [standalone]


#101176

FromRobin Koch <robin.koch@t-online.de>
Date2016-01-03 01:02 +0100
Message-ID<n69oev$uv8$1@news.albasani.net>
In reply to#101173
Am 02.01.2016 um 22:57 schrieb Chris Angelico:
> On Sun, Jan 3, 2016 at 3:56 AM, Robin Koch <robin.koch@t-online.de> wrote:
>> Am 02.01.2016 um 17:09 schrieb Tony van der Hoff:
>>>
>>> On 02/01/16 16:57, Robin Koch wrote:
>>>>
>>>> sum([int(0.2**k*n) for k in range(1, int(log(n, 5))+1)])
>>>
>>>
>>> But did you actually test it?
>>
>>
>> Yes, should work for n >= 1.
>>
>> 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.

I only tried values from 1 to 100 and then again 12345, I believe, to 
test the algorithm.

 > Floating point
> can't represent every integer, and above 2**53 you end up able to
> represent only those which are multiples of ever-increasing powers of
> two; 100! is between 2**524 and 2**525, so any float operations are
> going to be rounding off to the nearest 2**471 or thereabouts.
> That's... a lot of rounding. That's like trying to calculate whether
> pi is rational, but basing your calculations on the approximation
> 3.14. :)

When I find more time I take a closer look at it. Thank you (and 
Bernardo) for your clarification. I hope everyone who read my article 
reads yours, too and learns from it. ;-)

-- 
Robin Koch

[toc] | [prev] | [next] | [standalone]


#101177

FromBen Finney <ben+python@benfinney.id.au>
Date2016-01-03 11:20 +1100
Message-ID<mailman.186.1451780463.11925.python-list@python.org>
In reply to#101176
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

[toc] | [prev] | [next] | [standalone]


#101183

Fromsrinivas devaki <mr.eightnoteight@gmail.com>
Date2016-01-03 07:16 +0530
Message-ID<mailman.189.1451785646.11925.python-list@python.org>
In reply to#101176
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

[toc] | [prev] | [next] | [standalone]


#101203

FromTony van der Hoff <tony@vanderhoff.org>
Date2016-01-03 11:53 +0100
Message-ID<mailman.203.1451818386.11925.python-list@python.org>
In reply to#101161
On 02/01/16 17:56, Robin Koch wrote:
> Am 02.01.2016 um 17:09 schrieb Tony van der Hoff:
>> On 02/01/16 16:57, Robin Koch wrote:
>>> sum([int(0.2**k*n) for k in range(1, int(log(n, 5))+1)])
>>
>> But did you actually test it?
> 
> Yes, should work for n >= 1.
> 
> Why do you ask?
> 
>From your original post:

How about:

 >>> from math import log
 >>> sum([int(0.2**k*n) for k in range(1, int(log(n, 5))+1)])

That would never work; n is undefined. Now, you may have left that as an
exercise for the reader, but without warning, for an obvious newbie such
as the OP, that would have been unnecessarily confusing.

-- 
Tony van der Hoff  | mailto:tony@vanderhoff.org
Ariège, France     |

[toc] | [prev] | [next] | [standalone]


#101150

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2016-01-02 09:57 -0500
Message-ID<mailman.166.1451746655.11925.python-list@python.org>
In reply to#101139
On Sat, Jan 2, 2016 at 8:14 AM, yehudak . <katye2007@gmail.com> wrote:

> Vlastimil,
> Thank you so much, but...
> All that is Chinese for me.
> Can you show a 'normal' Python code for me?
>
> Yehuda
>
> 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,
> > rather an illustration of the available tools in python, than a
> > (submittable) solution:
> >
> > >>> import re, math
> > >>> len(re.search(r"0*$", str(math.factorial(100))).group())
> > 24
> > [or the same code on more lines with some indentation - if it is
> > preserved via e-mail]
> > >>> len(
> > ...     re.search(
> > ...         r"0*$",
> > ...         str(
> > ...             math.factorial(100)
> > ...             )
> > ...         ).group()
> > ...     )
> > 24
> > >>>
> >
> > I.e. You need the length of the string resulting as the match of the
> > regular expression search for a pattern representing zero or more "0"
> > at the end of the input text, which is the string version of 100!
> >
> > Of course, there are other ways to get this result :-)
> >
> > regards,
> >     vbr
> >
> --
> https://mail.python.org/mailman/listinfo/python-list
>

Can you improve your question?  Which python version, which os are standard
to tell.  Also, show the code you have written, its output, and what you
think is wrong with it.

This might be homework, since it is a very contrived problem.  But to push
you forward:  Can you get the factorial?  Can you get the string of that
result?  Do you know about lists, and slicing, and reversing a list (hint:
 a = "123", b = a[::-1] will return "321"
If you know these concepts, and know how to loop and count, you can solve
your puzzle
-- 
Joel Goldstick
http://joelgoldstick.com/stats/birthdays

[toc] | [prev] | [next] | [standalone]


#101152

FromVlastimil Brom <vlastimil.brom@gmail.com>
Date2016-01-02 16:24 +0100
Message-ID<mailman.168.1451748283.11925.python-list@python.org>
In reply to#101139
2016-01-02 14:14 GMT+01:00 yehudak . <katye2007@gmail.com>:
> Vlastimil,
> Thank you so much, but...
> All that is Chinese for me.
> Can you show a 'normal' Python code for me?
>
> Yehuda
>
> 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,
>> rather an illustration of the available tools in python, than a
>> (submittable) solution:
>>
>> >>> import re, math
>> >>> len(re.search(r"0*$", str(math.factorial(100))).group())
>> 24
>> [or the same code on more lines with some indentation - if it is
>> preserved via e-mail]
>> >>> len(
>> ...     re.search(
>> ...         r"0*$",
>> ...         str(
>> ...             math.factorial(100)
>> ...             )
>> ...         ).group()
>> ...     )
>> 24
>> >>>
>>
>> I.e. You need the length of the string resulting as the match of the
>> regular expression search for a pattern representing zero or more "0"
>> at the end of the input text, which is the string version of 100!
>>
>> Of course, there are other ways to get this result :-)
>>
>> regards,
>>     vbr
>
>
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

[toc] | [prev] | [next] | [standalone]


#101153

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2016-01-02 15:30 +0000
Message-ID<mailman.169.1451748640.11925.python-list@python.org>
In reply to#101139
On 02/01/2016 13:14, yehudak . wrote:
> Vlastimil,
> Thank you so much, but...
> All that is Chinese for me.
> Can you show a 'normal' Python code for me?
>
> Yehuda
>
> 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,
>> rather an illustration of the available tools in python, than a
>> (submittable) solution:
>>
>>>>> import re, math
>>>>> len(re.search(r"0*$", str(math.factorial(100))).group())
>> 24
>> [or the same code on more lines with some indentation - if it is
>> preserved via e-mail]
>>>>> len(
>> ...     re.search(
>> ...         r"0*$",
>> ...         str(
>> ...             math.factorial(100)
>> ...             )
>> ...         ).group()
>> ...     )
>> 24
>>>>>
>>
>> I.e. You need the length of the string resulting as the match of the
>> regular expression search for a pattern representing zero or more "0"
>> at the end of the input text, which is the string version of 100!
>>
>> Of course, there are other ways to get this result :-)
>>
>> regards,
>>      vbr
>>

I'll explain it if you stop top posting.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#101156

FromRobin Koch <robin.koch@t-online.de>
Date2016-01-02 16:54 +0100
Message-ID<n68rrg$k29$1@news.albasani.net>
In reply to#101139
Am 02.01.2016 um 12:49 schrieb katye2007@gmail.com:

> 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.

Using not Python, but math:

Every "0" at the end of 100! represents a multiplication by the factor 
10 or the factors 2 and 5.

There are 20 numbers with at least one factor 5.
There are  4 numbers with at least two factors 5 (=25).
There are no numbers with three factors 5 (=125).

There are 50 numbers with at least one factor 2.

So 100! contains 24 factors 5 and even more factors 2.
So 100! contains 24 facotrs 10 and therefore has 24 trailing zeros.

-- 
Robin Koch

[toc] | [prev] | [next] | [standalone]


#101162

FromPeter Otten <__peter__@web.de>
Date2016-01-02 18:18 +0100
Message-ID<mailman.173.1451755097.11925.python-list@python.org>
In reply to#101156
Robin Koch wrote:

> Am 02.01.2016 um 12:49 schrieb katye2007@gmail.com:
> 
>> 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.
> 
> Using not Python, but math:
> 
> Every "0" at the end of 100! represents a multiplication by the factor
> 10 or the factors 2 and 5.
> 
> There are 20 numbers with at least one factor 5.
> There are  4 numbers with at least two factors 5 (=25).
> There are no numbers with three factors 5 (=125).
> 
> There are 50 numbers with at least one factor 2.
> 
> So 100! contains 24 factors 5 and even more factors 2.
> So 100! contains 24 facotrs 10 and therefore has 24 trailing zeros.

Spelt in Python:

>>> def trailing_zeros_fact(n):
...     total = 0
...     while n:
...         n //= 5
...         total += n
...     return total
... 
>>> trailing_zeros_fact(100)
24
>>> trailing_zeros_fact(math.factorial(100))
23331553860986038170424809714066675122678992066095405367148240973804399998307478902235365994039129571563424480206805939562796302729215999999999999999999999901

[toc] | [prev] | [next] | [standalone]


#101159

FromTim Chase <python.list@tim.thechases.com>
Date2016-01-02 10:33 -0600
Message-ID<mailman.171.1451752514.11925.python-list@python.org>
In reply to#101139
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



[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.python


csiph-web