It looks like you're new here. If you want to get involved, click one of these buttons!

- 141.4K All Categories
- 104.6K Programming Languages
- 6.4K Assembler Developer
- 1.9K Basic
- 39.8K C and C++
- 4.3K C#
- 7.9K Delphi and Kylix
- 4 Haskell
- 9.6K Java
- 4.1K Pascal
- 1.3K Perl
- 2K PHP
- 516 Python
- 48 Ruby
- 4.3K VB.NET
- 1.6K VBA
- 20.8K Visual Basic
- 2.6K Game programming
- 311 Console programming
- 89 DirectX Game dev
- 1 Minecraft
- 110 Newbie Game Programmers
- 2 Oculus Rift
- 8.9K Applications
- 1.8K Computer Graphics
- 729 Computer Hardware
- 3.4K Database & SQL
- 522 Electronics development
- 1.6K Matlab
- 628 Sound & Music
- 256 XML Development
- 3.3K Classifieds
- 195 Co-operative Projects
- 184 For sale
- 189 FreeLance Software City
- 1.9K Jobs Available
- 600 Jobs Wanted
- 201 Wanted
- 2.9K Microsoft .NET
- 1.7K ASP.NET
- 1.1K .NET General
- 3.3K Miscellaneous
- 4 Join the Team
- 0 User Profiles
- 352 Comments on this site
- 59 Computer Emulators
- 2.1K General programming
- 181 New programming languages
- 611 Off topic board
- 168 Mobile & Wireless
- 42 Android
- 124 Palm Pilot
- 335 Multimedia
- 151 Demo programming
- 184 MP3 programming
- 0 Bash scripts
- 19 Cloud Computing
- 53 FreeBSD
- 1.7K LINUX programming
- 367 MS-DOS
- 0 Shell scripting
- 320 Windows CE & Pocket PC
- 4.1K Windows programming
- 891 Software Development
- 408 Algorithms
- 68 Object Orientation
- 87 Project Management
- 90 Quality & Testing
- 237 Security
- 7.5K WEB-Development
- 1.8K Active Server Pages
- 61 AJAX
- 2 Bootstrap Themes
- 55 CGI Development
- 19 ColdFusion
- 222 Flash development
- 1.4K HTML & WEB-Design
- 1.4K Internet Development
- 2.2K JavaScript
- 34 JQuery
- 284 WEB Servers
- 151 WEB-Services / SOAP

Lets say I got these eight numbers 2,6,12,17,18,24,29,32

I would like to get an array over every way to combine 7 of those numbers.

There would be eight of them:

2,6,12,17,18,24,29

2,6,12,17,18,24,32

2,6,12,17,18,29,32

2,6,12,17,24,29,32

2,6,12,18,24,29,32

2,6,17,18,24,29,32

2,12,17,18,24,29,32

6,12,17,18,24,29,32

For a nine numbers i would get 36: 7 number combination

For 10 numbers i would get 120: 7 number combinations

For 11 numbers i would get 330: 7 number combinations

For 12 numbers i would get 792: 7 number combinations

How do i build a loop for that would iterate an array with up to twelve numbers and return a new array with all possible 7 number combination?

Hope I'm making sense with my question

Terms of use / Privacy statement / Publisher: Lars Hagelin

Programmers Heaven articles / Programmers Heaven files / Programmers Heaven uploaded content / Programmers Heaven C Sharp ebook / Operated by CommunityHeaven LLC

© 1997-2015 Programmersheaven.com - All rights reserved.

## Comments

2,444Member:

: Lets say I got these eight numbers 2,6,12,17,18,24,29,32

: I would like to get an array over every way to combine 7 of those

: numbers.

: There would be eight of them:

: 2,6,12,17,18,24,29

: 2,6,12,17,18,24,32

: 2,6,12,17,18,29,32

: 2,6,12,17,24,29,32

: 2,6,12,18,24,29,32

: 2,6,17,18,24,29,32

: 2,12,17,18,24,29,32

: 6,12,17,18,24,29,32

:

: For a nine numbers i would get 36: 7 number combination

: For 10 numbers i would get 120: 7 number combinations

: For 11 numbers i would get 330: 7 number combinations

: For 12 numbers i would get 792: 7 number combinations

:

: How do i build a loop for that would iterate an array with up to

: twelve numbers and return a new array with all possible 7 number

: combination?

:

: Hope I'm making sense with my question

:

Actually, that's quite a challenging problem.

Here's a thought:

You lay out all the numbers (9 right now, but let's call it N)

2,6,12,17,18,24,29,32,35

You want all different lotto's of length L

And what you do each time it take N-L numbers out of this list.

So what you need to do is find a systematic way to take N-L numbers out of the list. The problem reduces into finding all series (a,b,c,...) of length N-L where a < b < c <...

In a single loop, using a dynamic array of size N-L:

[code]

Dim NList() As Integer = {...} 'TODO: Fill list with N numbers

Dim N As Integer = ?

Dim L As Integer = ?

Dim nCoords(N - L - 1) As Integer

Dim i As Integer

Dim bDone As Boolean = False

'Initial entry

For i = 0 To N - L - 1

nCoords(i) = i

Next i

Do

'Fix the -1's in nCoords (see below for why we need to;

' it won't be neccesary for the first loop)

For i = 1 To N - L - 1

[color=Blue]If (nCoords(i) = -1) Then

nCoords(i) = nCoords(i - 1) + 1

End If[/color]

Next i

'TODO: Output lotto number by taking each element of NList() whose

' index is not in nCoords()

...

'Now for the hardest part: increment nCoords() while keeping the

' restriction that a < b < c < ...

'Start with the last coordinate

i = N - L - 1

Do While i >= 0

[color=Blue] 'Add one, but make sure we stay below N - ((N - L - 1) - i)

' because each entry can not be bigger or equal to N, and

' each next entry is one bigger. So the restriction becomes:

'If we add 1 to this entry, make sure the last entry can still

' be below N[/color]

nCoords(i) += 1

If (nCoords(i) >= [color=Blue](L + 1 + i)[/color]) Then

'We'll also need to increment the coordinate before this one

'Here we have the -1's that need fixing

nCoords(i) = -1

i -= 1

Else

'We're done

i = -1

End If

Loop

'We're done as soon as the first coordinate needs fixing (= loop around)

Loop While [color=Blue]nCoords(0) <> -1[/color]

[/code]

In the TODO: Print Lotto, something like this can be used:

[code]

Dim sLine As String = ""

Dim bAdd As Boolean

Dim j As Integer

For i = 0 To N - 1

bAdd = True

'Check if 'i' is in nCoords()

For j = 0 To N - L - 1

If (nCoords(j) = i) Then

bAdd = False

Exit For

End If

Next j

'If not found, then we print this number

If (bAdd) Then

sLine = sLine & NList(i) & ","

End If

Next i

'Trim off the trailing ','

sLine = Left(sLine, sLine.Length - 1)

'Now sLine is your Lotto

[/code]

I tested it (in VB6, but should work for .NET as well) and it worked for me. The output seemed quite ok, even though I did not test it.

[color=Blue]EDIT: I had a bug in the nCoords() incrementation... I fixed it now. See the changes in Blue[/color]

Good luck

Best Regards,

Richard

The way I see it... Well, it's all pretty blurry

28Member: :

: : Lets say I got these eight numbers 2,6,12,17,18,24,29,32

: : I would like to get an array over every way to combine 7 of those

: : numbers.

: : There would be eight of them:

: : 2,6,12,17,18,24,29

: : 2,6,12,17,18,24,32

: : 2,6,12,17,18,29,32

: : 2,6,12,17,24,29,32

: : 2,6,12,18,24,29,32

: : 2,6,17,18,24,29,32

: : 2,12,17,18,24,29,32

: : 6,12,17,18,24,29,32

: :

: : For a nine numbers i would get 36: 7 number combination

: : For 10 numbers i would get 120: 7 number combinations

: : For 11 numbers i would get 330: 7 number combinations

: : For 12 numbers i would get 792: 7 number combinations

: :

: : How do i build a loop for that would iterate an array with up to

: : twelve numbers and return a new array with all possible 7 number

: : combination?

: :

: : Hope I'm making sense with my question

: :

:

: Actually, that's quite a challenging problem.

: Here's a thought:

:

: You lay out all the numbers (9 right now, but let's call it N)

: 2,6,12,17,18,24,29,32,35

:

: You want all different lotto's of length L

: And what you do each time it take N-L numbers out of this list.

: So what you need to do is find a systematic way to take N-L numbers

: out of the list. The problem reduces into finding all series

: (a,b,c,...) of length N-L where a < b < c <...

:

: In a single loop, using a dynamic array of size N-L:

: [code]:

: Dim NList() As Integer = {...} 'TODO: Fill list with N numbers

: Dim N As Integer = ?

: Dim L As Integer = ?

: Dim nCoords(N - L - 1) As Integer

: Dim i As Integer

: Dim bDone As Boolean = False

:

: 'Initial entry

: For i = 0 To N - L - 1

: nCoords(i) = i

: Next i

:

: Do

: 'Fix the -1's in nCoords (see below for why we need to;

: ' it won't be neccesary for the first loop)

: For i = 1 To N - L - 1

: If (nCoords(i) = -1) Then

: nCoords(i) = nCoords(i - 1) + 1

: 'As soon as we can't fix a coordinate we are done

: If (nCoords(i) >= N) Then Exit Do

: End If

: Next i

:

: 'TODO: Output lotto number by taking each element of NList() whose

: ' index is not in nCoords()

: ...

:

: 'Now for the hardest part: increment nCoords() while keeping the

: ' restriction that a < b < c < ...

: 'Start with the last coordinate

: i = N - L - 1

: Do While i >= 0

: 'Add one, but make sure we stay below N

: nCoords(i) += 1

: If (nCoords(i) >= N) Then

: 'We'll also need to increment the coordinate before this one

: 'Here we have the -1's that need fixing

: nCoords(i) = -1

: i -= 1

: Else

: 'We're done

: i = -1

: End If

: Loop

: Loop While True 'Loop will break when failing to fix coords

: [/code]:

:

: In the TODO: Print Lotto, something like this can be used:

: [code]:

: Dim sLine As String = ""

: Dim bAdd As Boolean

: Dim j As Integer

:

: For i = 0 To N - 1

: bAdd = True

: 'Check if 'i' is in nCoords()

: For j = 0 To N - L - 1

: If (nCoords(j) = i) Then

: bAdd = False

: Exit For

: End If

: Next j

: 'If not found, then we print this number

: If (bAdd) Then

: sLine = sLine & NList(i) & ","

: End If

: Next i

: 'Trim off the trailing ','

: sLine = Left(sLine, sLine.Length - 1)

: 'Now sLine is your Lotto

: [/code]:

:

: I tested it (in VB6, but should work for .NET as well) and it worked

: for me. The output seemed quite ok, even though I did not test it.

:

: Good luck

: Best Regards,

: Richard

:

: The way I see it... Well, it's all pretty blurry

Hi

Thank you. I will try youre code above. I've found out this so far by using a bit-pattern to find out the uniqe combinations. I got help with this through another board

Think of it this way.

If you wanted 3 items from a LIST of 4.

If the list is as.>> a,b,c,d

Get the bit patterns.

0000

0001

0010

0011

0101

0110

[color=Green]0111[/color] as b,c,d due to a being left out.

1000

1001

1010

[color=Green]1011[/color] as a, c,d due to b being left out.

[color=Green]1101[/color] as a,b, d due to c being left out.

[color=Green]1110[/color] as a,b,c due to d being left out.

1111

Only the 4 [color=Green]green[/color] highlighted ones have three ones in them and each pattern is unique.

Based on the bit pattern I pick out the numbers from the LIST in the code if the digit is a 1.

[code]

Option Strict On

Imports System.IO

Imports System.Environment

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Create a LIST of numbers of TYPE Integer.

Dim aListOfNumbers As New List(Of Integer)

'Add the numbers 1 to 13 to the LIST.

For num As Integer = 1 To 13

aListOfNumbers.Add(num)

Next

'Used to hold the results.

Dim resultsList As New List(Of String)

'Get the Combinations into another LIST.

resultsList = GetCombinations(7, aListOfNumbers)

'Show the output.

Dim outputString As String = ""

For Each str As String In resultsList

outputString &= str & NewLine

Next

MessageBox.Show(outputString)

Dim response As Integer 'Used for the response.

'Ask the user if HE/She wants the results in a FILE.>>

response = MessageBox.Show("Write to a file? (Y/N)?", "File write (Y/N)?", MessageBoxButtons.YesNo)

Dim sfd1 As New SaveFileDialog

sfd1.Filter = "Text files only|*.txt"

Dim sw As StreamWriter

Try

If response = vbNo Then

Application.Exit()

Else

sfd1.ShowDialog()

sw = New StreamWriter(sfd1.FileName)

'Write the contents of the LIST to a file.

For Each st As String In resultsList

sw.WriteLine(st)

Next

sw.Close()

sw.Dispose()

'Show the results in Notepad. :-)

Process.Start("Notepad.exe", sfd1.FileName)

sfd1.Dispose()

End If

Catch ex As Exception

MessageBox.Show("You must have clicked on CANCEL!!")

MessageBox.Show(ex.ToString)

End Try

End Sub

Private Function GetCombinations(ByVal numberOfItems As Integer, ByVal numberList As List(Of Integer)) As List(Of String)

Dim returnList As New List(Of String)

Dim binaryStringList1 As New List(Of String)

If numberOfItems > numberList.Count Then

MessageBox.Show("Error numberOfItems must be less than or equal to the number of items in the list.")

Return returnList

End If

If numberOfItems <= 0 Then

MessageBox.Show("Error numberOfItems must be greater than or equal to 1.")

Return returnList

End If

'Generate a list of the bit patterns.

Dim string1 As String = ""

For index1 As Integer = (Power(2, numberList.Count) - 1) To 0 Step -1

string1 = Convert.ToString(index1, 2) 'Convert the number to binary.

If string1.Length < numberList.Count Then

string1 = string1.PadLeft(numberList.Count, Convert.ToChar("0"))

End If

If Has_x_Number1s_In_It(numberOfItems, string1) = True Then

binaryStringList1.Add(string1)

End If

Next

'Extract each of the numbers from the number list

'where the bit pattern is a "1" and add that sequence

'to the returnList.

Dim outputString As String = ""

For Each str As String In binaryStringList1

For index2 As Integer = 0 To str.Length - 1

If str.Substring(index2, 1) = "1" Then

outputString &= numberList.Item(index2).ToString & ","

End If

Next

outputString = outputString.Substring(0, outputString.Length - 1)

returnList.Add(outputString)

outputString = ""

Next

Return returnList

End Function

Private Function Has_x_Number1s_In_It(ByVal numberSought As Integer, ByVal aString As String) As Boolean

Dim count As Integer = 0

For index As Integer = 0 To aString.Length - 1

If aString.Substring(index, 1) = "1" Then count += 1

Next

If count = numberSought Then

Return True

Else

Return False

End If

End Function

Private Function Power(ByVal aNumber As Integer, ByVal toThePowerOf As Integer) As Integer

Dim result As Integer = aNumber

For num As Integer = 2 To toThePowerOf

result = result * aNumber

Next

Return result

End Function

End Class

[/code]

2,444Member:

: Thank you. I will try youre code above. I've found out this so far

: by using a bit-pattern to find out the uniqe combinations. I got

: help with this through another board

: Think of it this way.

: If you wanted 3 items from a LIST of 4.

:

: If the list is as.>> a,b,c,d

: Get the bit patterns.

:

: 0000

: 0001

: 0010

: 0011

: 0101

: 0110

: [color=Green]0111[/color] as b,c,d due to a being left out.

:

: 1000

: 1001

: 1010

: [color=Green]1011[/color] as a, c,d due to b being left out.

: [color=Green]1101[/color] as a,b, d due to c being left out.

: [color=Green]1110[/color] as a,b,c due to d being left out.

: 1111

:

: Only the 4 [color=Green]green[/color] highlighted ones have three

: ones in them and each pattern is unique.

:

: Based on the bit pattern I pick out the numbers from the LIST in the

: code if the digit is a 1.

:

Yeah, I thought about that solution too. Problem is, I don't like doing 2^n permutation ... and counting the bits doesn't go that fast either.

So I thought further for a code that would loop through only exactly those permutation for which the bit-pattern restriction holds.

Ofcourse, for length 9 both codes will run fast enough. But my code will still run reasonably fast for 16, or 32 numbers.

EDIT: I did have a bug in my code though (it wasn't visible for N-L <= 2). I fixed it now - the changes are in my original post in blue

Have fun with your project

Best Regards,

Richard

The way I see it... Well, it's all pretty blurry

748Member:

: Lets say I got these eight numbers 2,6,12,17,18,24,29,32

: I would like to get an array over every way to combine 7 of those

: numbers.

: There would be eight of them:

: 2,6,12,17,18,24,29

: 2,6,12,17,18,24,32

: 2,6,12,17,18,29,32

: 2,6,12,17,24,29,32

: 2,6,12,18,24,29,32

: 2,6,17,18,24,29,32

: 2,12,17,18,24,29,32

: 6,12,17,18,24,29,32

:

: For a nine numbers i would get 36: 7 number combination

: For 10 numbers i would get 120: 7 number combinations

: For 11 numbers i would get 330: 7 number combinations

: For 12 numbers i would get 792: 7 number combinations

:

: How do i build a loop for that would iterate an array with up to

: twelve numbers and return a new array with all possible 7 number

: combination?

:

: Hope I'm making sense with my question

_________________________________________

Hi Eagleman,

You know who!!

In this site I have the I.d. of DrMarten.

For reference the other site thread is at.>>

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2673754&SiteID=1

Regards,

John

a.k.a DrMarten

P.S. I have not been on here in a while.