﻿version
6.01
0
Form1
1
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
1
0
0
0
238
268

0
0
0
Sub designer





addform(Form1,"Form1","",211,211,211)@
addtextbox(form1,TextBox1,5,55,215,205,"",255,255,255,0,0,0,True,True,True,9)@
End Sub
@EndOfDesignText@' (n,k) = n!/ (k! . (n-k)! )
' (n,k) = (n,n-k)
' (n-k) = (n.(n-1).(n-2).  .. (n-k+1)/ (1.2.3. .. k)
'******************************
Sub Globals
	n = 10
	k = 3
	Dim strings(0)
	Dim Data(0)
	Dim DataNext(0)
	txt = ""
	t = 1
End Sub
'******************************
Sub App_Start
	Form1.show
	temp = choose(n,k)
'	Msgbox("with n = " & n & " and k = " &  k & crlf & "there are " & temp & " combinations")
	Dim strings(temp)
	Dim Data(k)
	Dim DataNext(k)
	txt = ""

	t = 1
	Combination(n,k)
	txt = "Combinations: " & temp & CRLF
	textbox1.Text =StringBuilder

	For t = 2 To temp
		Successor
		DoEvents
		textbox1.Text = StringBuilder
	Next
End Sub
'******************************
Sub Successor
'Notice that you can identify the last lexicographic element, { 2, 3, 4 }, because it is the only element which has a first atom of 2—which equals the value n-k, OR in other words, it has a value of n-k at position 0. This relationship is true in general For all combinations. Likewise, you can identify the first lexicographic element, { 0, 1, 2 }, because it is the only one with a last atom of 2 OR, in other words, it has a value of k-1 at position k-1. Again, this is true in general. The Successor method returns null If there is no valid Next element. Alternatives To returning null would be To throw an exception OR Return an error code.
'
'The algorithm To generate the Successor element does Not use any special tricks. Essentially you start at the right-most atom AND move left Until you locate the left-most atom which should be incremented. Then you increment the atom at index i AND reset all atoms To the right of i To be 1 larger than the value To its left. For example, suppose n = 5 AND k = 3 AND you want the successor To the combination { 0, 3, 4 }. Index i starts at cell 2 (pointing To atom value 4), AND moves left Until it hits cell 0 (pointing at atom value 0). That atom value is incremented To 1, AND all atoms To the right (the 3 AND 4) are incremented from the value To the left in the array, yielding the result { 1, 2, 3 }.
'{ 0, 1, 2 } { 0, 3, 4 }
'{ 0, 1, 3 } { 1, 2, 3 }
'{ 0, 1, 4 } { 1, 2, 4 }
'{ 0, 2, 3 } { 1, 3, 4 }
'{ 0, 2, 4 } { 2, 3, 4 }

	'read old Data in new Data to compute sequence
	For i = 0 To k-1
		 DataNext(i) = Data(i)
	Next

	i = k-1
	Do While ( i > 0) AND DataNext(i) = n-k+i
		i= i-1
	Loop
	'increase atom at position i
	DataNext(i) = DataNext(i)+1
	j = i
	Do While j < k-1
		DataNext(j+1) = DataNext(j)+1
		j=j+1
	Loop
	
	For i = 0 To k-1
		Data(i) = DataNext(i)
	Next
End Sub
'******************************
Sub Combination(nn,kk)
	If (nn<0) OR (kk<0) Then 
		Msgbox("negative parameter in constructor")
	Else
		For i = 0 To kk-1
			Data(i) = i
		Next
	End If
End Sub
'******************************
Sub Stringbuilder
	txt = txt & t & "  {"
	For i = 0 To k-1
		txt = txt & Data(i) & "  "
	Next
	txt = txt & "}" & CRLF
	Return txt
End Sub
'******************************
Sub Choose(nn,kk)
	' effficient Choose method implementation

	If (nn<0) OR (kk<0) Then Msgbox("Invalid negative parameter in Choose()..")
	If (nn<kk) Then Return 0
	If (nn=kk) Then Return 1

	Delta = 0
	iMax = 0

	If (kk < (nn-kk)) Then 'choose(100,3)
		Delta = nn-kk
		iMax = kk
	Else 'choose(100,97)
		Delta = kk
		iMax = nn-kk
	End If

	Ans = Delta+1
	For i = 2 To iMax
		Ans = (Ans * (Delta + i))/i
	Next
	Return Ans
End Sub
'******************************

'public Combination Successor()
'{
'  If (this.data.Length == 0 ||
'      this.data[0] == this.n - this.k)
'    Return null;




