Permutations and array limits

fboehlandt

Active Member
Joined
Sep 9, 2008
Messages
334
Hi there,
I include the following code to create permutations of binary numbers. Just an adaptation of a similar code for letters. The permutations should be given in a n*k array:

Code:
Function PermNBinomial(nDigits As Long)
Dim MyArr As Variant, vArr As Variant, lRow As Long
ReDim vArr(1 To 2 ^ nDigits)
ReDim MyArr(1 To 2 ^ nDigits, 1 To nDigits)
PermNBinomial1 MyArr, vArr, nDigits, "", lRow, 1
PermNBinomial = MyArr
End Function
Sub PermNBinomial1(MyArr As Variant, vArr As Variant, ByVal lDigits As Long, ByVal s As String, lRow As Long, ByVal lind As Long)
Dim j As Long, s1 As String
For j = 1 To 2
    s1 = IIf(lind > 1, s, "") & Chr(Asc("0") + j - 1)
    If lind = lDigits Then
        lRow = lRow + 1
        vArr(lRow) = s1
        For i = 1 To lDigits
            MyArr(lRow, i) = CInt(Right(Left(vArr(lRow), i), 1))
        Next i
    Else
        PermNBinomial1 MyArr, vArr, lDigits, s1, lRow, lind + 1
    End If
Next j
End Sub

The code works fine except when it comes to nDigits > 11. Here it just returns a #Value error in the cells and I don't know why. Is there an array size limi that is being exceeded? If so, is there a workaround? Any help is greatly appreciated. Also note that
Code:
 CInt(Right(Left(vArr(lRow), i), 1))
is my quick and dirty solution to convert the text string into a 2-dimensional array.

p.s. for nDigits =12, for example, the above function returns the permutations for up to row 257 (or 3084 cells). The function is obviously entered as an arrray function. When the array is extended beyond 257 rows, I receive the #value error
 
Last edited:

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi,

I think you'll find that you've blown the limit for a Long datatype which is -2147483648 to 2147483647
 
Upvote 0
yes, you are right. I dont' see how though. Changed the datatypes to Double and it works. Thanks for your input
 
Upvote 0
Nope, hold your horses. Still no joy. I can increase the array but not to the resuired extent. I need an arry where n* k = 4096 * 12. I don't see why the limits for either Long or Double are violated?
 
Upvote 0
Sorry, but this doesn't help at all. It is not a question of running out of available resources. Neither should it be. This routine runs very fast and should require no working memory whatsoever. Also, none of the variables seem to reach their data type limit (but I shall double-check). Lastly, the array limit surely can't be as low as 4096 * 12? If anybody knows an effective way of achieving the below I would be very grateful. What I need is the following:
(example for 3 digits)

000
001
010
100
011
101
110
111

Ideally, this should work for up to at least 12 digits.
 
Upvote 0
Apologies.

I just tried your function via the following:
Code:
Sub Test()
Dim vTest As Variant

vTest = PermNBinomial(12)
End Sub
I set a breakpoint at the End Sub, & viewed the output array, which correctly returns a 12 * 4096 array.
 
Upvote 0
Same here, UBounds are 4096 and 12, so the array is complete. It appears as if it cannot be transferred to Excel as an array function though (there appears to be some kind of limit). Luckily, I just need to pass the array from one routine to another and then do something else with it. I'll see if this works for me. Thanks for the quick reply
 
Upvote 0
This works for me:
Code:
Sub Test()
Dim vTest As Variant

vTest = PermNBinomial(12)
ActiveSheet.Range("A1:" & Cells(UBound(vTest, 1), UBound(vTest, 2)).Address).Value = vTest

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,326
Messages
6,124,272
Members
449,149
Latest member
mwdbActuary

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top