Message Boards Message Boards

0
|
3676 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Partitioning a number into integer partition lengths.

Posted 12 years ago
Lets say I have a number n = 3245 and I want to split the number in to all integer partition lengths like:- ( I may wish to extend this to numbers greater than 30 digits).
{3, 245}, {32, 45}, {324, 5}, {3, 2, 45}, {32, 4, 5}, {3, 2, 4, 5}.
I have devised a way to determine the partitions like so.
a = IntegerPartitions[4]; b = Reverse /@ a; c =
Select[Sort[Join[a, b]], Length[#] > 1 &] // DeleteDuplicates

{{1, 3}, {2, 2}, {3, 1}, {1, 1, 2}, {2, 1, 1}, {1, 1, 1, 1}}
However, I can' t see how to map those partitions to the actual number,  I have tried using strings but there doesn' t seem to be a function to split a string by its position.
Any help or ideas would be appreciated.

Paul.
POSTED BY: Paul Cleary
2 Replies
Credit for this approach goes to Arnoud Buzing:
In[4]:= Map[FromDigits, MapThread[IntegerDigits[n][[#1 ;; #2]] &,
                 Module[{a = Accumulate[#]}, {a - # + 1, a}]] & /@ c, {2}]

Out[4]= {{3, 245}, {32, 45}, {324, 5}, {3, 2, 45}, {32, 4, 5}, {3, 2, 4, 5}}
Can be written more concisely in terms of the following undocumented function
?Internal`PartitionRagged
for example, assuming we also want to get {3245} and {3, 24, 5}:
part[n_Integer] := With[{l = IntegerDigits[n]},
  Map[FromDigits, Internal`PartitionRagged[l, #] & /@ Join @@ (Permutations /@ IntegerPartitions[Length[l]]), {2}]]

In[6]:= part[3245]

Out[6]= {{3245}, {324, 5}, {3, 245}, {32, 45}, {32, 4, 5}, {3, 24, 5}, {3, 2, 45}, {3, 2, 4, 5}}
POSTED BY: Ilian Gachevski
Posted 12 years ago
Thank you Ilian, that's perfect, the second example was also what I really needed.

Paul.
POSTED BY: Paul Cleary
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract