23
\$\begingroup\$

Let me introduce you to GAU numbers

GAU(1) = 1  
GAU(2) = 1122  
GAU(3) = 1122122333  
GAU(4) = 11221223331223334444  
GAU(6) = 11221223331223334444122333444455555122333444455555666666  
...  
GAU(10) = 11221223331223334444122333444455555122333444455555666666122333444455555666666777777712233344445555566666677777778888888812233344445555566666677777778888888899999999912233344445555566666677777778888888899999999910101010101010101010  

This challenge is pretty simple!

Given an integer n>0, find the number of digits of GAU(n)

Example

Let's make GAU(4)
we take the following steps (until we get to 4) and concatenate them

[1][122][122333][1223334444]   

you must write every number as many times as its value, but you have to count every time from 1

Let's try to make GAU(5)
we will have to count from 1 to 1

[1]   

then from 1 to 2 (but repeating every number as many times as its value)

[122]     

then from 1 to 3

[122333]   

then from 1 to 4

[1223334444]    

and finally from 1 to 5 (this is the last step because we want to find GAU(5))

[122333444455555]     

Now we take all these steps and concatenate them
the result is GAU(5)

11221223331223334444122333444455555     

We are interested in the number of digits of these GAU numbers.

Test cases

Input⟼Output

n   ⟼ Length(GAU(n))

1   ⟼ 1  
2   ⟼ 4  
3   ⟼ 10  
10  ⟼ 230   
50  ⟼ 42190  
100 ⟼ 339240  
150 ⟼ 1295790  

This is a challenge.
Shortest code in bytes will win.

If you still have any questions please let me know.
I really want everyone here to understand this magic-hidden-complex pattern

\$\endgroup\$
  • 4
    \$\begingroup\$ What does GAU stand for? \$\endgroup\$ – Leaky Nun Sep 23 '17 at 12:06
  • 21
    \$\begingroup\$ G is for GAU, A and U are just there for no reason \$\endgroup\$ – user72253 Sep 23 '17 at 12:09
  • 2
    \$\begingroup\$ Up until n=9, the lengths are tetrahedral numbers, but beyond that the multi-digit numbers get in the way of a simple closed form \$\endgroup\$ – Miff Sep 25 '17 at 13:54
  • \$\begingroup\$ FYI your test case says n ⟼ Length(GUA(n)), not GAU(n). \$\endgroup\$ – numbermaniac Sep 28 '17 at 1:18
  • 2
    \$\begingroup\$ @numbermaniac thanks for spotting this. GUA numbers are totally different. They haven't been invented yet! \$\endgroup\$ – user72253 Sep 30 '17 at 19:00

45 Answers 45

14
\$\begingroup\$

SOGL V0.12, 11 10 8 7 5 bytes

∫∫l*+

Try it Here! - this expects to be called as a function with the input on the stack and the input box empty.
7 byte alternative taking the input from the input box:

0.∫∫l*+

Try it Here!

0      push 0
 .     push the input
  ∫    iterate over a range 1..POP (input) inclusive, pusing the current number
   ∫    iterate over 1..POP (above loops number) inclusive, pusing the current number
    l    push that numbers length without popping the number
     *   multiply the length by the number
      +  add to the zero, or whatever it is now
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ push that numbers length without popping the number nice \$\endgroup\$ – Erik the Outgolfer Sep 23 '17 at 12:16
8
\$\begingroup\$

Haskell, 45 bytes

f n=sum[j*length(show j)|i<-[1..n],j<-[1..i]]

Try it online!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ A bit more unreadable but one byte cheaper: TIO \$\endgroup\$ – ბიმო Sep 23 '17 at 16:59
7
\$\begingroup\$

Brain-Flak, 166 bytes

<>(((()()())({}){})())<>{({}[()]<({}({}<<>({}[()])((){[()](<()>)}{}){{}((((({})({})){}{}){}))<>(({}<({}())>){()<({}[({})])>}{})(<>)}{}<>>({}({})())))>)}{}({}<{}{}{}>)

Try it online!

Explanation

<>(((()()())({}){})())<>           # Initialize second stack with 9 and 10
{({}[()]<                          # Do main loop n times:
  ({}
    ({}
      <
        <>({}[()])                 # Subtract 1 from counter to next power of 10
        ((){[()](<()>)}{}){        # If reached a power of 10 (say, 10^k):
          {}((((({})({})){}{}){})) # Multiply existing (10^k*0.9) by 10 and push twice
          <>                       # On first stack
          (
            ({}<({}())>)           # Increment length of numbers
            {()<({}[({})])>}{}     # Divide length of new set of numbers by this length
          )                        # Add together to get new set of numbers length
        (<>)}  
      {}<>>  
      ({}({})())                   # Add number length to number set length
    )                              # Add number set length to new segment length
  )                                # Add new segment length to total length
>)}                                # End main loop
{}({}<{}{}{}>)                     # Put result on stack by itself
| improve this answer | |
\$\endgroup\$
7
\$\begingroup\$

