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

39 Answers 39

9
\$\begingroup\$

Japt, 3 bytes

ì£U

Try it

Found a 3 that works with numbers.

ì£U
ì      Convert to base-10 array of digits, do ... and convert back:
 £     Map over the array with:
  U    Return the original input

Japt, 4 bytes

*sç1

Try it

*sç1
*       Multiply the input with
 s      Transform the input as a string:
  ç1    Replace each char by 1

If it is allowed to take the number as a string:

Japt, 3 bytes

*ç1

Try it

*ç1
*      Multiply the input with (coercing both sides to number)
 ç1    Replace each char by 1
\$\endgroup\$
8
\$\begingroup\$

Jelly, 4 3 bytes

-1 byte thanks to Unrelated String.

ṁDḌ

Try it online!

A port of my PARI/GP answer.

 D     # To digits
ṁ      # Fill with the input
  Ḍ    # From digits
\$\endgroup\$
1
7
\$\begingroup\$

Haskell, 23 bytes

f n=n*read('1'<$show n)

Try it online!

Multiplies n by the number obtained from replacing each of its digits with a 1. Same length pointfree:

(*)<*>read.('1'<$).show

Try it online!

\$\endgroup\$
7
\$\begingroup\$

R, 21 bytes

\(x)x*10^nchar(x)%/%9

Attempt This Online!

Uses an 'x multiplied by 111' approach, where the '111' is constructed as the next power-of-10 greater-or-equal to x, integer-divided by 9.

