Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

COMBIN

5,211 views
Skip to first unread message

olva...@my-deja.com

unread,
Jul 24, 2000, 3:00:00 AM7/24/00
to
Hello,

=COMBIN returns the number of combinations for a given number of
objects. Is there a way to show those numbers?

Regards

Osmario


Sent via Deja.com http://www.deja.com/
Before you buy.

Tushar Mehta

unread,
Jul 25, 2000, 3:00:00 AM7/25/00
to

Enumerate the entire list, one by one?

--
Regards,

Tushar Mehta
www.tushar-mehta.com
--
In <8liegk$cbm$1...@nnrp1.deja.com>, olva...@my-deja.com <olvavuka@my-
deja.com> wrote

Myrna Larson

unread,
Jul 25, 2000, 3:00:00 AM7/25/00
to

If you mean to list each of the specific combinations, I've written some
macros to do that. Let me know if you are interested in them.

olva...@my-deja.com

unread,
Jul 25, 2000, 3:00:00 AM7/25/00
to
In article <MPG.13e6e5a5...@msnews.microsoft.com>,

Tushar Mehta <ng_p...@bigfoot.com> wrote:
>
> Enumerate the entire list, one by one?
>
> --
> Regards,
>
> Tushar Mehta
> www.tushar-mehta.com

Hello!
Not the entire list. Suppose =COMBIN(60,6). I need to sum six numbers
to macht 171, AND those six numbers must be in the range =COMBIN(60;6).

Regards
Osmario.


=COMBIN(60,6)


> --
> In <8liegk$cbm$1...@nnrp1.deja.com>, olva...@my-deja.com <olvavuka@my-
> deja.com> wrote
> > Hello,
> >

> > =COMBIN returns the number of combinations for a given number of
> > objects. Is there a way to show those numbers?
> >

olva...@my-deja.com

unread,
Jul 25, 2000, 3:00:00 AM7/25/00
to
In article <lqbqnsgu0ibnql3nh...@4ax.com>,

Myrna Larson <myrna...@home.com> wrote:
>
>
> If you mean to list each of the specific combinations, I've written
some
> macros to do that. Let me know if you are interested in them.

Hi Myrna,

I'd like to.

Regards
Osmario.


>
> >> =COMBIN returns the number of combinations for a given number of
> >> objects. Is there a way to show those numbers?
>
>

Tushar Mehta

unread,
Jul 25, 2000, 3:00:00 AM7/25/00
to
[This followup was posted to microsoft.public.excel.misc and a courtesy
email copy was sent to olva...@my-deja.com <olva...@my-deja.com>.
Please use the newsgroup for further discussion.]

Is this a homework assignment of sorts? I ask because in doing some of
the boundary calculations, the numbers fell in place very conveniently.
That is what happens in homework assignments, but rarely in 'real life.'

In any case, here is a guideline. Maybe, Myrna [Larson] will post the
entire functioning code module.

Let the 6 values be I, J, K, L, M, N. There are only 5 degrees of
freedom and N is always determined by 171 - (I+J+K+L+M). Without any
loss of generality assume that I < J < K < L < M < N.

The crudest first pass would be to build 5 loops as:

for i=1 to 55
for j=i+1 to 56
for k=j+1 to 57
for l=k+1 to 58
for m=l+1 to 59
n=171-(i+j+k+l+m)
if n>0 and n <= 60 then "we have a solution"
wrap up all loops

The constraints can be tightened as follows:

Determine the largest value of I with:
Let the largest value of I be Imax.
Then the smallest value of J can be Imax+1
and the smallest value of K can be Imax+2
...
and the smallest value of N can be Imax+5
The result would be Imax + (Imax +1) + ... + (Imax +5) = 171
or
6*Imax+15=171 or Imax=26.
>> This is what makes me believe that the problem is an assignment <<

Determine the max. values of J through M in similar fashion. For
example, for any given value of I, the limit now is not 171 but (171-I)
and the largest value of J is given by the equation 5*Jmax+10=(171-I).
If the result is not an integer, use the TRUNC value.

