PDA

View Full Version : Cant count integer partitions by recursive function.



alex009988
07-20-2019, 03:59 AM
Hi guys I want to calculate the number of integer partitions. So that I use this code

Function p(m As Integer, n As Integer)
If m = 1 Or n = 1 Then
p(m, n) = 1
Else
If n > m Then
p(m, n) = p(m, m)
Else
If m = n Then
p(m, n) = p(m, m - 1) + 1
Else
p(m, n) = p(m, n - 1) + p(m - n, n)
End If
End If
End If
End Function
Sub Integer_partitions()
Debug.Print p(5, 5)
End Sub


But I get the error as " Run time error 28 Out of stuck space".
What's wrong with my code?
Regards,
Alex

dangelor
07-20-2019, 06:30 PM
Possibly...
Function p(m As Integer, n As Integer)
If m = 1 Or n = 1 Then
p = 1
Else
If n > m Then
p = p(m, m)
Else
If m = n Then
p = p(m, m - 1) + 1
Else
p = p(m, n - 1) + p(m - n, n)
End If
End If
End If
End Function