[Tim Peters] >> For a fun :-) exercise, prove that the number of trailing zeroes in n! >> is the sum, from i = 1 to infinity, of n // 5**i (of course as soon as >> you reach a value of i such that n < 5**i, the quotient is 0 at that i >> and forever after). >> >> In this case, >> >> 100 // 5 + 100 // 25 + 100 // 125 + ... = >> >> 20 + 4 + 0 + ... = >> >> 24
[Christian Tschabuschnig] > you should do that with floating-point, so that the quotient never get's > zero and the "i=1 to infinity" makes sense. Definitely not. You didn't do the exercise ;-) Hint: n // m is the number of integers in 1 through n divisible by m. > This way you (might) get 25 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. > which is correct; not 24. Nope again. Count the number of trailing zeros in 100! more carefully. _______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
