On 10 March 2018 at 02:18, MRAB <pyt...@mrabarnett.plus.com> wrote:
> On 2018-03-10 01:13, Steven D'Aprano wrote:
>> I am trying to enumerate all the three-tuples (x, y, z) where each of x,
>> y, z can range from 1 to ∞ (infinity).
>> This is clearly unhelpful:
>> for x in itertools.count(1):
>> for y in itertools.count(1):
>> for z in itertools.count(1):
>> print(x, y, z)
>> as it never advances beyond x=1, y=1 since the innermost loop never
>> Georg Cantor to the rescue! (Well, almost...)
>> Can anyone help me out here?
> Think about the totals of each triple. You'll get totals of 3, then totals
> of 4, etc.
> This might help, although the order they come out might not be what you
> def triples():
> for total in itertools.count(1):
> for i in range(1, total):
> for j in range(1, total - i):
> yield i, j, total - (i + j)
Mathematically, that's the usual generalisation of Cantor's diagonal argument.