generating a series for all 2 power n combinations in

Posted on 2011-09-28
Last Modified: 2012-05-12
I have n items and each item can be assigned a 1 or a 2. So I would like to get the matrix result that would generate all possible combinations.
For eg. if n= 3 , then the possible outcomes are : I  need an algorithm that can generate this series for n . Please help thanks. ideally i would like to store the result in a datatable

1 1 1
1 1 2
1 2 1
2 1 1
2 1 2
1 2 2
2 2 1
2 2 2
Question by:TrialUser
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 12

Assisted Solution

jagssidurala earned 100 total points
ID: 36719097
Logic is

2^0 2^0 2^0
2^0 2^0 2^1
2^0 2^1 2^0
2^0 2^1 2^1
2^1 2^0 2^0
2^1 2^0 2^1
2^1 2^1 2^0
2^1 2^1 2^1

The power to the num 2 is binary seris


Accepted Solution

Ambusy earned 400 total points
ID: 36719344
        Dim HighestValue As Integer = 2 ' max value
        Dim NrOfValues As Integer = 3 ' nr of values in one result
        Dim Values(NrOfValues) As Integer
        Dim i As Integer
        For i = 0 To NrOfValues - 1
            Values(i) = 1
        Values(NrOfValues - 1) = 0 ' to generate first as ALL 1
        For i = 1 To HighestValue ^ NrOfValues
            Values(NrOfValues - 1) += 1
            For j As Integer = NrOfValues - 1 To 0 Step -1
                If Values(j) > HighestValue Then
                    Values(j) = 1
                    Values(j - 1) += 1
                End If
            Dim Result As String = ""
            For j As Integer = 0 To NrOfValues - 1
                Result = Result & CStr(Values(j))

Open in new window


Author Comment

ID: 36814309
Thank you both for the suggestoins. Both the suggestions are great and made me understand the concept behind.

Ambusy, the code you provided just works great and does exactly what I wanted.

Just one question, if I change     HighestValue , and NrOfValues  then this logic should just work for any number of items and any number of possible values for each item right?

thanks again.
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now


Expert Comment

ID: 36817258
Exactly, that's why I wrote them as variables. I tested them with values in a textbox to be certain it workes.

Expert Comment

ID: 36896300
If my solution is correct, please accept it.

Author Closing Comment

ID: 36948593
the solution works great. does exactly what I want

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question