| Bytes | Lang | Time | Link |
|---|---|---|---|
| 091 | Tcl | 180404T203616Z | sergiol |
| 033 | AWK | 250326T205042Z | xrs |
| 004 | Uiua | 250323T232014Z | janMakos |
| 004 | ☾ | 250323T223608Z | Joao-3 |
| 001 | Vyxal 3 s | 240129T193817Z | pacman25 |
| 020 | Pari/GP | 170805T031909Z | alephalp |
| 002 | Thunno 2 S | 230615T171640Z | The Thon |
| 006 | J | 170804T220414Z | Conor O& |
| 008 | K ngn/k | 221014T012834Z | oeuf |
| nan | Fig | 221013T145658Z | Seggan |
| 014 | Factor + math.unicode | 221013T055211Z | chunes |
| 002 | Vyxal | 221013T050600Z | DialFros |
| 060 | Zsh | 180807T042302Z | Noskcaj |
| 055 | EXCEL | 180807T023518Z | remoel |
| 050 | C gcc | 180312T153049Z | vazt |
| 008 | Gol><> | 180406T170133Z | Bubbler |
| 006 | Add++ | 180121T171958Z | caird co |
| 029 | Ruby | 180121T193515Z | Asone Tu |
| 013 | Perl 6 | 180210T025643Z | Jarrod F |
| 052 | Common Lisp | 180121T074743Z | Renzo |
| 079 | SNOBOL4 CSNOBOL4 | 170804T195443Z | Giuseppe |
| nan | Perl 5 | 170804T200357Z | Xcali |
| 075 | R | 180121T194151Z | Florian |
| 106 | Python 3 | 180121T163701Z | Nathan D |
| 054 | Python 3 | 180121T092117Z | Manish K |
| 064 | Scala | 180121T131915Z | 6infinit |
| 036 | BrainFlak | 170804T202219Z | DJMcMayh |
| 006 | Pyt | 171226T003926Z | mudkip20 |
| 053 | Java 8 | 170804T213154Z | Jakob |
| 040 | TXR Lisp | 170804T221111Z | Kaz |
| 021 | Röda | 170807T154143Z | fergusq |
| 057 | Excel VBA | 170808T121153Z | Taylor R |
| 056 | Math++ | 170807T182011Z | SuperJed |
| 004 | TIBASIC | 170807T174953Z | calc84ma |
| 026 | Clojure | 170807T153844Z | MattPutn |
| 032 | Vim | 170804T220501Z | ბიმო |
| 1478 | Taxi | 170807T142239Z | Engineer |
| 060 | Excel VBA Immediate Window | 170807T135418Z | Engineer |
| 056 | Python 2 | 170805T200828Z | Rohit-Pa |
| 032 | Ruby | 170805T141454Z | Idva |
| 003 | Neim | 170805T111450Z | Okx |
| 046 | Python 2 | 170805T060221Z | 0xffcour |
| 028 | BrainFlak | 170805T044227Z | Wheat Wi |
| 055 | PHP | 170804T233006Z | WebSmith |
| 013 | Haskell | 170804T213017Z | H.PWiz |
| 004 | APL | 170804T225615Z | marinus |
| 049 | Mathematica | 170804T225539Z | ZaMoC |
| 013 | Retina | 170804T221638Z | Neil |
| 002 | Husk | 170804T204432Z | ბიმო |
| 024 | Bash + GNU utilities | 170804T204617Z | Digital |
| 035 | Python 3 | 170804T205404Z | Jonathan |
| 059 | C# .NET Core | 170804T205050Z | Kamil Dr |
| 003 | 05AB1E | 170804T195924Z | Adnan |
| 068 | C# .NET Core | 170804T203106Z | Grzegorz |
| 045 | Python 2 | 170804T200829Z | totallyh |
| 037 | Python 3 | 170804T203318Z | Business |
| 025 | Haskell | 170804T202006Z | Wheat Wi |
| 028 | JavaScript ES6 | 170804T202828Z | ETHprodu |
| 004 | Jelly | 170804T195837Z | Jonathan |
| 004 | Pyth | 170804T201616Z | Maltysen |
| 003 | Jelly | 170804T202202Z | ETHprodu |
| 058 | Python 3 | 170804T201713Z | Halvard |
| 004 | Japt | 170804T195101Z | ETHprodu |
| 003 | MATL | 170804T201055Z | DJMcMayh |
| 080 | C# .NET Core | 170804T200720Z | Ian H. |
| 040 | PowerShell | 170804T195725Z | AdmBorkB |
| 003 | Gaia | 170804T200416Z | Business |
| 034 | JavaScript ES6 | 170804T200013Z | Arnauld |
| 056 | Python 2 | 170804T195757Z | Rod |
Tcl, 91 bytes
proc T n {time {append s +[join [split $n ""] +]
regsub .$ $n "" n} [string le $n]
expr $s}
proc T n {time {append s +[join [split $n ""] +]
regsub .$ $n "" n} [string le $n]
expr $s}
Tcl, 93 bytes
proc T n {time {incr s [expr [join [split $n ""] +]]
regsub .$ $n "" n} [string le $n]
set s}
proc T n {time {incr s [expr [regsub -all (.) $n +\\1]]
regsub .$ $n "" n} [string len $n]
set s}
proc T n {time {incr s [expr [join $L +]]
set L [lreplace $L e e]} [llength [set L [split $n ""]]]
set s}
Uiua, 4 bytes
/+\+
Takes input as a list of digits.
Similar to many other entries, it computes the prefix sum, then sums the result.
☾, 4 chars
Ϝ+⨁
Test it here!
Takes input as a list of digits, but the test link above makes it take an actual integer. Simply does the sum of cumulative sums.
Pari/GP, 20 bytes
Takes the input as a list of digits.
a->a*Colrev([1..#a])
Pari/GP, 15 bytes
Takes the input as a polynomial. For example, 5*x^4 + 3*x^3 + 7*x^2 + x + 6 means 53716.
a->(a+a')%(x-1)
Thunno 2 S, 2 bytes
ƒʂ
Explanation
ƒʂ # implicit input -> 654321
ƒ # preƒixes -> [[6], [6,5], [6,5,4], [6,5,4,3], [6,5,4,3,2], [6,5,4,3,2,1]]
ʂ # ʂum each list -> [6, 11, 15, 18, 20, 21]
# sum this list -> 91
# implicit output
J, 7 6 bytes
1#.+/\
Try it online! Takes a list of digits, such as f 6 5 4 3 2 1.
Explanation
The current explanation is same as the old, but noting that 1#.x ⇔ +/x (both compute sum).
Old:
[:+/+/\ (for explanation, input = 6 5 4 3 2 1)
\ over the prefixes of the input:
/ reduce:
+ addition (summation)
this gives is the cumulative sum of the input: 6 11 15 18 20 21
[: apply to the result:
+/ summation
this gives the desired result: 90
A little more true to the original problem would be [:+/@,]/, which is "sum" (+/) the flattened (,) prefixes of the input (]\).
K (ngn/k), 10 8 bytes
+/+\.'$:
-2 bytes thanks to ngn
Pretty basic train.
Explanation:
+/+\.'$: Main function. Takes implicit input with Right (:)
$ Convert input to string
' For each of the characters
. Eval them, converting them into a number
+\ Cumulative sum
+/ Sum
Fig, \$2\log_{256}(96)\approx\$ 1.646 bytes
Sk
Sk # Takes a list of digits as input
k # Prefixes
S # Sum of the automagically flattened list
Zsh, 60 bytes
f(){echo $((${(j:+:)${(@f)$(for i in $@;echo $((x+=i)))}}))}
Takes input as individual parameters
EXCEL, 55 bytes
Using Immediate Window.
[A65:A89]="=MID(REPT(CHAR(ROW())&CHAR(ROW()+1),2),1,3)"
C (gcc), 51 50 bytes
-1 byte thanks to @ceilingcat
i,r;f(t,l)int*t;{for(;l;)for(i=--l;~i;)r+=t[i--];}
Gol><>, 8 bytes
IEh@+:@+
Input format is space-separated digits.
How it works
IEh@+:@+
I Take next input as number
E If last input was EOF...
h Print the top as number and halt
Otherwise...
The stack looks like [sum_of_digits triangle digit]
@ Move 3rd from top (sum_of_digits) to the top
+ Add top two (update `sum_of_digits`)
: Duplicate top
@+ Add `sum_of_digits` to `triangle`
Repeat from the beginning
Perl 6, 13 bytes
Takes a list of digits
{[+] [\+] @_}
Perl 6 has a produce routine, which can be more tersely invoked using the 'triangle reduce' meta operator: [\ ]. Seems like it was made for this task.
Common Lisp, 53 52 bytes
(loop as(x . y)on(reverse(read))sum(+(reduce'+ y)x))
Input as list of digits.
-1 byte thanks to @ceilingcat.
SNOBOL4 (CSNOBOL4), 79 bytes
N =INPUT
D N LEN(1) . D REM . N :F(O)
X =X + D
Y =Y + X :(D)
O OUTPUT =Y
END
Input from stdin, output to stdout.
Perl 5, 19 + 1 (-p) = 20 bytes
s/./$\+=$p+=$&/ge}{
How?
$\ holds the cumulative total, $p holds the total of the digits on the current line. Each line of the parallelogram is simply the previous line with the next digit of the number appended. Therefore, it is the sum of the previous line plus the new digit. This iterates over all of the digits, calculating the sums as it goes. The actual substitution is irrelevant; it's just a means to iterate over the digits without creating an actual loop. At the end, $\ is printed implicitly by the -p option.
R, 75 bytes
function(x,b=nchar(x))sum(sapply(x%/%10^(0:b),function(y)y%/%10^(0:b)%%10))
I did not see an R solution yet, so here goes..
Python 3, 106 bytes
a=input()
c=[]
d=0
b=[int(g) for g in a]
for i in range(1,len(a)+1):
c+=b[0:i]
for t in c:
d+=t
print(d)
My original code was:
a,c,d=input(),[],0
b=[int(g) for g in a]
for i in range(1,len(a)+1):
c+=b[0:i]
for t in c:
d+=t
print(d)
But then I realized that by initializing all of my variables on one line, I gained two characters. :P
Python 3, 94 58 54 bytes
Thanks to Mr. Xcoder for helping me save quite some bytes!
lambda n:sum(int(v)*(len(n)-i)for i,v in enumerate(n))
Takes input as a string. It simply multiplies each digit by the number of times it needs to be added and returns their sum.
Scala, 64 bytes
def f(s:String)=s.indices.flatMap(i=>s.take(i+1).map(_-'0')).sum
Brain-Flak, 65, 50, 36 bytes
([])({<{}>{<({}[()])>[]}{}<([])>}{})
After lots of revising, I'm now very proud of this answer. I like the algorithm, and how nicely it can be expressed in brain-flak.
Most of the byte count comes from handling 0's in the input. In fact, if we could assume there were no 0's in the input, it would be a beautifully short 20-byte answer:
({{<({}[()])>[]}{}})
But unfortunately, brain-flak is notorious for bad handling of edge cases.
Explanation
First, an observation of mine:
If the input is n digits long, the first digit will appear in the triangle n times, the second digit will appear n-1 times, and so on onto the last digit, which will appear once. We can take advantage of this, since it's really easy to calculate how many digits of input are left in brain-flak, namely
[]
So here's how the code works.
# Push the size of the input (to account for 0's)
([])
# Push...
(
# While True
{
# Pop the stack height (evaluates to 0)
<{}>
# For each digit *D*...
# While true
{
# Decrement the counter (the current digit we're evaluating),
# but evaluate to 0
<({}[()])>
# Evaluate the number of digits left in the input
[]
# Endwhile
}
# This whole block evaluates to D * len(remaining_digits), but
# without affecting the stack
# Since we looped D times, D is now 0 and there is one less digit.
# Pop D (now 0)
{}
# Push the stack height (again, evaluating it as 0)
<([])>
# End while
}
# Pop a 0 off (handles edge case of 0)
{}
# end push
)
Pyt, 9 6 bytes
ąĐŁř↔·
Explanation:
Implicit input
ą Convert to array of digits
Đ Duplicate digit array
Łř↔ Create a new array [len(array),len(array)-1,...,1]
· Dot product with digit array
Implicit output
Java 8, 53 bytes
I implemented a lambda for each acceptable input type. They each iterate through the number's digits, adding the proper multiple of each to an accumulator.
Integer as input (53 bytes)
Lambda from Integer to Integer:
n->{int s=0,i=1;for(;n>0;n/=10)s+=n%10*i++;return s;}
String representation as input (72 bytes)
Lambda from String to Integer:
s->{int l=s.length(),n=0;for(int b:s.getBytes())n+=(b-48)*l--;return n;}
Digit array as input (54 bytes)
Lambda from int[] (of digits, largest place value first) to Integer:
a->{int l=a.length,s=0;for(int n:a)s+=n*l--;return s;}
- -7 bytes thanks to Olivier Grégoire
TXR Lisp, 67 40 bytes
(opip digits reverse conses flatten sum)
Run:
1> (opip digits reverse conses flatten sum)
#<intrinsic fun: 0 param + variadic>
2> [*1 53716]
66
Note that if we take advantage of the input being a list of integers representing digits, this reduces to:
(opip reverse conses flatten sum)
and if the digits may be in reverse order already, then just
(opip conses flatten sum)
Note also that this is a "useless use of opip"; all the terms are function names, and so [chain digits reverse conses flatten sum] could be used, and that would be the recommended way to code this; it's just one character longer.
Röda, 31 21 bytes
{i=0{[i]if i+=_}|sum}
Takes the input as a stream of digits.
31 bytes
{addHead 0|reduceSteps _+_|sum}
Takes the input as a stream of digits.
Explanation:
{addHead 0|reduceSteps _+_|sum} /* An anonymous function */
addHead 0 /* Append 0 to the beginning of the stream */
|reduceSteps _+_ /* Create a cumulative sum */
|sum /* Sum all numbers in the stream */
The reduceSteps function does not return the first item in the stream, so it is necessary to add a zero at the head of the stream.
Excel VBA, 57 Bytes
Anonymous VBE immediate window function that takes input from the range [A1], and outputs the triangle sum described above to the VBE immediate window
l=[Len(A1)]:For i=1To l:s=s+Mid([A1],i,1)*(l+1-i):Next:?s
Math++, 56 bytes
?>n
n>g
o+g%10>o
_(g/10)>g
3+3*!g>$
_(n/10)>n
2+6*!n>$
o
TI-BASIC, 4 bytes
sum(cumSum(Ans
Takes a list of digits as input, and calculates the sum of the cumulative sum (i.e. prefix sum) of the list.
The cumSum( token is 2 bytes large.
Clojure, 26 bytes
#(apply +(reductions + %))
There's a built-in, but in standard Clojure fashion, it's a long-ass word. Takes input as a list of digits.
Vim, 60 59 32 keystrokes
Thanks a lot @CowsQuack for the tip with the recursive macro and the h trick, this saved me 27 bytes!
qqYp$xh@qq@qVHJ:s/./&+/g⏎
C<C-r>=<C-r>"0⏎
Ungolfed/Explained
This will build the triangle like described (only that it keeps it left-aligned):
qq q " record the macro q:
Yp " duplicate the line
$x " remove last character
h " move to the left (this is solely that the recursive macro calls stop)
@q " run the macro recursively
@q " run the macro
The buffer looks now like this:
53716
5371
537
53
5
Join all lines into one and build an evaluatable expression from it:
VH " mark everything
J " join into one line
:s/./&+/g⏎ " insert a + between all the characters
The " register now contains the following string (note missing 0):
5+3+7+1+6+ +5+3+7+1+ +5+3+7+ +5+3+ +5+ +
So all we need to do is append a zero and evaluate it:
C " delete line and store in " register
<C-r>= ⏎ " insert the evaluated expression from
<C-r>" " register "
0 " append the missing 0
Taxi, 1478 bytes
Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to Chop Suey.Go to Chop Suey:n 1 r 1 l 4 r 1 l.[a]Switch to plan "b" if no one is waiting.Pickup a passenger going to The Babelfishery.Go to Zoom Zoom:n 1 l 3 r.1 is waiting at Starchild Numerology.Go to Starchild Numerology:w 4 l 2 r.Pickup a passenger going to Addition Alley.Go to Addition Alley:w 1 r 3 r 1 r 1 r.Pickup a passenger going to Addition Alley.Go to The Babelfishery:n 1 r 1 r.Go to Chop Suey:n 6 r 1 l.Switch to plan "a".[b]Go to Addition Alley:n 1 l 2 l.Pickup a passenger going to Cyclone.[c]Go to Zoom Zoom:n 1 l 1 r.Go to Cyclone:w.Pickup a passenger going to The Underground.Pickup a passenger going to Multiplication Station.Go to The Babelfishery:s 1 l 2 r 1 r.Pickup a passenger going to Multiplication Station.Go to Multiplication Station:n 1 r 2 l.Pickup a passenger going to Addition Alley.Go to The Underground:n 2 l 1 r.Switch to plan "d" if no one is waiting.Pickup a passenger going to Cyclone.Go to Addition Alley:n 3 l 1 l.Switch to plan "c".[d]Go to Addition Alley:n 3 l 1 l.[e]Pickup a passenger going to Addition Alley.Go to Zoom Zoom:n 1 l 1 r.Go to Addition Alley:w 1 l 1 r.Pickup a passenger going to Addition Alley.Switch to plan "f" if no one is waiting.Switch to plan "e".[f]Go to Zoom Zoom:n 1 l 1 r.Go to Addition Alley:w 1 l 1 r.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:n 1 r 1 r.Pickup a passenger going to Post Office.Go to Post Office:n 1 l 1 r.
Un-golfed:
[ Pickup stdin and split into digits ]
Go to Post Office: west 1st left 1st right 1st left.
Pickup a passenger going to Chop Suey.
Go to Chop Suey: north 1st right 1st left 4th right 1st left.
[a]
[ Count the digits ]
Switch to plan "b" if no one is waiting.
Pickup a passenger going to The Babelfishery.
Go to Zoom Zoom: north 1st left 3rd right.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: west 4th left 2nd right.
Pickup a passenger going to Addition Alley.
Go to Addition Alley: west 1st right 3rd right 1st right 1st right.
Pickup a passenger going to Addition Alley.
Go to The Babelfishery: north 1st right 1st right.
Go to Chop Suey: north 6th right 1st left.
Switch to plan "a".
[b]
Go to Addition Alley: north 1st left 2nd left.
Pickup a passenger going to Cyclone.
[c]
[ Multiply each digits by Len(stdin)-Position(digit) ]
Go to Zoom Zoom: north 1st left 1st right.
Go to Cyclone: west.
Pickup a passenger going to The Underground.
Pickup a passenger going to Multiplication Station.
Go to The Babelfishery: south 1st left 2nd right 1st right.
Pickup a passenger going to Multiplication Station.
Go to Multiplication Station: north 1st right 2nd left.
Pickup a passenger going to Addition Alley.
Go to The Underground: north 2nd left 1st right.
Switch to plan "d" if no one is waiting.
Pickup a passenger going to Cyclone.
Go to Addition Alley: north 3rd left 1st left.
Switch to plan "c".
[d]
Go to Addition Alley: north 3rd left 1st left.
[e]
[ Sum all the products ]
Pickup a passenger going to Addition Alley.
Go to Zoom Zoom: north 1st left 1st right.
Go to Addition Alley: west 1st left 1st right.
Pickup a passenger going to Addition Alley.
Switch to plan "f" if no one is waiting.
Switch to plan "e".
[f]
[ Output the results ]
Go to Zoom Zoom: north 1st left 1st right.
Go to Addition Alley: west 1st left 1st right.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: north 1st right 1st right.
Pickup a passenger going to Post Office.
Go to Post Office: north 1st left 1st right.
Excel VBA Immediate Window, 60 bytes
a=StrReverse([A1]):For i=1 ToLen(a):b=b+i*Mid(a,i,1):Next:?b
Input is in cell A1 of the active sheet. Output is to the VBA immediate window.
Python 2, 56 bytes
lambda a:sum((i+1)*int(a[-i-1])for i in range(0,len(a)))
Some edit as suggested by @Mr.Xcoder
Python 2, 50 bytes
lambda a:sum(-~i*int(a[~i])for i in range(len(a)))
Neim, 3 bytes
𝐗𝐂𝐬
Explanation:
𝐗 Get prefixes of input, including itself
𝐂 Implicitly join elements together, and create an array with all the digits
𝐬 Sum
Alternative answer:
𝐗𝐣𝐬
Explanation:
𝐗 Get prefixes of input, including itself
𝐣 Join
𝐬 Implicitly convert to a digit array, and sum
Brain-Flak, 28 bytes
(([]){[{}]({}<>{})<>([])}{})
14 bytes if we don't need to support zeros (which we do)
({({}<>{})<>})
DJMcMayhem has a cool answer here you should check out. Unfortunately for him I wasn't about to let him win at his own language :P
How does it work?
Lets start with the simple version.
({({}<>{})<>})
The main action here is ({}<>{})<>, that takes the top of the left stack and adds to to the top of the right stack. By looping this operation we sum up the current stack (until it hits a zero) placing the sum on the off stack. That's pretty mundane, the interesting part is that we sum up the results of all these runs as our result. This will calculate the desired value. Why? Well lets take a look at an example, 123. On the first grab we just get 1 so our value is 1
1
On the next grab we return 1 plus the 2
1
1+2
On the last run we have all three together
1
1+2
1+2+3
Do you see the triangle? The sum of all the runs is the "triangle" of the list.
Ok but now we need it to work for zeros, here I used the same trick as DJMcMayhem, plus some fancy footwork. Instead of looping until we hit a zero we loop until the stack is empty.
([])({<{}>({}<>{})<><([])>}{})
I then used this tip, written by none other than yours truly, to golf off another 2 bytes.
(([]){[{}]({}<>{})<>([])}{})
And there we have it. I would be surprised if there was a shorter solution, but then again stranger things have happened.
PHP, 51 55 bytes
+4 bytes—original version failed on all inputs with 0 as a digit.
<?php while($x!=$d=array_pop($argv))$t+=++$p*$d;echo$t;
EXPLANATION:
<?php
The programme takes separate digits as CLI parameter arguments, which arrive in the code as an array, $argv.
Each character at a time is popped from the end of the array. When the array is empty array_pop returns null. Null is detected by comparing the result to an undeclared variable $x, a saving of 2 code bytes over using null itself. (Without this comparison any 0 digit evaluates to false and the loop ends early.)
while ($x != $d = array_pop($argv))
$p is the position from the end i.e. the last digit (the first one popped) is in position 1. $p is
undeclared and so equals 0, but is incremented before it is used each time, so starts off as 1.
The position is multiplied by the digit and added to the total $t.
$t += ++$p * $d;
The final result is printed.
echo $t;
Haskell, 13 bytes
sum.scanl1(+)
Takes input as list of digits. Calculates the cumulative sums then sums them.
APL, 4 bytes
+/+\
This takes the input as a list of digits, e.g.:
(+/+\) 5 3 7 1 6
66
Explanation
+/ sum of
+\ partial sums of input
Mathematica, 49 bytes
Tr@Array[Tr@s[[;;#]]&,Length[s=IntegerDigits@#]]&
Retina, 13 bytes
.
$`$&
.
$*
1
Try it online! Link includes test cases. Explanation: The first stage generates all the prefixes of the original number, the second stage converts each digit to unary, and the third stage takes the total.
Bash + GNU utilities, 32 24
tac|nl -s*|paste -sd+|bc
Input read from STDIN.
Update: I see the input may be given as a list of digits. My input list is newline-delimited.
Explanation
tac # reverse digit list
|nl -s* # prefix line numbers; separate with "*" operator
|paste -sd+ # join lines onto one line, separated with "+" operator
|bc # arithmetically evaluate
Python 3, 35 bytes
I just noticed that this is only really a slight golf of Business Cat's answer in the end though!
f=lambda a:a>[]and sum(a)+f(a[:-1])
C# (.NET Core), 59 bytes
using System.Linq;N=>N.Reverse().Select((d,i)=>i*d+d).Sum()
Substantially different from the other C# answers. Input is a list of digits. All test cases included in the TIO link.
Could save a bunch of bytes if allowed to take input as a backwards list of digits with leading 0.
05AB1E, 3 bytes
Code
ηSO
Explanation
η # Take the prefixes of the number
S # Split into single numbers
O # Sum all the numbers
C# (.NET Core), 84 68 bytes
a=>a.Select((x,i)=>a.Take(i+1).Sum(c=>c-48)).Sum()
Byte count also includes
using System.Linq;
Explanation:
a=> // Take a string
a // Take the string as collection of chars
.Select((x,i)=> // Replace the collection with
a.Take(i+1) // Substrings increasing in size
.Sum(c=>c-48)) // Sum each substring's digits (as ints) together
.Sum() // Sum the new collection of sums
I know it's a little bigger than there's already posted C# answer, but mine is coming from a different approach so I thought I will post it anyway.
Python 2, 49 45 bytes
-4 bytes thanks to Mr. Xcoder.
lambda n:sum(-~i*n[~i]for i in range(len(n)))
Takes input as a list of digits.
Haskell, 25 bytes
Takes input as list of digits
f[]=0
f x=sum x+f(init x)
Haskell, 41 bytes
Takes input as string representation
f[]=0
f x=sum(map(read.(:[]))x)+f(init x)
JavaScript (ES6), 28 bytes
a=>a.map(d=>t+=c+=d,t=c=0)|t
Takes input as a list of digits.
Jelly, 5 4 bytes
Ṛæ.J
A monadic link taking a list of decimal digits and returning the triangle of the number that list represents.
How?
Ṛæ.J - Link: list of numbers (the decimal digits), d e.g. [9,4,5,0]
Ṛ - reverse d [0,5,4,9]
J - range(length(d)) [1,2,3,4]
æ. - dot-product (0*1 + 5*2 + 4*3 + 9*4 = 58) 58
Jelly, 3 bytes
+\S
Try it online! Uses the same technique as my Japt answer: cumulative addition, then sum.
Japt, 7 6 4 bytes
å+ x
Explanation
å+ x Implicit: input = digit list
å+ Cumulative reduce by addition. Gives the sum of each prefix.
x Sum.
Old solution:
å+ ¬¬x
Explanation
å+ ¬¬x Implicit: input = string
å+ Cumulative reduce by concatenation. Gives the list of prefixes.
¬ Join into a single string of digits.
¬ Split back into digits.
x Sum.
Implicit: output result of last expression
C# (.NET Core), 80 bytes
n=>{int v=0,i=0;for(;i<n.Length;)v+=int.Parse(n[i]+"")*(n.Length-i++);return v;}
Takes a string as input and outputs the triangled number.
Explanation:
For each character in the input string, convert the char to an int and multiply it by the length of the string minus the index of the char.
PowerShell, 54 48 40 bytes
param($a)$a|%{$o+=$_*($a.count-$i++)};$o
Takes input as a list of digits.
Note that the output is just the input digit multiplied by its corresponding negative index in the string and then cumulatively summed. So, that's what we do here.
We loop over each element in the input list, each iteration perform a multiplication, and then sum the results together into the total $o+=. That's left on the pipeline, output is implicit.
Gaia, 4 3 bytes
…_Σ
Function accepting a list of digits and leaving the result on the stack.
Explanation
… Prefixes
_ Flatten
Σ Sum
JavaScript (ES6), 34 bytes
f=(n,i=1)=>n&&n%10*i+f(n/10|0,i+1)
Test cases
f=(n,i=1)=>n&&n%10*i+f(n/10|0,i+1)
console.log(f(0)) // -> 0
console.log(f(1)) // -> 1
console.log(f(12)) // -> 4
console.log(f(123)) // -> 10
console.log(f(999)) // -> 54
console.log(f(100000)) // -> 6
console.log(f(654321)) // -> 91