Husk, 5 bytes

ṁLΣQḣ

Try it online!

Explanation

ṁ  map over
 L  length
  Σ  sum list
   Q  all sublists
    ḣ  range 1 .. N
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Nice algorithm! \$\endgroup\$ – H.PWiz Sep 25 '17 at 19:33
4
\$\begingroup\$

Jelly, 7 bytes

Rx`ƤDFL

Try it online!

| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

05AB1E, 5 bytes

LŒJJg

Try it online!

Explanation

L      # push [1 .. a]
 Π    # all substrings (a)
  J    # numbers to strings (inner)
   J   # numbers to strings (outer)
    g  # length
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Welcome to the site :) \$\endgroup\$ – James Sep 25 '17 at 5:01
3
\$\begingroup\$

Python 2, 53 bytes

lambda x:sum(len(`i`)*i*(x-i+1)for i in range(1,x+1))

Try it online!

| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

Husk, 7 bytes

Σ∫mS*Lḣ

Try it online!

Ungolfed/Explanation

         -- implicit input N                        | 10
  m   ḣ  -- map the following function over [1..N]  | [1,2,3,4]
   S*L   --   multiply length of number by itself   | [1,2,3,4] (only important for numbers ≥ 10)
 ∫       -- prefix sums                             | [0,1,3,6,10]
Σ        -- sum                                     | 20
| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

Husk, 7 bytes

ṁLṁṘNḣḣ

Try it online!

Explanation

          Implicit input, e.g 4
      ḣ   Range from 1 to n                               [1,2,3,4]
     ḣ    Prefixes                                        [[],[1],[1,2],[1,2,3],[1,2,3,4]]
  ṁ       Map and then concatenate
   ṘN     Repeat each number in each list by its index    [[],[1],[1,2,2],[1,2,2,3,3,3],[1,2,2,3,3,3,4,4,4,4]]
                                                          [1,1,2,2,1,2,2,3,3,3,1,2,2,3,3,3,4,4,4,4]
ṁ         Map and then sum
 L        Length (of number: 10 -> 2)                     26
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Oh another Husk solution :) Didn't see your submission when posting mine, same bytecount but they're sufficiently different, so I'll leave mine here too. \$\endgroup\$ – ბიმო Sep 23 '17 at 12:34
3
\$\begingroup\$

JavaScript (ES6), 57 55 bytes

n=>[...Array(n)].reduce(x=>x+(++y+"").length*y*n--,y=0)

Try it online!

| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

Python 2, 59 58 bytes

Another one bytes the dust thanks to Jonathan Frech.

f=lambda n:n and sum(i*len(`i`)for i in range(n+1))+f(n-1)

Try it online!

Not short but eh... what the heck.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ len(`i`)*i for -> i*len(`i`)for. \$\endgroup\$ – Jonathan Frech Sep 23 '17 at 17:54
  • \$\begingroup\$ 53 bytes, non-recursive. \$\endgroup\$ – Jonathan Frech Sep 23 '17 at 22:46
3
\$\begingroup\$

CJam, 20 bytes

q~),(\{),{_s,*+}*+}%

Try it online!

The number is passed in the "input" field.

Ungolfed explanation: (example input = 2)

q~),(\{),{_s,*+}*+}%                                             | Stack:
q                     read input as string                       | "2"
 ~                    eval input (add it to stack as integer)    | 2
  )                   add 1                                      | 3
   ,                  range (convert to array with values 0...N) | [0, 1, 2]
    (                 pop first item of array                    | [1, 2] 0
     \                swap top two values of stack               | 0 [1, 2]
      {           }   for each item in array...                  | 0 1
       )              add 1                                      | 0 2
        ,             range (convert to array with values 0...N) | 0 [0, 1]
         {     }      for every element in the array...          | 0 0
          _           duplicate                                  | 0 0 0
           s          convert to string                          | 0 0 "0"
            ,         get length of string                       | 0 0 1
             *        multiply                                   | 0 0
              +       add                                        | 0 1
                *     fold                                       | 0 1
                 +    add                                        | 1
                   %  repeat                                     | 4

It seems hard when explained lol.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

J, 24 bytes

[:+/[:+/\[:(*#@":"0)1+i.

Similar high-level approach to dzaima's APL answer, translated into J, except we calculate the number's length by turning it into a string first instead of taking logs, and we get to use J's hook to multiply that length by the number itself: (*#@":"0). After that it's just the sum of the scan sum.

Try it online!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ 1(#.]*#\*#\.)1#@":@+i. also works for 22 bytes \$\endgroup\$ – miles Sep 26 '17 at 2:31
  • \$\begingroup\$ @miles That's clever -- it took me a bit to figure it out. How long have been programming in J for? \$\endgroup\$ – Jonah Sep 26 '17 at 5:11
  • \$\begingroup\$ A bit after I joined code-golf. I don't actually use it to write any real programs as no I know would be able to read it, but I do use it as an advanced desktop calculator now, and usually always have a window open to calculate something. \$\endgroup\$ – miles Sep 26 '17 at 5:47
2
\$\begingroup\$

R, 39 bytes

function(n)sum(nchar(rep(1:n,n:1*1:n)))

Verify all test cases!

Simple algorithm; I observed, as most did, that for i in 1:n, i is repeated i*(n-i+1) times. So I create that vector, count the number of characters in each, and sum them.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Python 2, 51 50 bytes

lambda n:sum(~k*(k-n)*len(`k+1`)for k in range(n))
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ @LeakyNun Why? I developed this answer myself. I didn't even check the other answers. \$\endgroup\$ – orlp Sep 23 '17 at 12:12
  • 1
    \$\begingroup\$ This doesn't even output the right answer, gives 0 for n=1, 3 for n=2 and 14 for n=3 \$\endgroup\$ – Halvard Hummel Sep 23 '17 at 12:12
  • \$\begingroup\$ @HalvardHummel Oops, messed up a sign and forgot a +1. Fixed now. \$\endgroup\$ – orlp Sep 23 '17 at 12:16
  • \$\begingroup\$ I see that you finally understood the patern! Is there a way to test your code online or the other Python 2 answer covers this, too? \$\endgroup\$ – user72253 Sep 23 '17 at 13:28
1
\$\begingroup\$

JavaScript (ES6), 50 42 bytes

Updated: now basically a port of what other answers are doing.

f=(n,i=1)=>n&&`${n}`.length*n*i+f(n-1,i+1)

Test cases

f=(n,i=1)=>n&&`${n}`.length*n*i+f(n-1,i+1)

console.log(f(1))   // 1
console.log(f(2))   // 4
console.log(f(3))   // 10
console.log(f(10))  // 230
console.log(f(50))  // 42190
console.log(f(100)) // 339240
console.log(f(150)) // 1295790

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Mathematica, 66 bytes

Tr[1^(f=Flatten)[IntegerDigits/@f@(a=Array)[a[#~Table~#&,#]&,#]]]&
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

QBIC, 21 bytes

[:|[a|[c|A=A+!c$}?_lA
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Actually, 13 bytes

R♂R♂i⌠;n⌡Mεjl

Try it online!

Explanation:

R♂R♂i⌠;n⌡Mεjl
R              range(1, n+1)
 ♂R            range(1, x+1) for x in previous
   ♂i          flatten into 1D list
     ⌠;n⌡M     for x in list:
      ;n       repeat x x times
          εj   concatenate to string
            l  length
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Japt, 12 11 10 9 bytes

õõÈ*sÊÃxx

Try it or test all numbers from 1 to 150.


Explanation

Implicit input of integer U.

õõ

Generate an array of integers from 1 to U and then generate sub-arrays from 1 to each integer.

È   Ã

Pass the elements of each sub-array through a function.

*sÊ

Convert the current element to a string (s), get it's length (Ê) and multiply it by the element.

xx

Reduce the main array by addition after first doing the same to each sub-array.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Jq 1.5, 82 49 43 bytes

[range(.)+1|range(.)+1|"\(.)"*.|length]|add

Expanded

[   range(.)+1        # for i=1 to N
  | range(.)+1        # for j=1 to i
  | "\(.)"*.          # "j" copied j times
  | length            # convert to length
] | add               # add lengths

Sample Run

$ jq -Mr '[range(.)+1|range(.)+1|"\(.)"*.|length]|add' <<< "150"
1295790

Try it online! also jqplay.org

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Stacked, 28 bytes

[~>[~>[:rep]"!]"!flat''#`#']

