Enumerating all 3-tuples

Elliott Roper nospam at yrl.co.uk
Mon Mar 12 11:31:30 EDT 2018


On 10 Mar 2018, Paul Moore wrote
(in article<mailman.8.1520680557.1867.python-list at python.org>):

> On 10 March 2018 at 02:18, MRAB<python at 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
> > > finishes.
> > >
> > > 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
> > want:
> >
> > 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.
>
> Paul

Would a multi-dimensional Hilbert curve do the job? See the Wikipedia article 
for starters

-- 
To de-mung my e-mail address:- fsnospam$elliott$$ PGP Fingerprint: 1A96 3CF7 
637F 896B C810 E199 7E5C A9E4 8E59 E248




More information about the Python-list mailing list