473,473 Members | 1,723 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Generating combinations

Hi there,

I'm looking for an algorithm that will generate combinations in the
following way:

Given a lower bound, and an upper bound, generate a set of size n
containing all combinates of numbers between the bounds.

e.g if lower_bound = 5, upper bound = 7, size = 3, the algorithm would
generate:

555
556
557
565
566
567
575
576
577
655

etc.....

(in an ideal world i'd eliminate dupes as well, so 557 wont be produced
as well as 575).

I thought initially of generating a 'master set' such as '555666777',
and running next_permutation on it to get all the subsets, just taking
the first 3 each time, but in the biggest case( 9 numbers, 1 to 9) this
would take ages.

Is there a simple way?

Sep 8 '05 #1
4 5514
pa***********@lycosmax.co.uk wrote:
Hi there,

I'm looking for an algorithm that will generate combinations in the
following way:

Given a lower bound, and an upper bound, generate a set of size n
containing all combinates of numbers between the bounds.

e.g if lower_bound = 5, upper bound = 7, size = 3, the algorithm would
generate:

555
556
557
565
566
567
575
576
577
655

etc.....

(in an ideal world i'd eliminate dupes as well, so 557 wont be produced
as well as 575).

I thought initially of generating a 'master set' such as '555666777',
and running next_permutation on it to get all the subsets, just taking
the first 3 each time, but in the biggest case( 9 numbers, 1 to 9) this
would take ages.

Is there a simple way?


Hint: you may use recursion. To avoid duplicates, never generate a new
digit which is less than a previous digit.

Sep 8 '05 #2

555
556
557
566
567
577
666
667
677
777
start at the right digit, IF you can add 1 to it, and it would still be less
than upper_bound, then update this digit (old digit + 1) and update all the
digits to the right of it as equal to it, ELSE move to the next digit to the
left, and try the same.
Stop when you cant update the first digit to the left.

<pa***********@lycosmax.co.uk> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Hi there,

I'm looking for an algorithm that will generate combinations in the
following way:

Given a lower bound, and an upper bound, generate a set of size n
containing all combinates of numbers between the bounds.

e.g if lower_bound = 5, upper bound = 7, size = 3, the algorithm would
generate:

555
556
557
565
566
567
575
576
577
655

etc.....

(in an ideal world i'd eliminate dupes as well, so 557 wont be produced
as well as 575).

I thought initially of generating a 'master set' such as '555666777',
and running next_permutation on it to get all the subsets, just taking
the first 3 each time, but in the biggest case( 9 numbers, 1 to 9) this
would take ages.

Is there a simple way?

Sep 8 '05 #3

<pa***********@lycosmax.co.uk> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Hi there,

I'm looking for an algorithm that will generate combinations in the
following way:

Given a lower bound, and an upper bound, generate a set of size n
containing all combinates of numbers between the bounds.

e.g if lower_bound = 5, upper bound = 7, size = 3, the algorithm would
generate:

555
556
557
565
566
567
575
576
577
655
notice that the above sequence is the same as with an appropriate mapping

000 = 0
001 = 1
002 = 2
010 = 3
011 = 4
012 = 5
020 = 6
021 = 7
022 = 8
100 = 9
101 = 10
102 = 11
110 = 12
etc...
i.e, (upper bound - lower bound) is your base and the "order" is size

so to generate the list above in base 10 you simply do

for(int i=0; i< something; i++)
{
list[i] = i;
}

very simple huh? ;) (Ofcourse you don't need to use a list to store them
though)
by converting val to the your bass and using the a digit mapping array you
can easily get what you want.

i.e. 000 = 555 because '0' = '5', etc...

022 = 577 because '0' = '5' and '2' = 7
Hopefully that makes sense. Basicaly you just need to figure out how to
convert from base 10 to base (upper bound - lower bound) and write a routine
to generate a conversion mapping and then it should be pretty easy.
etc.....

(in an ideal world i'd eliminate dupes as well, so 557 wont be produced
as well as 575).

I'm not exactly sure what you mean by dupes ;/
I thought initially of generating a 'master set' such as '555666777',
and running next_permutation on it to get all the subsets, just taking
the first 3 each time, but in the biggest case( 9 numbers, 1 to 9) this
would take ages.

Is there a simple way?

It think so...
Jon
Sep 8 '05 #4
Jon Slaughter wrote:
<pauljwilliams> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
(in an ideal world i'd eliminate dupes as well, so 557 wont be produced
as well as 575).


I'm not exactly sure what you mean by dupes ;/


Duplications.

Combinations vs. Permutations.

Ben
--
I'm not just a number. To many, I'm known as a String...
Sep 8 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alex Vinokur | last post by:
Does STL contain algorithms which generate (enable to generate) exponents, permutations, arrangements, and combinations for any numbers and words? -- Alex Vinokur mailto:alexvn@connect.to...
0
by: Alex Vinokur | last post by:
C++ program for generating combinatorial objects (exponents, permutations, arrangements, combinations for any numbers and words) can be downloaded at...
16
by: akameswaran | last post by:
Ok, this is really irritating me. I'm sure there are different ways of doing this - I'm interested in the algo, not the practical solution, I'm more trying to play with iterators and recursion. I...
8
by: Mir Nazim | last post by:
Hello, I need to write scripts in which I need to generate all posible unique combinations of an integer list. Lists are a minimum 12 elements in size with very large number of possible...
1
by: mrajanikrishna | last post by:
Hi friends, I am generating combinations from a set with different sub sets. Here is my code. For this example, actually I need to get 56 combinations but I am getting 35 only. Can anybody...
6
by: py_genetic | last post by:
Hi, I'm looking to generate x alphabetic strings in a list size x. This is exactly the same output that the unix command "split" generates as default file name output when splitting large...
4
by: RSH | last post by:
Okay my math skills aren't waht they used to be... With that being said what Im trying to do is create a matrix that given x number of columns, and y number of possible values i want to generate...
1
by: Voxus | last post by:
(This is for C++ guys not C) Hi, I’m a new user and I haven't paid any attention to setting my details, as I am just here to get a little help on programming a small project I’m working on. I...
10
by: henryrhenryr | last post by:
Hi here's a test of logic for mathmeticians (I do have an actual use for this too...) I need to split a 4 or 5 word phrase into all possible combinations of the individual words: eg: 4...
14
by: bjorklund.emil | last post by:
Hello pythonistas. I'm a newbie to pretty much both programming and Python. I have a task that involves writing a test script for every possible combination of preference settings for a software...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.