Try it online!

Some might ask, "At which point are aliases unreadable?" If this isn't close, you have a very liberal definition of "readability".

Explanation

[~>[~>[:rep]"!]"!flat''#`#']    input: N
 ~>[          ]"!               for each number K from 1 to N
    ~>[    ]"!                  for each number J from 1 to K
       :rep                     repeat J J times
                 flat           flatten the resultant array
                     ''#`       join by the empty string
                         #'     get the length of said string
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Ruby, 41 40 bytes

->n{(1..n).map{|x|[x]*x*=-x-~n}*''=~/$/}

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

C# (.NET Core), 94 80 74 bytes

n=>{int b=0,a=0,i;while(a++<n)for(i=0;i++<a;)b+=(i+"").Length*i;return b;}

Try it online!

I was hoping to find a direct solution like what @kamoroso94's answer started, but gave up as I was spending too much time on it. There probably is a way of doing it, but the formula needs to adjust for every magnitude step.

Acknowledgements

14 bytes saved thanks to @someone

6 bytes saved thanks to @Kevin Cruijssen

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ n=>{int b=0,a=0,i;for(;a++<n;)for(i=0;i++<a;)b+=i.ToString().Length*i;return b;} Try it online! for 80 bytes and performance. \$\endgroup\$ – the default. Sep 24 '17 at 10:54
  • 1
    \$\begingroup\$ i.ToString() can be (i+"") to save some more bytes. \$\endgroup\$ – Kevin Cruijssen Sep 25 '17 at 7:42
1
\$\begingroup\$

MATL, 15 bytes

:ttP*Y"10&YlQks

Try it online!

Explanation:

:                range 1:input (implicit input)
 tt              duplicate twice
   P             reverse
    *            multiply elementwise
     Y"          runlength decoding
       10&Yl     log10
            Qk   increment and floor
              s  sum (implicit output)
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ That logarithm is expensive :-) You can replace it by converting to string, removing spaces, length: :ttP*Y"VXzn \$\endgroup\$ – Luis Mendo Oct 6 '17 at 23:33
1
\$\begingroup\$

Perl 6, 36 bytes

{[+] 1..*Z*($_...1).map:{.chars*$_}}

Test it

Expanded:

{  # bare block lambda with implicit parameter 「$_」

  [+]               # reduce the following using &infix:«+»

    1 .. *          # Range from 1 to infinity

    Z*              # zip using &infix:«*»

    ( $_ ... 1 )    # sequence from the input down to 1
    .map:           # for each one
    { .chars * $_ } # multiply the number of digits with itself
}
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Charcoal, 18 14 bytes

IΣE⊕NΣE⊕ι×λLIλ

Try it online! Link is to verbose version of code. Edit: Using Sum saved me 4 bytes. Explanation:

  E⊕N           Map from 0 to the input (loop variable i)
      E⊕ι       Map from 0 to i (loop variable l)
            Iλ  Cast l to string
           L    Take the length
         ×λ     Multiply by l
     Σ          Sum the results
 Σ              Sum the results
I               Cast to string
                Implicitly print
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ :| Sum sums numbers in the strings when given string arguments \$\endgroup\$ – ASCII-only Oct 4 '17 at 11:23
  • \$\begingroup\$ @ASCII-only It wasn't that, it was just printing a Σ instead... \$\endgroup\$ – Neil Oct 4 '17 at 12:36
  • \$\begingroup\$ @ASCII-only Also, best I can do with Sum is still 18 bytes: Print(Cast(Sum(Map(InclusiveRange(1, InputNumber()), Sum(Map(InclusiveRange(1, i), Times(l, Length(Cast(l))))))))); \$\endgroup\$ – Neil Oct 4 '17 at 12:46
  • \$\begingroup\$ wait you keep forgetting Incremented :P \$\endgroup\$ – ASCII-only Oct 4 '17 at 12:50
  • \$\begingroup\$ @ASCII-only I tried sum of product but that was 17 bytes: ≔⊕NθIΣEθ×⁻θι×ιLIι. However, using Incremented instead of InclusiveRange shaves 4 bytes off my previous comment! \$\endgroup\$ – Neil Oct 4 '17 at 14:08
1
\$\begingroup\$

Ohm v2, 7 bytes

@@D×JJl

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

[Dyalog APL], 22 20 bytes

{+/≢¨⍕¨↑,/(/⍨¨⍳¨⍳⍵)}

Try it online!

Explanation:

{+/≢¨⍕¨↑,/(/⍨¨⍳¨⍳⍵)}
{                  } anonymous function with right argument named ⍵
                ⍳⍵   range 1 to right arg
              ⍳¨     for each, range 1 to it
             ¨       for each
           /⍨          for each item, repeat right arg left arg times
          (       )  take that and
        ,/           join the sub-arrays together
       ↑             convert from a nested array to a simple array (or something like that, I don't quite understand it :p)
     ⍕¨              convert each number to a char-array (aka string version)
   ≢¨                get length of each
 +/                  sum that together
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Röda, 31 bytes

{f={seq 1,_}f|f|[#`$_`*_1]|sum}

Try it online!

| improve this answer | |
\$\endgroup\$

Your Answer

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