[Tutor] puzzled again by decimal module
Tim Peters
tim.peters at gmail.com
Sat Aug 19 04:31:58 CEST 2006
[Tim Peters]
>> You would in this case, and that would be wrong. In fp you'd get an
>> approximation to the exact n * (1./5 + 1./5**2 + ...) == n/4. (use
>> the rule for the sum of an infinite geometric series). For example,
>> that way you'd compute that 4! == 24 has 4/4 == 1 trailing zero,
>> instead of the correct 4 // 5 == 0 trailing zeroes, and that 9! ==
>> 362880 has 9/4 == 2.25 trailing zeroes instead of the correct 9 // 5
>> == 1 trailing zero.
[Christian Tschabuschnig]
> well ... you're right, of course.
Of course -- but you should still do the exercise ;-)
>> Nope again. Count the number of trailing zeros in 100! more carefully.
> since i'm not able to count further than to 10 past midnight, i used
> echo -n "000000000000000000000000" | wc -c
> to do the work. and because i'm a confused person i forgot the "-n" :-)
This is a danger in trying to use tools other than Python -- never
touch them :-)
>>> prod = 1
>>> for i in xrange(2, 101):
... prod *= i
>>> s = str(prod)
>>> s[-25:]
'4000000000000000000000000'
>>> s.endswith('0' * 25)
False
>>> s.endswith('0' * 24)
True
or the ever-popular :-):
>>> from itertools import groupby
>>> firstkey, firstgroup = groupby(reversed(s)).next()
>>> firstkey
'0'
>>> len(list(firstgroup))
24
More information about the Tutor
mailing list