Determining all the upper limits will speeden up the loop dramatically
since, for example, the i loop is now i=1..26 rather than i=1..55, or a
reduction of over 50%! The reductions in the other loops should be of
the same magnitude.

It is also possible to determine lower limits; however, the process is
reversed in that we start at the other end, so to say.

Clearly, N is known to equal 171-(I+J+K+L+M). Given a particular
combination of I, J, K, and L, determine the lower limit of M as
follows:

The largest possible value of N is 60. Consequently, the smallest
possible value of M is 171-(I+J+K+L)-60. If this is greater than 60, no
feasible solution exists for this combination of I, J, K, and L. Skip
the M loop altogether! If it is less than L+1, use L+1

A similar process calculates the smallest possible value of L. The
largest possible values of N and M are 60 and 59 respectively.
Consequently, for any combination of I, J, and K, the smallest value of
L is given by 171-(I+J+K)-59-60. Again, if Lmin > 60, no feasible
solution exists; if Lmin<J+1, use J+1

--
Regards,

Tushar Mehta
www.tushar-mehta.com
--
In <8lkjmc$urf$1...@nnrp1.deja.com>, olva...@my-deja.com <olvavuka@my-


deja.com> wrote
> In article <MPG.13e6e5a5...@msnews.microsoft.com>,
> Tushar Mehta <ng_p...@bigfoot.com> wrote:
> >
> > Enumerate the entire list, one by one?
> >
> > --
> > Regards,
> >
> > Tushar Mehta
> > www.tushar-mehta.com
>
> Hello!
> Not the entire list. Suppose =COMBIN(60,6). I need to sum six numbers
> to macht 171, AND those six numbers must be in the range =COMBIN(60;6).
>
> Regards
> Osmario.
>
>
>
>
> =COMBIN(60,6)
> > --
> > In <8liegk$cbm$1...@nnrp1.deja.com>, olva...@my-deja.com <olvavuka@my-
> > deja.com> wrote
> > > Hello,
> > >

> > > =COMBIN returns the number of combinations for a given number of
> > > objects. Is there a way to show those numbers?
> > >

> > > Regards
> > >
> > > Osmario

Myrna Larson

unread,
Jul 25, 2000, 3:00:00 AM7/25/00
to

On Tue, 25 Jul 2000 17:47:24 GMT, olva...@my-deja.com wrote:

Now that you specify the size of the problem -- 60 items, taken 6 at a time --
my code won't work for you. =COMBIN(60,6) = 50,063,860.

My code has a restriction that the results must fit one worksheet, so it's
limited to 65,536*256 = 16,777,216 combinations (XL97/2000)

The results of your problem require almost 3 entire worksheets -- all columns
and rows filled. It would be impossibly slow to calculate with XL VBA (though
most of the time is taken by writing the data to a worksheet).

If you were to write the information in human readable form to a text file
(which is much faster), each combination would require about 19 bytes. The
file size would be more than 850 megabytes. If you printed the combinations,
at 4 per line and 60 lines per page, it would require 3,546,190 pages. If you
code it in binary, 6 bytes per combination, it still requires more than 300
megabytes, and it isn't "human readable".

Just curious, but why do you want/need to do this? Is this a lottery issue?

olva...@my-deja.com

unread,
Jul 25, 2000, 3:00:00 AM7/25/00
to
In article <ovtrns444gcnl9cj2...@4ax.com>,

Myrna Larson <myrna...@home.com> wrote:
>
> Just curious, but why do you want/need to do this? Is this a lottery
issue?
>

Thanks Myrna. Yes, it's a lottery issue.

olva...@my-deja.com

unread,
Jul 25, 2000, 3:00:00 AM7/25/00
to
In article <MPG.13e7bdd33...@msnews.microsoft.com>,
Tushar Mehta <ng_p...@bigfoot.com> wrote:

Thanks Tushar Mehta for your response

Regards
Osmario

Chip Pearson

unread,
Jul 25, 2000, 3:00:00 AM7/25/00
to

> Thanks Myrna. Yes, it's a lottery issue.

No offense, but lotteries are random. Make your life simple, and bet
1-2-3-4-5 on every ticket. Your odds are as good on that combination as
anything else. And for odds, the state lotteries are a rip off. The
expected return is 0.45 or so. Go to Vegas and play Roulette. The expected
return is about 0.97.

