# Sum of multiples of powers of ten of an integer [duplicate]

Write a function to produce, for a non-negative integer, a string containing a sum of terms according to the following:

3 -> "3"

805 -> "800 + 5"

700390 -> "700000 + 300 + 90"

0 -> "0"

13 -> "10 + 3"

200 -> "200"


Any input other than a non-negative integer can be ignored. Zero terms must be skipped unless the input is 0. Terms must go from largest to smallest. The plus signs must have only a single space on either side.

This is code golf; fewest number of bytes wins.

## marked as duplicate by Jo King code-golf StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Jul 7 at 2:52

• The edge case of an input of zero seems awkward and yet easily avoidable with "for a positive integer...". – Jonathan Allan Jul 7 at 2:11
• Thank you so much for detecting the inconsistency. Regarding the +, I'd like to see how people can golf the join, so it was intentional. I say "nonnegative" instead of "positive" on purpose as well. Nonnegative includes 0 which will indeed make the code longer and IMHO more of a challenge to golf. It's true that a list of positive integers (filtered to remove zeros) is way easier, but I did add the two complications as a challenge. I don't use this site too often so if these kinds of complications are frowned upon please let me know for sure. Thanks. – Ray Toal Jul 7 at 2:16
• ...not really frowned upon, just seems like the core of the challenge on its own would make for a better question. Are the single spaces required or optional? – Jonathan Allan Jul 7 at 2:21
• Required. Though I think I have an idea of how to ask next time. :) Cheers :) – Ray Toal Jul 7 at 2:26
• Thanks good to know. For next time. – Ray Toal Jul 7 at 2:39

# Jelly, 14 bytes

DḊƬḌINḟ0ȯ0j”+K


A full program which prints the result

Try it online!

### How?

DḊƬḌINḟ0ȯ0j”+K - Main Link: integer, n       e.g. 805
D              - to base ten                      [8,0,5]
Ƭ            - collect until a fixed point:
Ḋ             -   dequeue                        [[8,0,5],[0,5],[5],[]]
Ḍ           - from base ten (vectorises)       [ 805,    5,    5,  0]
I          - incremental differences          [    -800,   0,   -5 ]
N         - negate                           [     800,   0,    5 ]
ḟ0       - filter discard zeros             [     800,         5 ]
ȯ0     - OR zero (replacing an empty list with 0)
j”+  - join with '+' characters         [     800,  '+',   5 ]
K - join with space characters       [800,' ','+',' ',5]
- implicit, smashing print         "800 + 5"


# Perl 5-p, 53 bytes

$&&&push@a,$&.y//0/cr while s/.//;$"=" + ";$_="@a"||0


Try it online!

# APL (Dyalog Unicode), 31 bytesSBCS

Anonymous tacit prefix function, taking the number as a string.

(1↓∘∊1@1∘×⊆⌽∘⍳∘≢('+',10⊥↑)¨⊢)⍎¨


Try it online!

⍎¨ execute each character (this gives the digits as numbers)

() apply the following anonymous tacit prefix function to that:

…()¨⊢ apply the below function between each element and the corresponding element of…

⌽ the reversal
∘ of
⍳ the ɩndices (1…N)
∘ of
≢ the tally of digits

↑ take that many elements, padding with zeros (as there's only ever one)

10⊥ evaluate that in base ten

'+', prepend a plus

…⊆ partition it beginning a new segment where indicated by:

1@1 a one at the first element
∘ of
× the sign (0 for 0; 1 for all other)

1↓ drop the first
∘ of
∊ the ϵnlisted (flattened) data