# Decompose a number into a sum of repdigits

Repdigits are numbers of the form a * (10^n - 1)/9 with a in [-9,9]\{0} (in other words 111, -3333, 66, numbers which are made by repeating only one digit)

Goal: Write a program or function which takes a single positive integer N and prints N = s_1 + s_2 + ... + s_k. There should be one number per line and the numbers need to be right-aligned. No two summands should have the same number of digits and adding zeros is not allowed. The output should be ordered in ascending or descending order (by number of digits)

Examples:

in:
24192
out:
24192 =
22222 +
2222 -
222 -
33 +
3

in:
24192
out:
-7
-22
+888
+1111
+22222
=24192

in:
113
out:
113=    NOT  111+  no empty lines  111+
111+          00+
2            2                     2


As you can see there can be multiple solutions and some artistic freedom is allowed. Leading and trailing whitespace in each line is allowed

Shortest byte count wins

• Should you declare some rules to prevent e.g. just printing N ones? Commented Oct 28, 2014 at 14:18
• This is covered already: "No two summands should have the same number of digits" Commented Oct 28, 2014 at 14:53
• These numbers are also called repdigits. Commented Oct 28, 2014 at 16:18

# perl 5 - 97 92 93 86

$x=$_=pop;{printf"%15s
",$_;$_=$x,s!\d!//,$&!eg,$x-=$_,$i++?s/^\b/+/:s/^/=/;/0/||redo}  Input given as a parameter: $perl a.pl 2224192
2224192
=2222222
+1111
+888
-22
-7

• It doesn't print anything if the number has a 0 in it. Maybe it's the /0/ looping condition. Commented Oct 30, 2014 at 0:15
• Thanks, indeed that is the case. I confused myself with the perl shortcut loops. They have condition at the end but still check it at the first iteration. I have to look for "+0" then. Commented Oct 30, 2014 at 6:58
• Actually I can simulate do ... while() with just one extra character using redo. Commented Oct 30, 2014 at 9:55

## CJam, 55 50 bytes

'=l:L+Li{_W>"-+"=1$zs(\,)*+:Ii-L,_S*I+\~>\}h;]W%N*  Test it here. Uses the output format  -7 -22 +888 +1111 +2222222 =2224192  I might golf this more once I'm beaten. Explanation: '=l:L+Li{_W>"-+"=1$zs(\,)*+:Ii-L,_S*I+\~>\}h;]W%N*
'=                                                 "Push = character.";
l:L                                              "Read STDIN and store in L.";
+L                                            "Concatenate, push new copy of L.";
i                                           "Convert to integer.";
{                                 }h       "Do-while loop. Leaves the condition on the
stack. I will use the remainder for that.";
_W>                                       "Duplicate remainder, compare with -1.";
"-+"=                                  "Select appropriate sign character.";
1$"Copy remainder again."; zs "Take abs() and convert to string."; ( "Shift off first digit."; \ "Swap with string."; , "Get length."; ) "Increment."; * "Repeat digit that often."; + "Concatenate with sign."; :I "Store in I."; i- "Convert to integer. Subtract from remainder."; "Now we'll right-justify I."; L, "Load input, get length."; _ "Duplicate."; S* "Repeat space that often."; I+ "Load string and concatenate."; \~ "Swap with length. Bitwise complement."; > "Take that many characters from the right."; \ "Swap with remainder."; ; "Discard final remainder (0)."; ] "Wrap in array."; W% "Reverse."; N* "Join with line feeds.";  The resulting array is printed automatically at the end of the program. # JavaScript ES6 - 145 i=0;f=n=>{return g=n<0,n=Math.abs(n)+'',l=n.length,r=l-1?n[0].repeat(l):n,(i>0?g?'-':'+':n+'=')+'\n'+' '.repeat(i++)+(l-1?r+f((+n-r)*(g?-1:1)):r)}  Paste into Firefox console. Run as f(24192). Output for f(24192): 24192= 22222+ 1111+ 888- 22- 7  • With the input 55 it includes a 0 in the sum (which is a bug). Commented Oct 30, 2014 at 0:29 ## GolfScript 77 ~.[{..0>2*(.@*.,\[0=]*''+~*.@\-.}do;]-1%{[.0>' +'=\n].1=,2$,\-' '*\+}%'='@
`