22
\$\begingroup\$

Let’s take a positive integer such as 123. We define the shifted auto-sum of this integer as follows:

  • 123 has 3 digits. We thus consider 3 copies of 123.

  • We stack each copy on top of each other, shifted by 1 digit each time:

    123
     123
      123
    
  • We pad each copy with 0s (excluding the last one) to get a standard column addition:

    12300
     1230
      123
    -----
    13653
    

Test cases

Input                Shifted auto-sum

1                    1
12                   132
100                  11100
123                  13653
6789                 7542579
4815162342           5350180379464981962

Scoring

This is , so the shortest answer in bytes wins.

\$\endgroup\$
0

45 Answers 45

1
2
3
\$\begingroup\$

Ruby, 40 36 bytes

p [*0...gets.size].sum{eval$_+?0*_1}

Attempt This Online!

-4 bytes thanks to Steffan

\$\endgroup\$
1
  • 2
    \$\begingroup\$ 36 bytes: p [*0...gets.size].sum{eval$_+?0*_1} \$\endgroup\$
    – naffetS
    Aug 25, 2022 at 17:46
3
\$\begingroup\$

><>, 25 bytes

::a(?va,{:@a*}!
=?n+0>~l1

Try it online

Explanation

:             !   # save a copy of the input on the stack
 :a(?v            # if the current number is lower than 10 move to next row
      a,          # else divide the current number by 10
        {:@a*}    # and store a copy of the original number * 10 on the stack

     >~           # discard the current number
=?n    l1         # if there is only one item on the stack, print it
   +0             # else add the top 2 numbers, and push a 0 to be discarded
\$\endgroup\$
3
\$\begingroup\$

GolfScript, 9 bytes

.,1`*~\~*

Try it online!

There's clearly a better way to do this, only I haven't discovered it.

\$\endgroup\$
3
\$\begingroup\$

J, 12 11 bytes

10#.]#~$@":

Try it online!

-1 byte thanks to rdm

\$\endgroup\$
1
  • \$\begingroup\$ Here, $@": could replace [:$": \$\endgroup\$
    – rdm
    Nov 15, 2022 at 17:53
2
\$\begingroup\$

Vyxal, 4 bytes

ẏ↵*∑

Try it Online!

ẏ    # Exclusive range 0-length
 ↵   # 10 to the power of each of those
  *  # Multiply by the input
   ∑ # Sum
\$\endgroup\$
2
\$\begingroup\$

MathGolf, 5 bytes

hª*y*

Try it online.

Explanation:

h      # Push the length of the (implicit) input-integer (without popping)
 ª*    # Push a list of the length amount of 1s:
 ª     #  Push [1]
  *    #  Python-style multiply it to the length
   y   # Join it together and implicitly convert it to an integer
    *  # Multiply it to the input
       # (after which the entire stack is output implicitly as result)
\$\endgroup\$
2
\$\begingroup\$

Factor, 33 bytes

[ dup log10 1 /i 1 + 10^ 9 /i * ]

Try it online!

       ! 123
dup    ! 123 123
log10  ! 123 2.089905111439398
1      ! 123 2.089905111439398 1
/i     ! 123 2
1      ! 123 2 1
+      ! 123 3
10^    ! 123 1000
9      ! 123 1000 9
/i     ! 123 111
*      ! 13653
\$\endgroup\$
2
\$\begingroup\$

PARI/GP, 21 bytes

n->n*(10^#Str(n)-1)/9

Attempt This Online!


PARI/GP, 29 bytes

f(n,m=n)=if(n,10*f(n\10,m)+m)

Attempt This Online!


PARI/GP, 31 bytes

n->fromdigits([n|i<-digits(n)])

Attempt This Online!

\$\endgroup\$
2
\$\begingroup\$

BQN, 27 bytes

{𝕩≤𝕨÷10?(𝕨×𝕩)+𝕨𝕊𝕩×10;𝕨×𝕩}⟜1

Try it at BQN REPL

Recursive function to try to circumvent the lack of simple base-conversion, string-conversion or logarithm functions in BQN.

{𝕩≤𝕨÷10?(𝕨×𝕩)+𝕨𝕊𝕩×10;𝕨×𝕩}⟜1     # recursive function with input 𝕨 
                         ⟜1     #  and second argument 𝕩 initiall set to 1
 𝕩≤𝕨÷10?                        # is 𝕩 less than or equal to 𝕨÷10?
                                # if yes:
        (𝕨×𝕩)                   #  return 𝕨×𝕩
             +                  #  plus
              𝕨𝕊𝕩×10            #  the result of a recursive call
                                #   with 𝕩 multiplied by 10
                    ;           # otherwise:
                     𝕨×𝕩        #  just return 𝕨×𝕩
\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can do a logarithm in BQN by undoing the Power function (but be aware of floating-point errors). There's also •math.Log10 for this specific case, which does appear to have exact results when given a power of 10. \$\endgroup\$
    – DLosc
    Jul 26, 2022 at 18:39
  • \$\begingroup\$ @DLosc - Aha! Good point - thank you... now to see if using this can reduce the byte-count.... to work.... \$\endgroup\$ Jul 26, 2022 at 18:53
2
\$\begingroup\$

Raku, 13 bytes

{$_*S:g/./1/}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

C++ (gcc), 42 bytes

[](int&n){int i=1;for(;n/i;)i*=10;n*=i/9;}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Prolog (SWI), 40 bytes

X+N:-N is(10^(floor(log10(X))+1)-1)*X/9.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

ECMAScript 2016, 73 bytes

n=>Array(String(n).length).fill(n).map((a,b)=>a*10**b).reduce((a,b)=>a+b)
\$\endgroup\$
1
\$\begingroup\$

PowerShell Core, 46 bytes

"$(1..($s="$args").Length|%{"+$s";$s+=0})"|iex

Try it online!

For example for 123:
We generate an array containing: "+123","+1230""+12300"
Then we join them all in a space separated string "+123 +1230 +12300"
Then we execute it as a PowerShell expression, returning 13653

\$\endgroup\$
1
\$\begingroup\$

Thunno 2 S, 3 bytes

Że×

Try it online!

Explanation

Że×  # Implicit input
Ż    # Push [0..len(input))
 e   # Take 10 ** each
  ×  # Multiply by input
     # Auto-sum and output
\$\endgroup\$
1
2

Your Answer

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

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