Sum of Modulo Sums

Given an integer n > 9, for each possible insertion between digits in that integer, insert an addition + and evaluate. Then, take the original number modulo those results. Output the sum total of these operations.

An example with n = 47852:

47852 % (4785+2) = 4769
47852 % (478+52) =  152
47852 % (47+852) =  205
47852 % (4+7852) =  716
-----
5842

Input

A single positive integer in any convenient format, n > 9.

Output

The single integer output following the above construction technique.

Rules

• You don't need to worry about input larger than your language's default type.
• Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
• Standard loopholes are forbidden.
• This is so all usual golfing rules apply, and the shortest code (in bytes) wins.

Examples

47852 -> 5842
13 -> 1
111 -> 6
12345 -> 2097
54321 -> 8331
3729105472 -> 505598476

05AB1E, 12 10 bytes

v¹¹N¹‚£O%O

Uses the CP-1252 encoding. Try it online!

• nice, didn't reload and posted D.s¨s.p¨R+¹s%O without seeing this ;P – Magic Octopus Urn Dec 9 '16 at 15:39

JavaScript, 43 47 bytes

f=
n=>eval(n.replace(/./g,'+'+n+"%($+ +'$&$'')")) I.oninput=_=>O.value=f(I.value) <input id=I> <input id=O disabled> Takes input as string. Edit: +4 bytes: Leading zeroes in JavaScript converts the number to octal ): • That snippet is pretty neat, seeing it update in real-time like that. – AdmBorkBork Dec 9 '16 at 15:05 • Can you save a byte by doing (+'$&$''+$)? – Neil Dec 9 '16 at 16:48
• @Neil. In the first iteration $ is empty, and it will throw error trying to eval (13+) (as example). – Washington Guedes Dec 9 '16 at 17:37 Brachylog, 20 bytes :{$@~c#C:@$a+:?r%}f+ Try it online! Explanation This implements the formula given. The only thing we have to be careful about is when a 0 is in the middle of the input: in that case Brachylog gets pretty quirky, for example it won't accept that a list of integers starting with a 0 can be concatenated into an integer (which would require ignoring the leading 0 — this is mainly programmed that way to avoid infinite loops). Therefore to circumvent that problem, we convert the input to a string and then convert back all splitted inputs into integers. Example Input: 47852 :{ }f Find all outputs of that predicate: [716,205,152,4769]$@                     Integer to String: "47852"
~c#C                 #C is a list of two strings which when concatenated yield the Input
e.g. ["47","852"]. Leave choice points for all possibilities.
G-2
\d+|,
$* (1+);\1* 1 Not exactly efficient... Try it online! (The first line enables a linefeed-separated test suite.) Explanation \B ,$';$_¶$

Between every pair of characters, we insert a comma, everything in front of the match, a semicolon, the entire input, a linefeed, and everything after the match. For input 12345 this gives us:

1,2345;12345
12,345;12345
123,45;12345
1234,5;12345
12345

I.e. every possible splitting of the input along with a pair of the input. We don't need that last line though so:

G-2

\d+|,
$* This replaces each number as well as the comma with its unary representation. Since the comma isn't a number, it's treated as zero and simply removed. This adds the two parts in each splitting. (1+);\1* This computes the modulo by removing all copies of the first number from the second number. 1 That's it, we simply count how many 1s are left in the string and print that as the result. Pyth, 14 bytes s.e%QssMcQ]k A program that takes input of an integer and prints the result. Test suite How it works s.e%QssMcQ]k Program. Input: Q s.e%QssMcQ]kQ Implicit input fill .e Q Map over str(Q) with k as 0-indexed index: cQ]k Split str(Q) into two parts at index k sM Convert both elements to integers s Sum %Q Q % that s Sum Implicitly print Haskell, 62 bytes f x|m<-mod x=sum[m$div x(10^k)+m(10^k)|(k,_)<-zip[0..]$show x] Defines a function f. See it pass all test cases. Perl 6, 33 bytes {sum$_ X%m:ex/^(.+)(.+)$/».sum} Expanded: { # bare block lambda with implicit parameter ｢$_｣