My father once told me, "A lottery is the perfect tax -- it is voluntary,
and it is regressive."


Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com ch...@cpearson.com


<olva...@my-deja.com> wrote in message news:8ll2mo$auu$1...@nnrp1.deja.com...


> In article <ovtrns444gcnl9cj2...@4ax.com>,
> Myrna Larson <myrna...@home.com> wrote:
> >
> > Just curious, but why do you want/need to do this? Is this a lottery
> issue?
> >
>
> Thanks Myrna. Yes, it's a lottery issue.
>

Dave Peterson

unread,
Jul 25, 2000, 3:00:00 AM7/25/00
to
another quote my cousin uses:

"A lottery is a tax on stupid people."

--

Dave Peterson
pete...@netzero.net

Myrna Larson

unread,
Jul 25, 2000, 3:00:00 AM7/25/00
to

On Tue, 25 Jul 2000 16:08:53 -0400, Tushar Mehta <ng_p...@bigfoot.com>
wrote:

>Maybe, Myrna will post the entire functioning code module

Since you asked, here it is. It is generic, i.e. it isn't written specifically
for a given population and set size, as yours it. It will do permutations or
combinations. It uses a recursive routine to generate the subsets, one routine
for combinations, a different one for permutations.

To use it, you put the letter C or P (for combinations or permutations) in a
cell. The cell below that contains the number of items in a subset. The cells
below are a list of the items that make up the population. They could be
numbers, letters and symbols, or words, etc.

You select the top cell, or the entire range and run the sub. The subsets are
written to a new sheet in the workbook.

Option Explicit

Dim vAllItems As Variant
Dim Buffer() As String
Dim BufferPtr As Long
Dim Results As Worksheet

Sub ListPermutations()
Dim Rng As Range
Dim PopSize As Integer
Dim SetSize As Integer
Dim Which As String
Dim N As Double
Const BufferSize As Long = 4096

Set Rng = Selection.Columns(1).Cells
If Rng.Cells.Count = 1 Then
Set Rng = Range(Rng, Rng.End(xlDown))
End If

PopSize = Rng.Cells.Count - 2
If PopSize < 2 Then GoTo DataError

SetSize = Rng.Cells(2).Value
If SetSize > PopSize Then GoTo DataError

Which = UCase$(Rng.Cells(1).Value)
Select Case Which
Case "C"
N = Application.WorksheetFunction.Combin(PopSize, SetSize)
Case "P"
N = Application.WorksheetFunction.Permut(PopSize, SetSize)
Case Else
GoTo DataError
End Select
If N > Cells.Count Then GoTo DataError

Application.ScreenUpdating = False

Set Results = Worksheets.Add

vAllItems = Rng.Offset(2, 0).Resize(PopSize).Value
ReDim Buffer(1 To BufferSize) As String
BufferPtr = 0

If Which = "C" Then
AddCombination PopSize, SetSize
Else
AddPermutation PopSize, SetSize
End If
vAllItems = 0

Application.ScreenUpdating = True
Exit Sub

DataError:
If N = 0 Then
Which = "Enter your data in a vertical range of at least 4 cells. " _
& String$(2, 10) _
& "Top cell must contain the letter C or P, 2nd cell is the number " _
& "of items in a subset, the cells below are the values from which " _
& "the subset is to be chosen."

Else
Which = "This requires " & Format$(N, "#,##0") & _
" cells, more than are available on the worksheet!"
End If
MsgBox Which, vbOKOnly, "DATA ERROR"
Exit Sub
End Sub

Private Sub AddPermutation(Optional PopSize As Integer = 0, _
Optional SetSize As Integer = 0, _
Optional NextMember As Integer = 0)

Static iPopSize As Integer
Static iSetSize As Integer
Static SetMembers() As Integer
Static Used() As Integer
Dim i As Integer

If PopSize <> 0 Then
iPopSize = PopSize
iSetSize = SetSize
ReDim SetMembers(1 To iSetSize) As Integer
ReDim Used(1 To iPopSize) As Integer
NextMember = 1
End If

