# Re: Fastest way to generate all combinations?

*From*: Richard Heathfield <invalid@xxxxxxxxxxxxxxx>*Date*: Tue, 01 Aug 2006 17:15:46 +0000

Bryan said:

I want to generate a list of all possible combinations of x, y, z where

each of xyz can range from 0 -> 1000. What is the fastest way to do

this in C++?

Im using 3 nested for loops right now, but I was wondering if there was

a more efficient algorithm for this?

1000 * 1000 * 1000 * sizeof(int) is, best case, getting on for 2 Gigabytes,

and may well be double that. I have to ask - are you *sure* you want to do

this?

Anyway, one obvious optimisation is:

for(unsigned long n = 0; n < 1000000000UL; n++)

{

mylist[n].x = n / 1000000UL;

mylist[n].y = (n / 1000UL) % 1000UL;

mylist[n].z = n % 1000UL;

}

which eliminates your inner loops.

--

Richard Heathfield

"Usenet is a strange place" - dmr 29/7/1999

http://www.cpax.org.uk

email: rjh at above domain (but drop the www, obviously)

.

**Follow-Ups**:**Re: Fastest way to generate all combinations?***From:*Hallvard B Furuseth

**Re: Fastest way to generate all combinations?***From:*Chris Uppal

**Re: Fastest way to generate all combinations?***From:*blmblm

**References**:**Fastest way to generate all combinations?***From:*Bryan

- Prev by Date:
**Fastest way to generate all combinations?** - Next by Date:
**Re: Fastest way to generate all combinations?** - Previous by thread:
**Fastest way to generate all combinations?** - Next by thread:
**Re: Fastest way to generate all combinations?** - Index(es):