| Bytes | Lang | Time | Link |
|---|---|---|---|
| 015 | APLNARS | 250905T180026Z | Rosario |
| 015 | Dyalog APL | 250903T210322Z | Aaron |
| 009 | cQuents | 250902T203838Z | Stephen |
| 007 | Vyxal 3 | 250902T144433Z | Themooni |
| 036 | AWK | 250902T140958Z | xrs |
| 032 | Juby | 230721T152527Z | Jordan |
| 003 | Thunno 2 JL | 230721T061915Z | The Thon |
| 050 | Excel VBA | 170923T181114Z | Taylor R |
| 029 | Retina | 171025T144553Z | Martin E |
| 073 | Ruby | 170924T072906Z | EJAg |
| 044 | Bash | 170926T152127Z | Nahuel F |
| 031 | Röda | 171008T121119Z | fergusq |
| 093 | Powershell | 171008T104348Z | Ally Wil |
| 020 | [Dyalog APL] | 170923T123452Z | dzaima |
| 007 | Ohm v2 | 171005T073214Z | Cinaski |
| 111 | PocoLithp | 171005T060801Z | Andrakis |
| 014 | Charcoal | 170923T193348Z | Neil |
| 007 | Jelly | 170928T003720Z | sporkl |
| 044 | Scala | 170927T184105Z | cubic le |
| nan | 170926T135353Z | Brad Gil | |
| 005 | Husk | 170925T063339Z | Icer Wil |
| 015 | MATL | 170925T134604Z | Giuseppe |
| 039 | R | 170923T121820Z | Giuseppe |
| 074 | C# .NET Core | 170924T015451Z | Ayb4btu |
| 076 | Java 8 | 170925T075433Z | Kevin Cr |
| 040 | Ruby | 170925T065452Z | G B |
| 005 | 05AB1E | 170925T045859Z | Icer Wil |
| 012 | RProgN 2 | 170925T041030Z | ATaco |
| 028 | Stacked | 170924T211113Z | Conor O& |
| 036 | Kotlin | 170924T142340Z | 2xsaiko |
| 006 | 05AB1E | 170924T084519Z | Emigna |
| 024 | J | 170923T220328Z | Jonah |
| 020 | CJam | 170924T053001Z | Iaka Noe |
| nan | Perl 5 | 170924T035600Z | Xcali |
| 043 | Jq 1.5 | 170923T233012Z | jq170727 |
| 009 | Japt | 170923T130440Z | Shaggy |
| 034 | Mathematica | 170923T204821Z | JungHwan |
| 013 | Actually | 170923T201019Z | user4594 |
| 166 | BrainFlak | 170923T193422Z | Nitrodon |
| 058 | Python 2 | 170923T143233Z | totallyh |
| 021 | QBIC | 170923T181948Z | steenber |
| 045 | Haskell | 170923T162618Z | jferard |
| 055 | JavaScript ES6 | 170923T150629Z | Shaggy |
| 007 | Husk | 170923T121429Z | H.PWiz |
| 066 | Mathematica | 170923T125904Z | ZaMoC |
| 042 | JavaScript ES6 | 170923T121842Z | Arnauld |
| 005 | SOGL V0.12 | 170923T115818Z | dzaima |
| 007 | Husk | 170923T122454Z | ბიმო |
| 047 | Python 3 | 170923T121818Z | Leaky Nu |
| 050 | Python 2 | 170923T120905Z | orlp |
| 007 | Jelly | 170923T121029Z | Leaky Nu |
| 053 | Python 2 | 170923T120828Z | anon |
APL(NARS), 15 chars
{≢∊⍕¨∊⍴⍨¨¨⍳¨⍳⍵}
test:
f←{≢∊⍕¨∊⍴⍨¨¨⍳¨⍳⍵}
f 1
1
f 2
4
f 3
10
f 10
230
f 50
42190
f 100
339240
f 150
1295790
Dyalog APL, 15 bytes
{≢∊⍕¨∊(/⍨⍳)¨⍳⍵}
⍳⍵ # Range of 1..input
( )¨ # For each of those numbers
⍳ # range of 1..N again
/⍨ # and replicate with the same arg on left and right.
# e.g. 1 2 3 / 1 2 3 -> 1 2 2 3 3 3
∊ # Flatten
⍕¨ # Convert each digit to a character
∊ # Flatten again
≢ # Count
💎
Created with the help of Luminespire.
cQuents, 9 bytes
;b$
;$*S$
Explanation
;b$ first line
; mode series - given input n, output the sum of the first n terms
each term is
b second line ( )
$ current index
;$*S$ second line
; mode series - given input n, output the sum of the first n terms
each term is
$ current index
* times
S string ( )
$ current index
Vyxal 3, 7 bytes
ɾD⇄×$Ŀ•
ɾD⇄×$Ŀ•
ɾ # range 1..input (inclusive)
D # triplicate
⇄ # reverse one copy
× # multiply the reverse with the not-reversed
$Ŀ # get the lengths of each number in the third copy
• # multiply that and sum (dot product)
💎
Created with the help of Luminespire.
<script type="vyxal3">
ɾD⇄×$Ŀ•
</script>
<script>
args=[["1"],["2"],["3"],["10"],["50"],["100"],["150"]]
</script>
<script src="https://themoonisacheese.github.io/snippeterpreter/snippet.js" type="module"/>
AWK, 36 bytes
{for(;i++<$1;)s+=t+=i*length(i)}$0=s
Port of this clever answer.
I had made it this far (48 bytes):
{for(;i++<$1;)for(j=0;j++<i;)s+=length(j)*j}$0=s
J-uby, 32 bytes
:+|:*&(:+|:*&~:*%-[I])|:join|:+@
Explanation
:+ | :* & (:+ | :* & ~:* % -[I]) | :join | :+@
:+ | :* & ( ) # Range, then map with...
:+ | :* & # Range, then map with...
~:* % -[I] # [n] * n
| :join | :+@ # Then join all elements and get length
Thunno 2 JL, 3 bytes
RJF
Or, 5 bytes flagless: RJFJl
Explanation
RJFJl # Implicit input
R # Range [1..input]
J # Join into a string
F # All substrings
J # Join into a string
l # Length of the string
# Implicit output
Excel VBA, 66 51 50 Bytes
Anonymous VBE immediate window function that takes input from the range [A1] and outputs to the VBE
For i=1To[A1]:For j=1To i:s=s+j*Len(j):Next j,i:?s
Alternate Version, 94 Bytes
Anonymous VBE immediate window function that takes input,n, from range [A1] constructs the GUA(n) and outputs the length of it to the VBE immediate window
[B1].Resize([A1],1)="=IfError(Offset(B1,-1,0)&Rept(Row(),Row()),1)":?[Len(TextJoin("",1,B:B))]
Retina, 29 bytes
.+
$*
1
1$`¶
1
1$%`¶
1
$.%_
.
Explanation
.+
$*
Convert input to unary.
1
1$`¶
Turn the input N into a range from 1 to N by inserting the prefix up to each 1 after it, together with a linefeed.
1
1$%`¶
Do the same thing to each value in that range. This time we use $%`, which is just the prefix on the current line. So this gives 1, 1,2, 1,2,3, 1,2,3,4, ... (in unary). Now all we need to do is repeat each value accordingly and converting to decimal.
1
$.%_
This is done by replacing each 1 with the length of the line its on.
.
Finally we count the number of digits.
Ruby, 73 bytes
f=->(t){(1..t).map{|n|(1..n).map{|n|Array.new(n,n)}}.flatten.join.length}
Bash, 48, 44 bytes
Saved 4 bytes thanks to for + curly braces trick
for((;r+=t+=n*${#n},++n<=$1;));{ :;};echo $r
Powershell, 93 bytes
$a=@();$b=@();1..$n|% $_{$i=0;Do{$a+=$_;$i++}while($i-ne$_);$b+=$a-join''};($b-join'').Length
Code exploded a bit:
$inputNum = 10
$numbers = 1..$inputNum
$array = @()
$bigArray = @()
Foreach ($number in $numbers){
$i = 0
Do {
$array += $number
$i++
}
while ($i -ne $number)
$bigArray += $array -join ''
}
($bigArray -join '').Length
[Dyalog APL], 22 20 bytes
{+/≢¨⍕¨↑,/(/⍨¨⍳¨⍳⍵)}
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
PocoLithp, 111 bytes
(define gau(#(N I)(if(!(defined I))(gau N 1)(if(!= N 0)(+(*(*(length(expl(str N)))N)I)(gau(- N 1)(+ I 1)))N))))
More readable version:
(define gau (# (N I) (begin
(if (! (defined I)) ;; I not supplied, recurse with I set to 1
(gau N 1)
(begin ;; I supplied, calculate
(define A (length (expl (str N)))) ;; `N`.length
(if (!= N 0)
;; N != 0, head recursive calculation
(+ (* (* A N) I) (gau (- N 1) (+ I 1)))
;; N == 0, return it
N)
)
)
)))
Usage:
(gau 123) => 662505
(gau 256) => 7532088
Named function gau, which takes 1 or 2 parameters. If called with 1 parameter, calls self with 2nd parameter set to 1. This implements the default variables used in other implementations.
This could possibly be made tail recursive, but it would likely be even longer than it already is.
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
Jelly, 7 bytes
RRx`€VV
Explanation:
RRx`€VV Example input: 3
R Range. Result: [1, 2, 3]
R Range (vectorizes). Result: [[1], [1, 2], [1, 2, 3]]
€ Apply to €ach...
x` Repeat each number itself times. Result: [[[1]], [[1], [2, 2]], [[1], [2, 2], [3, 3, 3]]]
VV Concatenate them into one big friendly number. Result: 1122122333
Perl 6, 36 bytes
{[+] 1..*Z*($_...1).map:{.chars*$_}}
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
}
MATL, 15 bytes
:ttP*Y"10&YlQks
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)
R, 39 bytes
function(n)sum(nchar(rep(1:n,n:1*1:n)))
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.
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;}
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
Java 8, 76 bytes
n->{int r=0,i=0,j;for(;i++<n;)for(j=0;j++<i;r+=(j+"").length()*j);return r;}
I have the feeling this can be golfed with a more arithmetic approach, instead of using String#length..
Explanation:
n->{ // Method with integer as both parameter and return-type
int r=0, // Result-integer
i=0,j; // Index integers
for(;i++<n;) // Loop (1) from 0 to the input (exclusive)
for(j=0; // Reset `j` to 0
j++<i; // Inner loop (2) from 0 to `i` (exlusive)
r+= // Append the result-String with:
(j+"").length()*j // The length of `j` multiplied by `j`
); // End of inner loop (2)
// End of inner loop (1) (implicit / single-line body)
return r; // Return result-String
} // End of method
05AB1E, 5 bytes
LŒJJg
Explanation
L # push [1 .. a]
Œ # all substrings (a)
J # numbers to strings (inner)
J # numbers to strings (outer)
g # length
Stacked, 28 bytes
[~>[~>[:rep]"!]"!flat''#`#']
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
Kotlin, 36 bytes
{x:Int->(1..x).flatMap{1..it}.sum()}
Test:
val GAU = {x:Int->(1..x).flatMap{1..it}.sum()}
GAU(5)
=> 35
05AB1E, 6 bytes
LLD×Jg
Explanation
L # range [1 ... input]
L # for each range [1 ... x]
D # duplicate
× # repeat each in the first list a number of times determined by the second list
J # join to string
g # length
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.
CJam, 20 bytes
q~),(\{),{_s,*+}*+}%
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.
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
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.
Mathematica, 34 bytes
Sum[i⌊1+Log10@i⌋,{j,#},{i,j}]&
Actually, 13 bytes
R♂R♂i⌠;n⌡Mεjl
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
Brain-Flak, 166 bytes
<>(((()()())({}){})())<>{({}[()]<({}({}<<>({}[()])((){[()](<()>)}{}){{}((((({})({})){}{}){}))<>(({}<({}())>){()<({}[({})])>}{})(<>)}{}<>>({}({})())))>)}{}({}<{}{}{}>)
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
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)
Not short but eh... what the heck.
QBIC, 21 bytes
[:|[a|[c|A=A+!c$}?_lA
JavaScript (ES6), 57 55 bytes
n=>[...Array(n)].reduce(x=>x+(++y+"").length*y*n--,y=0)
Husk, 7 bytes
ṁLṁṘNḣḣ
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
Mathematica, 66 bytes
Tr[1^(f=Flatten)[IntegerDigits/@f@(a=Array)[a[#~Table~#&,#]&,#]]]&
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
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*+
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
Husk, 7 bytes
Σ∫mS*Lḣ
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
Python 2, 51 50 bytes
lambda n:sum(~k*(k-n)*len(`k+1`)for k in range(n))