Results 1 to 7 of 7

Arrange balls in bins

Threaded View

  1. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: A very challenging programming combinatorics problem.

    97 - 1 - 1 - 1
    ...
    and so on till 1 - 1 - 1 - 97
    ...
    (not permutations)
    1-1-1-97 is a permutation of 97-1-1-1 so this routine would return only one of them, not both.

    Perhaps this will do what you want.
    I also think that it might be wise for you (or one of the mods) to remove the email address from your post.
    Note that the shape of ElementsSummingTo(100,4) is 4 rows by 7153 columns.

    Sub test()
        Dim myAns As Variant
        
        myAns = ElementsSummingTo(100, 4)
        
        Range("A1").Resize(UBound(myAns, 2), UBound(myAns, 1)).Value = Application.Transpose(myAns)
    End Sub
    
    
    Function ElementsSummingTo(ByVal SumOfElements As Long, ByVal NumberOfElements As Long) As Variant
        Dim Result As Variant
        Dim subResult As Variant
        Dim CurrentHigh As Long
        Dim flag As Boolean
        Dim i As Long, j As Long, Pointer As Long
        
        ReDim Result(1 To NumberOfElements, 1 To 1)
        
        If NumberOfElements = 1 Then
            Result(1, 1) = SumOfElements
        Else
            CurrentHigh = SumOfElements - (NumberOfElements - 1)
            Pointer = 0
            
            Do
                subResult = ElementsSummingTo(SumOfElements - CurrentHigh, NumberOfElements - 1)
                flag = False
        
                    For i = 1 To UBound(subResult, 2)
                        If subResult(1, i) <= CurrentHigh Then
                            flag = True
                            
                            Pointer = Pointer + 1
                            If UBound(Result, 2) < Pointer Then ReDim Preserve Result(1 To NumberOfElements, 1 To 2 * Pointer)
                            
                            Result(1, Pointer) = CurrentHigh
                            For j = 2 To NumberOfElements
                                Result(j, Pointer) = subResult(j - 1, i)
                            Next j
                            
                        End If
                    Next i
    
                CurrentHigh = CurrentHigh - 1
            Loop While flag
    
            ReDim Preserve Result(1 To NumberOfElements, 1 To Pointer)
        End If
        
        ElementsSummingTo = Result
    End Function
    Last edited by mikerickson; 12-24-2015 at 07:11 AM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Challenging Problem
    By Raiju02 in forum Excel General
    Replies: 3
    Last Post: 12-19-2011, 10:58 AM
  2. Really challenging problem
    By excelfool1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-08-2011, 10:23 PM
  3. combinatorics with words
    By jrtaylor in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-12-2011, 05:45 PM
  4. Combinatorics Question
    By wondering2 in forum Excel General
    Replies: 3
    Last Post: 11-15-2010, 02:01 AM
  5. Data filtering problem - challenging!
    By Cumberland in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-02-2007, 04:00 AM
  6. Challenging Problem
    By Naji in forum Excel General
    Replies: 1
    Last Post: 01-11-2006, 01:10 PM
  7. Challenging Graph problem!
    By Qwerty in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 12-13-2005, 11:54 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1