34
\$\begingroup\$

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
\$\endgroup\$

31 Answers 31

0
\$\begingroup\$

K (oK), 31 bytes

Solution:

+/(+/+.:''1_(0,'!#$x)_\:$x)!'x:

Try it online!

Example:

+/(+/+.:''1_(0,'!#$x)_\:$x)!'x:47852
5842

Explanation:

Most of the code goes to building up the lists out of the input. Am wondering if there is a trick I'm missing.

{.:''1_(0,'!#$x)_\:$x}47852
(4 7852
47 852
478 52
4785 2)

Full breakdown:

+/(+/+.:''1_(0,'!#$x)_\:$x)!'x: / the solution
                             x: / store input as variable x
                           !'   / mod (!) each-both (')
  (                       )     / do this together
                        $x      / string x
                     _\:        / cut (_) each-left (\:)
            (       )           / do this together
                  $x            / string x
                 #              / count length of the string
                !               / til, range 0..n-1
             0,'                / prepend 0 to each
          1_                    / drop the first one
      .:''                      / value (.:) each-each (nested)
     +                          / flip rows and columns
   +/                           / sum up the columns
+/                              / sum up results of the modulo
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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