9
\$\begingroup\$

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

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

4 Answers 4

6
\$\begingroup\$

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
\$\endgroup\$
3
  • \$\begingroup\$ It doesn't print anything if the number has a 0 in it. Maybe it's the /0/ looping condition. \$\endgroup\$
    – feersum
    Oct 30, 2014 at 0:15
  • \$\begingroup\$ 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. \$\endgroup\$
    – nutki
    Oct 30, 2014 at 6:58
  • \$\begingroup\$ Actually I can simulate do ... while() with just one extra character using redo. \$\endgroup\$
    – nutki
    Oct 30, 2014 at 9:55
2
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ With the input 55 it includes a 0 in the sum (which is a bug). \$\endgroup\$
    – feersum
    Oct 30, 2014 at 0:29
0
\$\begingroup\$

GolfScript 77

~.[{..0>2*(.@*`.,\[0=]*''+~*.@\-.}do;]-1%{[.0>' +'=\`n].1=,2$`,\-' '*\+}%'='@

Online demo links:

A slightly more readable version (if GolfScript can be called readable) with unit tests version is available here.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.