(now reading the other answers: this is the same approach already used in xnor's Python answer)

\$\endgroup\$
6
\$\begingroup\$

Python 2, 25 bytes

lambda n:10**len(`n`)/9*n

Try it online!

\$\endgroup\$
5
\$\begingroup\$

JavaScript (Node.js), 27 bytes

n=>(n+'').replace(/./g,1)*n

Try it online!

It is shorter than

n=>n*(g=i=>i<n?g(i+9):i/9)`` // 28 bytes
n=>~~(.1**~Math.log10(n)/9)*n // 29 bytes
n=>(10**-~Math.log10(n)-1)/9*n // 30 bytes
\$\endgroup\$
4
  • \$\begingroup\$ I suspect there might be a clever recursive solution... \$\endgroup\$
    – emanresu A
    Jul 26 at 9:09
  • \$\begingroup\$ @emanresuA Best recursive function I can get is 33 bytes. Even worse than the Math.log10 ones. n=>(g=i=>i+n<[n]+n&&i+g(i*10))(n) \$\endgroup\$
    – tsh
    Jul 26 at 9:21
  • \$\begingroup\$ @tsh 30 bytes recursive: n=>(g=i=>i&&n+10*g(i/10|0))(n). \$\endgroup\$
    – alephalpha
    Jul 26 at 11:03
  • \$\begingroup\$ @alephalpha nice idea. and I got a 28 bytes recursive, still longer than the string replace. \$\endgroup\$
    – tsh
    Jul 27 at 2:26
5
\$\begingroup\$

Bash, 19 bytes

expr ${1//?/1} * $1

An unusual example of expr being shorter than regular $[] arithmetic expansion.

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Jelly, 5 bytes

D1€Ḍ×

Try it online!

I suspect there's a 4-byter but I can't find one.

D  Ḍ  # To digits...
 1€   # Fill with 1
    × # Multiply by original
\$\endgroup\$
4
\$\begingroup\$

Vyxal, 8 bytes

₌ẏL+↲›⌊∑

Try it Online!

(yes I know there's a 4 byter, but I liked this approach so much I posted it separately)

Kids these days with their "ten to the power of" approaches. Hasn't anyone ever heard of literal spec interpretation? :p

Takes input as a string.

Explained

₌ẏL+↲›⌊∑
₌ẏL      # Push the range [0, len(in)) and len(in)
  +      # add those together
   ↲›    # pad the input left with spaces until length for each item in that list and replace all spaces with 0s
     ⌊∑  # convert each item to int and sum
\$\endgroup\$
4
\$\begingroup\$

C (gcc), 38 36 35 bytes

m;f(n){n*=m=exp10(m=log10(n)+1)/9;}

Try it online!

Saved 2 3 bytes thanks to tsh!!!

\$\endgroup\$
3
  • \$\begingroup\$ f(n){n=n*(exp10(n=log10(n)+1)-1)/9;} At least works on TIO gcc. Though actually UB. \$\endgroup\$
    – tsh
    Jul 26 at 9:08
  • \$\begingroup\$ @tsh Most golfed C uses UB, works with gcc v11 and v13. Nice one - thanks! :D \$\endgroup\$
    – Noodle9
    Jul 26 at 9:10
  • \$\begingroup\$ 35 bytes: m;f(n){n*=m=exp10(m=log10(n)+1)/9;} \$\endgroup\$
    – tsh
    Jul 26 at 10:39
4
\$\begingroup\$

Retina, 16 bytes

.+
$.($($.&*1)**

Try it online! Link includes test cases. Explanation: $($.&*1) represents the input with its characters replaced by 1s, which is then implicitly multiplied by the original input. (Retina's multiplication is decimal times unary, so the extra * is required to convert one of the decimal numbers to unary for the multiplication, and the $.( converts the result back to decimal.)

\$\endgroup\$
2
  • \$\begingroup\$ How is it possible for this to return a value greater than the amount of memory in bytes? \$\endgroup\$
    – Deadcode
    Jul 28 at 14:13
  • 1
    \$\begingroup\$ @Deadcode Although $.( has the semantics of "length of contents", Retina 1 doesn't actually calculate the contents and then take the length. Instead, it calculates the length of each of the terms and takes the sum, using arbitrary-precision arithmetic. See github.com/m-ender/retina/wiki/… \$\endgroup\$
    – Neil
    Jul 28 at 15:17
4
\$\begingroup\$

APL (Dyalog Unicode), 7 bytes

Anonymous tacit prefix function.

10⊥⊢⊣¨⍕

Try it online!

 stringify

⊣¨ defer each digit in favour of:

 the argument

10⊥ evaluate as (carrying) base-10 digits

\$\endgroup\$
4
\$\begingroup\$

BQN, 15 bytes

10⊸×⊸+˜´⊢⊣¨•Fmt

Try it at BQN online!

Explanation

10⊸×⊸+˜´⊢⊣¨•Fmt
            •Fmt  Convert the argument number to a string (list of characters)
          ⊣¨      Replace each character with
        ⊢         The argument number
       ´          Right fold
      ˜           on this function with reversed arguments:
    ⊸+              Add the right argument to
10⊸×                The left argument times 10

Other versions

Another 15-byte solution that constructs a list of powers of 10:

+´⊢×10⋆↕∘≠∘•Fmt

A 16-byte solution that constructs a list of 10s and multiplication-scans it:

+´÷⟜10×`·10¨•Fmt

Another 16-byte solution that constructs a string of 1s and evaluates it:

⊢×·•BQN·'1'¨•Fmt
\$\endgroup\$
3
\$\begingroup\$

Python, 57 bytes

lambda n:sum(n//10for i in range(len(str(n)))if(n:=n*10))

Attempt This Online!

Python, 31 bytes

lambda n:int('1'*len(str(n)))*n

Attempt This Online!

Python, 29 bytes

Port of @xnor's answer.

lambda n:10**len(str(n))//9*n

Attempt This Online!

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

05AB1E, 5 4 bytes

$g×*

-1 byte porting emanresuA's Jelly answer

Try it online or verify all test cases.

Alternative 4-byter (port of @Bubbler's Japt answer:

gиTβ

Try it online or verify all test cases.

Explanation:

$     # Push 1 and the input
 g    # Pop the input, and push its length
  ×   # Repeat 1 that many times
   *  # Multiply it to the (implicit) input-integer
      # (after which the result is output implicitly)

g     # Push the length of the (implicit) input-integer
 и    # Repeat the (implicit) input-list that many times
  Tβ  # Convert it from a base-10 list to a base-10 integer
      # (after which the result is output implicitly)
\$\endgroup\$
2
  • \$\begingroup\$ alternative 4 bytes: SdJ* (or ādJ*, and some more equivalences) \$\endgroup\$ Jul 26 at 19:33
  • \$\begingroup\$ @CommandMaster Yeah, I came across SdJ* as well when I was trying to find a 3-byter. There are indeed a lot of 3-byters to create "1"*len(input()) in Python-pseudo-code, like ηĀJ or €dJ or S1: or €gJ. Another 4-byter based on the second answer for just the legacy version of 05AB1E is €ITβ. But yeah, no 3 byter found thus far.. \$\endgroup\$ Jul 26 at 20:58
3
\$\begingroup\$

Nibbles, 4 bytes (8 nibbles)

`@~.`p$@

Inspired by alephalpha's Jelly answer.

    `p$     # convert input to string
   .        # map function over each element (characters in string):
       @    # second argument (arg1=element, arg2=input)
            # so now we have digits-of-input copies of the input
`@          # interpret as digits in base
  ~         # 10

enter image description here

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

Charcoal, 8 bytes

I×NI×1Lθ

Try it online! Link is to verbose version of code. Works with both integer and string input. Explanation:

     1      Literal string `1`
    ×       Repeated by
       θ    First input
      L     Digit length
   I        Cast to integer
 ×          Multiplied by
  N         Input as an integer
I           Cast to string
            Implicitly print

7 bytes but only accepts numeric input in JSON format:

I↨χEIθθ

Try it online! Link is to verbose version of code. Explanation: Port of @alephalpha's Jelly answer.

     θ  Input
    I   Cast to string
   E    Map over characters
      θ Input
 ↨χ     Convert from "base 10"
I       Cast to string
        Implicitly print
\$\endgroup\$
3
\$\begingroup\$

Pip, 7 bytes

a*:1X#a

Try it online!

Explanation

Port of Bubbler's string-based Japt answer:

a*:1X#a
     #a  Length (number of digits) of cmdline argument
   1X    String of that many 1s
 *:      (Treat as a number and) Multiply by
a        Cmdline argument
\$\endgroup\$
3
\$\begingroup\$

dc, 10

?dZAr^9/*p

Try it online!

Explanation

?             # Read input
 d            # duplicate input on stack
  Z           # push count digits of input to stack
   Ar         # push 10, reverse top 2 stack elements
     ^        # calculate power of 10
      9/      # divide by 9 to get 111....1
        *     # multiply by original input
         p    # print
\$\endgroup\$
3
\$\begingroup\$

Desmos, 29 bytes

f(k)=∑_{n=0}^{logk-.5}10^nk

Try It On Desmos!

Using the formula $$n\cdot\frac{10^{length(n)}-1}9$$ is surprisingly 1 byte longer:

f(k)=k(10^{floor(logk)+1}-1)/9
\$\endgroup\$
1
  • 1
    \$\begingroup\$ was waiting for this lol \$\endgroup\$ Jul 26 at 22:42
3
\$\begingroup\$

Vyxal r, 4 bytes

Lẋ₀β

Try it Online!

Another approach.

Vyxal s, 3 bytes

ẏ↵*

Try it Online!

To get even with these 3 byters, lol.

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

Perl 5 -Minteger -pF , 12 bytes

$_*=10**@F/9

Try it online!

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

Wolfram Language (Mathematica), 27 26 bytes

#(10^IntegerLength@#-1)/9&

Try it online!

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

Regex 🐇 (PCRE2 v10.35+), 36 bytes

^(?*(\1{10}|^x{9})*x(x*$))(?*\2x+)x+

Attempt This Online!
Attempt This Online! - just the test cases (that are small enough to complete in reasonable time)

Takes its input in unary, as the length of a string of xs. Returns its output as the number of ways the regex can match. (The rabbit emoji indicates this output method. It can yield outputs bigger than the input, and is really good at multiplying.)

^                     # Anchor to start; tail = N = input number
(?*                   # Non-atomic lookahead - try all possibilities to find ones
                      # resulting in a later match. This is used to multiply the
                      # number of possible matches outside by the number of
                      # possible matches inside.
    (\1{10}|^x{9})*x  # tail -= {any power of 10}
    (x*$)             # \2 = tail = N - {the power of 10}
)
(?*                   # Another non-atomic lookahead
    \2                # tail = {the power of 10 chosen above}
    x+                # Multiply the number of possible matches by tail. This
                      # applies for each power of 10 found, and those numbers of
                      # matches will add together.
)
x+                   # Add tail possible matches, where tail==N, with the above
                     # multiplier being applied.

Regex 🐇 (Perl / PCRE), 108 76 bytes

^(x?)(?=(x*)\2$).*(?=\2((x+)\4{8}(?=\4$))*x{5}$)()?x+.*(?=\2$)(()?x+|\1+$)|x

Try it online! - Perl v5.28.2 / Attempt This Online! - Perl v5.36+
Try it online! - PCRE1
Try it online! - PCRE2 v10.33 / Attempt This Online! - PCRE2 v10.40+
Try it online! - PCRE2 - just the test cases

Without (?*...) molecular lookahead, it's much harder to do multiplication. So we reserve half of the number to use as a counter for one operand of the multiplication, letting \$a=\lfloor n/2\rfloor\$ and \$b=n\bmod 2\$, such that \$2a+b=n\$, in order to emulate operations on \$n\$.

We don't actually need to construct the number of the form 111...111 to multiply by. We can pretty much directly sum \$n\$ multiplied by each power of \$10\$, that is to say,

$$\large\left({\sum_{k=1}^{\lfloor log_{10}n\rfloor}2{{10^k}\over 2}(2a+b)}\right)+n = \sum_{k=0}^{\lfloor log_{10}n\rfloor}10^k n$$

using emulated arithmetic on \$2a+b=n\$.

    ^                         # Anchor to start; tail = N = input number
    (x?)                      # \1 = N % 2; tail -= \1
    (?=(x*)\2$)               # \2 = floor(N / 2)
    .*                        # Find any value of tail ≤ N satisfying the
                              # following:
    (?=
        # Assert that 2*(tail-\2) is a power of 10
        \2                      # tail -= \2
        (
            (x+)\4{8}(?=\4$)  # Assert tail is divisible by 10; tail /= 10
        )*                    # Iterate the above as many times as possible,
                              # minimum zero.
        x{5}$                 # Assert tail == 5
    )
    ()?                       # possibleMatches *= 2, for the following:
    x+                        # possibleMatches *= tail - \2, for the following:
    .*(?=\2$)                 # tail = \2
    (
        ()?                   # possibleMatches *= 2, for the following:
        x+                    # possibleMatches += tail
    |
        \1+$                  # possibleMatches += \1
    )
|
                              # Unanchored
    x                         # possibleMatches += N

Regex (.NET), 129 117 bytes

^(?=(x)*)(x?)(?=(x*)\3$)(((?=\3((x+)\7{8}(?=\7$))*x{5}$)(?=((?=.*(?=\3)(?<1>x)*){4}(?=.*(?=\2)(?<1>x)?){2}x)*\3))?x)*

Try it online!
Try it online! - just the test cases

Returns its output as the capture count of \1.

This is now a fairly straight port of the Perl/PCRE 🐇 version.

The former 129 byte version actually constructed the number whose digits are all 1 to multiply by – indirectly – by constructing the number \$c\$ whose every digit is a 1 and has one less digit than the input, such that \$10c+1\$ is the actually desired number, then returning \$(10c)(2a+b)+n\$ \$=(10c+1)n\$ as a capture count. That version couldn't return a result for an input of \$0\$, but this version can.

^                               # Anchor to start; tail = N = input number
(?=(x)*)                        # \1.captureCount = tail = N
(x?)                            # \2 = N % 2; tail = tail-\2 == \3 * 2
(?=(x*)\3$)                     # \3 = floor(N / 2)
(
    (
        (?=                       # Lookahead conditional
            # Assert that 2*(tail-\3) is a power of 10
            \3                      # tail -= \3
            (
                (x+)\7{8}(?=\7$)    # Assert tail is divisible by 10; tail /= 10
            )*                      # Iterate the above as many times as possible,
                                    # minimum zero.
            x{5}$                   # Assert tail == 5
        )
        # If the above assertion matches, do the following:
        (?=
            (
                (?=
                    .*(?=\3)    # tail = \3
                    (?<1>x)*    # \1.captureCount += tail
                ){4}            # Iterate the above 4 times
                (?=
                    .*(?=\2)    # tail = \2
                    (?<1>x)?    # \1.captureCount += tail
                ){2}            # Iterate the above 2 times
                x
            )*                  # Iterate the above as many times as possible,
                                # which will be a number of iterations equal to
                                # exactly half the current power of 10.
            \3                  # Assert tail >= \3
        )
    )?
    x
)*                              # Iterate the above as many times as possible,
                                # applying the payload to each power of 10

I think I may have discovered a bug in the .NET regex engine, or at least the version on TIO, while working on this. The following variant of the same length should have worked, but didn't:

^(?=(x)*)(x?)(?=(x*)\3$)((?(?=\3((x+)\6{8}(?=\6$))*x{5}$)(?=((?=.*(?=\3)(?<1>x)*){4}(?=.*(?=\2)(?<1>x)?){2}x)*\3))x)*

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

PHP, 29 bytes

fn($n)=>$n*~-10**strlen($n)/9

Try it online!

\$\endgroup\$
9
  • 1
    \$\begingroup\$ This is a code-golf challenge i.e. your code needs to be as short as possible. You can start with removing whitespace and move on to better tricks like the ones in the site's PHP thread.. Happy golfing! \$\endgroup\$
    – Razetime
    Jul 30 at 17:10
  • \$\begingroup\$ Here's a start, at 84 bytes: Try it online! \$\endgroup\$
    – Steffan
    Jul 30 at 18:29
  • \$\begingroup\$ Using a formula, though, this can be 29 bytes: Try it online! \$\endgroup\$
    – Steffan
    Jul 30 at 18:35
  • \$\begingroup\$ @Steffan thank you so much for correction :) \$\endgroup\$ Jul 30 at 19:07
  • \$\begingroup\$ @Steffan how did you convert this into just 29 bytes? i mean can you please guide me so that I can answer more questions? \$\endgroup\$ Jul 30 at 19:18
3
\$\begingroup\$

Knight, 12 bytes

O*/^10L=xP9x

Try it online!

Ungolfed:

OUTPUT (* (/ (^ 10 (LENGTH (= x PROMPT))) 9) x)
\$\endgroup\$
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\$
    – Steffan
    Aug 25 at 17:46
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\$

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.