sum

$_ # the input X% # cross modulus m :exhaustive / # all possible ways to segment the input ^ (.+) (.+)$
/».sum         # sum the pairs
}

Mathematica, 75 bytes

Tr@ReplaceList[IntegerDigits@#,{a__,b__}:>Mod[#,FromDigits/@({a}+{b}+{})]]&

This uses pattern matching on the list of digits to extract all partitions of them into two parts. Each such partition into a and b is then replaced with

Mod[#,FromDigits/@({a}+{b}+{})]

The notable thing here is that sums of lists of unequal length remain unevaluated, so e.g. if a is 1,2 and b is 3,4,5 then we first replace this with {1,2} + {3,4,5} + {}. The last term is there to ensure that it still remains unevaluated when we evenly split an even number of digits. Now the Map operation in Mathematica is sufficiently generalised that it works with any kind of expression, not just lists. So if we map FromDigits over this sum, it will turn each of those lists back into a number. At that point, the expression is a sum of integers, which now gets evaluated. This saves a byte over the more conventional solution Tr[FromDigits/@{{a},{b}}] which converts the two lists first and then sums up the result.

Actually, 16 15 bytes

Golfing suggestions welcome! Try it online!

Edit: -1 byte thanks to Teal pelican.

;╗$lr╤╜d+╜%MΣ Ungolfing Implicit input n. ;╗ Save a copy of n to register 0.$l       Yield the number of digits the number has, len_digits.
r        Yield the range from 0 to len_digits - 1, inclusive.
...M   Map the following function over that range, with variable x.
╤        Yield 10**x.
╜        Push a copy of n from register 0.
d        Push divmod(n, 10**x).
+        Add the div to the mod.
╜        Push a copy of n from register 0.
%        Vectorized modulo n % x, where x is a member of parition_sums.
This function will yield a list of modulos.
Σ        Sum the results together.
Implicit return.
• If you move ╜% inside the functions section you don't need to use the ♀ and it'll save you 1 byte :D (;╗$lr╤╜d+╜%MΣ) – Teal pelican Dec 12 '16 at 15:46 • @Tealpelican Thanks for the tip :D Let me know if you come up with any other golfing suggestions – Sherlock9 Dec 12 '16 at 16:06 Ruby, 64 bytes ->s{s.size.times.reduce{|a,i|a+s.to_i%eval(s[0,i]+?++s[i..-1])}} Takes the input as a string • Unfortunately, Ruby interprets integer literals beginning with 0 as octal, meaning this fails for the last test case. Here's a 78-byte solution addressing that. – benj2240 Feb 16 '18 at 3:14 Befunge, 101 96 bytes &10v #v_00p:::v>+%\00g1+:9 *v$v+55g00</|_\55+
\<$>>\1-:v ^<^!:-1 +^+^*+55\_$%\00g55
.@>+++++++

Try it online!

Explanation

&              Read n from stdin.
100p           Initialise the current digit number to 1.

-- The main loop starts here --

:::            Make several duplicates of n for later manipulation.

v+55g00<       Starting top right, and ending bottom right, this
>>\1-:v          routine calculates 10 to the power of the

Try it online!

Java 8, 127 66 bytes

n->{long m=n,r=0,p=1;for(;m>9;r+=n%((m/=10)+n%(p*=10)));return r;}

-61 bytes by creating a port of @adrianmp's C# answer.

Try it here.

Try it online!

Japt, 11 10 bytes

¬x@%OvUi+Y

Try it

Explanation

:Implicit input of string U
¬              :Split to an array of characters
@            :Pass each character at index Y through a function
Ui+Y     :  Insert a + in U at index Y
Ov         :  Evaluate as Japt
%           :  Modulo U by the above
x             :Reduce by addition
• This was marked as low quality :P – Christopher Jan 29 '18 at 17:51