[Tutor] Can my code be optimized any further (speed-wise)?

Geoframer geoframer at gmail.com
Sun Oct 8 08:43:38 CEST 2006


Erm i made a typo

Sample input should ofcourse be without the '!''s

So input =
5
3
5
50
100
125

Sorry - Geoframer


On 10/8/06, Geoframer <geoframer at gmail.com> wrote:
>
> The problem is that these assignments are evaluated automatically. So i
> can not use description strings to clearify output or input etc.
>
> Basically it's like this:
>
> a = the number of times i'm going to read input to evaluate
> lst = the factors of 5. I.e. (5, 5^2, 5^3, 5^4.... etc) until the last one
> wich is smaller then 1000000000
>
> For those that didn't know the number of trailing zero's in a factorial is
> decided by the numer of factors of 5, because only a 5 can create a 0.
> Remember a factorial (n) = n*....*4*3*2*1. The list does not represent
> factorials.
>
> Example input of this program:
> 5                                 #number of times i'm going to read in a
> number
> 3                                 # 3! = 6, no trailing 0's zo the output
> would be 0
> 5                                 # 5! = 120, 1 trailing 0's so the output
> would be 1
> 50!                              #50!=  <very big number>, 12 trailing 0's
> (50/5 + 50/25=12)
> 100!                            #100!= <very big number>, 24 trailing 0's
> (100/5 + 100/25 = 24)
> 125!                            #125!= <very big number>, 31 trailing 0's
> (125/5 + 125/25 +125/125 = 31)
>
> Output would be:
> 0
> 1
> 12
> 24
> 31
>
> Hope this clarifies what i'm trying to achieve here...
>
> Ciao - Geofram
>
>
> On 10/8/06, Alan Gauld <alan.gauld at btinternet.com> wrote:
> >
> > Hi,
> >
> > Maybe its just me but I didn't understand what you are
> > trying to do...
> >
> > > The problem is to compute the number of trailing zero's in
> > > factorials (n! =
> > > 1*2*3*4*.......*n). with n <= 1000000000
> > >
> > > My solution is as follows (a = times to read a number (b) to
> > > process) :
> > >
> > >
> > ---------------------------------------------------------------------------
> > >
> > > a = input()
> >
> > what does a represent? Its usually worth putting a prompt string into
> > input() - and raw_input() - just as documentation if nothing else!
> > And maybe a descriptive variable name if appropriate.
> >
> > > for i in range(a):
> > >    lst = (5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125,
> > > 9765625,
> > > 48828125, 244140625)
> >
> > None of these have any trailing zeros so far as I can tell?
> > What answer are you expecting from this function?
> >
> > >    ans = 0
> > >    b = input()
> >
> > similarly, what is b supposed to be?
> >
> > >    for i in lst:
> >
> > And here you are throwing away the i from the outer loop
> >
> > >        if i <= b:
> > >            ans += b//i
> >
> > And now you divide some arbitrary input value by each factorial
> > in turn and sum the results?
> >
> > >    print ans
> >
> > > Please, if you have suggestions to improve the speed of this
> > > algorithm give
> > > an argumentation as to why a certain something is faster.
> >
> > I nbeed to understand what you are tryying to do first, its
> > not making much sense at the moment. (But it is after midnight! :-)
> >
> > > P.s. I know python is probably not the best language for these kinds
> > > of
> > > problems (I can code it in C or C++), it just bugs me that it's
> > > possible
> > > and my solution is failing...  ;-)
> >
> > Actually it looks like exactly what I would use Python for rarther
> > than C/C++. The speed issue can be solved in numerous ways but
> > as always lets be sure we get the functionality clear before starting
> > to optimise things.
> >
> > Alan G.
> >
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061008/f399702b/attachment.htm 


More information about the Tutor mailing list