Strict Integer Partition UDF

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
349
Office Version
  1. 2003 or older
Platform
  1. Windows
I am trying to generate a function to compute strict integer partition count for a given n. I am referencing the below recursive code.

My recursive code is also attached which is not giving the output for a given n. The expected output is 1, 1, 1, 2, 2, 3, 4, 5, 6, 8 for n=1 to 10.

Rich (BB code):
def partitionsQ(n,d=0):    
#http://mathworld.wolfram.com/PartitionFunctionQ.html
#http://reference.wolfram.com/language/ref/PartitionsQ.html
#https://oeis.org/A000009
#https://codegolf.stackexchange.com/a/71945/17547    
if n==0: return 1    
return sum(partitionsQ(n-k,n-2*k+1) for k in range(1,n-d+1))

VBA Code:
Function partitionsQ(n, Optional d = 0)
    
    Dim k

    If n = 0 Then partitionsQ = 1
    
    For k = 1 To n - d + 1
        partitionsQ = partitionsQ + partitionsQ(n - k, n - 2 * k + 1)
    Next

End Function
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
To further add, the output is the coefficients of Taylor series expansion of (1x^n) ... reference - Partitions
 
Upvote 0
I'm not entirely sure of the underlying mathematics, nor is my Python good enough to be sure that the translation to VBA is correct, but this version of the UDF generates the same results as the OEIS sequence up to 20. I wouldn't take it much higher since it's a very inefficient function, and already noticeably slow at 20.

VBA Code:
Function partitionsQ(n, Optional d = 0)
Dim k As Long

    If n = 0 Then
        partitionsQ = 1
    Else
        For k = 1 To n - d
            partitionsQ = partitionsQ + partitionsQ(n - k, n - 2 * k + 1)
        Next
    End If

End Function
 
Upvote 0
I am trying to generate a function to compute strict integer partition count for a given n. I am referencing the below recursive code.

My recursive code is also attached which is not giving the output for a given n. The expected output is 1, 1, 1, 2, 2, 3, 4, 5, 6, 8 for n=1 to 10.

Hi

I believe that's the result for n=0 to 9, not n=1 to 10

Try also:

Excel Formula:
Function PartC(n As Long, Optional i As Long = 1) As Long

For i = i To (n - 1) \ 2
    PartC = PartC + PartC(n - i, i + 1)
Next i
PartC = PartC + 1
End Function
 
Upvote 0

Forum statistics

Threads
1,214,416
Messages
6,119,386
Members
448,891
Latest member
tpierce

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