For i = 1 To iPopSize
If Used(i) = 0 Then
SetMembers(NextMember) = i
If NextMember <> iSetSize Then
Used(i) = True
AddPermutation , , NextMember + 1
Used(i) = False
Else
SavePermutation SetMembers()
End If
End If
Next i

If NextMember = 1 Then
SavePermutation SetMembers(), True
Erase SetMembers
Erase Used
End If

End Sub 'AddPermutation

Private Sub AddCombination(Optional PopSize As Integer = 0, _
Optional SetSize As Integer = 0, _
Optional NextMember As Integer = 0, _
Optional NextItem As Integer = 0)

Static iPopSize As Integer
Static iSetSize As Integer
Static SetMembers() As Integer
Dim i As Integer

If PopSize <> 0 Then
iPopSize = PopSize
iSetSize = SetSize
ReDim SetMembers(1 To iSetSize) As Integer
NextMember = 1
NextItem = 1
End If

For i = NextItem To iPopSize
SetMembers(NextMember) = i
If NextMember <> iSetSize Then
AddCombination , , NextMember + 1, i + 1
Else
SavePermutation SetMembers()
End If
Next i

If NextMember = 1 Then
SavePermutation SetMembers(), True
Erase SetMembers
End If

End Sub 'AddCombination

Private Sub SavePermutation(ItemsChosen() As Integer, _
Optional FlushBuffer As Boolean = False)

Dim i As Integer, sValue As String
Static RowNum As Long, ColNum As Long

If RowNum = 0 Then RowNum = 1
If ColNum = 0 Then ColNum = 1

If FlushBuffer = True Or BufferPtr = UBound(Buffer()) Then
If BufferPtr > 0 Then
If (RowNum + BufferPtr - 1) > Rows.Count Then
RowNum = 1
ColNum = ColNum + 1
If ColNum > 256 Then Exit Sub
End If

Results.Cells(RowNum, ColNum).Resize(BufferPtr, 1).Value _
= Application.WorksheetFunction.Transpose(Buffer())
RowNum = RowNum + BufferPtr
End If

BufferPtr = 0
If FlushBuffer = True Then
Erase Buffer
RowNum = 0
ColNum = 0
Exit Sub
Else
ReDim Buffer(1 To UBound(Buffer))
End If

End If

'construct the next set
For i = 1 To UBound(ItemsChosen)
sValue = sValue & ", " & vAllItems(ItemsChosen(i), 1)
Next i

'and save it in the buffer
BufferPtr = BufferPtr + 1
Buffer(BufferPtr) = Mid$(sValue, 3)
End Sub 'SavePermutation


tim...@my-deja.com

unread,
Jul 26, 2000, 3:00:00 AM7/26/00
to
In article <OCNhNbo9$GA.195@cppssbbsa04>,

"Chip Pearson" <ch...@cpearson.com> wrote:
> No offense, but lotteries are random. Make your life simple, and bet
> 1-2-3-4-5 on every ticket. Your odds are as good on that combination
> as anything else.


The odds are as good, but choosing 1-2-3-4-5 is not good strategy.
There are surprisingly many people thinking like Chip, and betting on
this combination, thus sharing the possible win.

kvec...@gmail.com

unread,
Mar 31, 2013, 5:46:30 AM3/31/13
to
Beware that in the new versions of excell you have to use Cells.CountLarge instead of Cells.Count or you will get Runtime error 6: overflow

cam....@gmail.com

unread,
May 13, 2013, 4:28:31 PM5/13/13
to

> Beware that in the new versions of excell you have to use Cells.CountLarge >instead of Cells.Count or you will get Runtime error 6: overflow
>>

Note also that in Excel 2010 you will get an overflow error even using Cells.CountLarge if only the first cell of the data column is selected and the data column is empty. This can be trapped by changing lines 17-19 of the ListPermutations subroutine to:

If Rng.Cells.Count = 1 Then
Set Rng = Range(Rng, Rng.End(xlDown))
If WorksheetFunction.CountA(Rng) = 0 Then
GoTo DataError
End If
End If
0 new messages