A question on modification of a list via a function invocation

Chris Angelico rosuav at gmail.com
Fri Sep 8 08:42:11 EDT 2017


On Fri, Sep 8, 2017 at 10:05 PM, Steve D'Aprano
<steve+python at pearwood.info> wrote:
> On Fri, 8 Sep 2017 05:48 pm, Gregory Ewing wrote:
>
>> Steve D'Aprano wrote:
>>> A harder question is, what if you take a random number from the Integers? How
>>> many digits will it have in (say) base 10? I don't have a good answer to
>>> that. I think it may be ill-defined.
>>
>> I think the answer is that on average it has infinitely many
>> digits -- despite every actual integer only having finitely
>> many digits!
>
> I don't think that talking about the average integer is meaningful. Assuming we
> are talking about the arithmetic mean, we're dealing with a divergent sum that
> depends on the way you do the summation:
>
> 0 + 1 + -1 + 2 + -2 + 3 + -3 + ... = 0
>
> 0 + 1 + 2 + 3 + 4 + ... + (-1 - 2 - 3 - 4 - ...) = ∞ - ∞ which is undefined.
>
> (Other means have similar problems, they're just harder to write or less
> familiar.)

Here's a different take on the problem. The first integer (zero) has,
let's say, no digits. Then the next 18 (1 through 9 and -1 through -9)
have one digit. The next 180 have two digits (getting us to 99 and
-99). Etcetera. Multiply each digit count by how many numbers have it,
and divide by the grand total:

(0 * 1 + 1 * 18 + 2 * 18 * 10 + 3 * 18 * 100 ...) / (1 + 18 + 18*10 +
18*100 ...)

As you add terms, the earlier terms become diminishingly significant
to the result, and the final term approaches digits/1.1 (in this
example, 3 * 1800 / (1800+180+18+1)). Since digits is tending towards
+∞, so is the sum.

ChrisA



More information about the Python-list mailing list