Can we output the possible ways a number is made using a UDF?

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
358
Office Version
  1. 2003 or older
Platform
  1. Windows
The following concept at Rosetta Code got me interested Integer.

Is there a way to output a string for a given n? such as, for example,
- The integer n=5 has 7 names as “1+1+1+1+1”, “2+1+1+1”, “2+2+1”, “3+1+1”, “3+2”, “4+1”, “5”. Or,
- The integer n=4 has 5 names as “1+1+1+1”, “2+1+1”, “2+2”, “3+1”, “4”.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Those are called partitions of a number. There's a macro here that calculates them:

 
Upvote 0
@Eric W ; Thanks I could not relate it to integer partition.

On help, how to print the output to Sheet versus Immediate Widow?

VBA Code:
Sub demo()
  printPartitions 4, 4, ""
End Sub

Function printPartitions(tgt As Long, max As Long, sfx As String) As String
  If tgt = 0 Then
    Debug.Print sfx
  Else
    If max > 1 Then printPartitions tgt, max - 1, sfx
    If max <= tgt Then printPartitions tgt - max, max, max & " " & sfx
  End If
End Function
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,121
Members
453,021
Latest member
Justyna P

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