1

i created a code in VBA to calculate the amount of combinations for stirling numbers of second kind. But in following example only half of the values are correct.


The result should be 1,7,6,1 if n is equal to 4. (Wikipedia stirling numbers

I get 1,7,6.5,4.16

Sub stirlingerzahlen()
Dim n As Integer
Dim sum As Double
Dim subsum As Double
Dim k As Long
Dim j As Long
n = 4

For k = 1 To n Step 1
    For j = 0 To k Step 1
       subsum = 1 / Application.WorksheetFunction.Fact(k) * (-1) ^ (k - j) * Application.WorksheetFunction.Fact(k) / Application.WorksheetFunction.Fact(j) * j ^ n
        sum = sum + subsum
    Next
    Sheets("Tabelle2").Cells(k, 1) = sum
    sum = 0
Next
End Sub

Can someone find the mistake?

  • 1
    Your binomial coefficient calculation is incorrect. Also, with care, you can do this in integer arithmetic. – Bathsheba Mar 17 '15 at 13:03
  • 1
    Thank you for your reply. I am not really into math or coding. I want to solve a logistics question. An approach how to continue or how to modify my code would be great. – Oliver Voll Mar 17 '15 at 13:27

1 Answers1

1

There is another version of the formula which seems to be easier to implement:

http://home.mathematik.uni-freiburg.de/junker/ss10/DAS-SS10.pdf (Page 13)

enter image description here

And here the updated code:

Sub stirlingerzahlen()
Dim n As Integer
Dim sum As Double
Dim subsum As Double
Dim k As Long
Dim j As Long
n = 4

For k = 1 To n Step 1
    For j = 0 To k
        subsum = (((-1) ^ (k - j)) * ((j ^ n) / (Application.WorksheetFunction.Fact(j) * Application.WorksheetFunction.Fact(k - j))))
        sum = sum + subsum
    Next
    Sheets("Tabelle2").Cells(k, 1) = sum
    sum = 0
Next

End Sub
JanTheGun
  • 2,165
  • 17
  • 25
  • Thanks a lot. It works fine. Do you have any idea how to start the code if i want to calculate all the combinations and show them in excel. For example: { a,b,c} {d }; { a,b,d} {c }; {a,b} {c,d} ? – Oliver Voll Mar 17 '15 at 18:22
  • Take a look at following answer: http://stackoverflow.com/a/22128114/2507819 The paper of Michael Orlov describes how to write an algorithm for that. Also there is a link to a java class "SpecialPartitionGenerator" which does exactly what you need. Just try to understand it and then you can easily convert it to VBA. If you experience any problems just open a new question. PS: it would be nice if you flag my answer as "accepted" as it's working for you ;-) – JanTheGun Mar 18 '15 at 09:32
  • Both of your answers helped me a lot. I will ask a new question how to Convert the C++ to VBA because I am not into this language. – Oliver Voll Mar 19 '15 at 10:03
  • Careful! The formula given above will give incorrect results if you try to implement it using integer (floor) division. See [here](https://math.stackexchange.com/a/46844/7133) for a formula that works using integer math. An equivalent formula is [here](https://en.wikipedia.org/wiki/Stirling_numbers_of_the_second_kind#Definition). (It's important to keep the k! divisor in those formulas outside of the sum.) – dubiousjim Apr 19 '18 at 01:52