# Triangle a number!

We are used to the term "squaring" n to mean calculating n2. We are also used to the term "cubing" n to mean n3. That being said, why couldn't we also triangle a number?

# How to triangle a number?

• First off, let's pick a number, 53716.

• Position it in a parallelogram, whose side length equals the number of digits of the number, and has two sides positioned diagonally, as shown below.

    53716
53716
53716
53716
53716

• Now, we want to ∆ it, right? To do so, crop the sides that do not fit into a right-angled triangle:

    5
53
537
5371
53716

• Take the sums of each row, for this example resulting in [5, 8, 15, 16, 22]:

    5  -> 5
53  -> 8
537  -> 15
5371  -> 16
53716  -> 22

• Sum the list [5, 8, 15, 16, 22], resulting in 66. This is the triangle of this number!

# Specs & Rules

• The input will be a non-negative integer n (n ≥ 0, n ∈ Z).

• You may take input and provide output by any allowed mean.

• Input may be formatted as an integer, a string representation of the integer, or a list of digits.

• Default loopholes disallowed.

• This is , so the shortest code in bytes wins!

# More Test Cases

Input -> Output

0 -> 0
1 -> 1
12 -> 4
123 -> 10
999 -> 54
100000 -> 6
654321 -> 91


Inspiration. Explanations are encouraged!

• are you sure that 645321 -> 91 ?
– Rod
Aug 4 '17 at 19:52
• @Rod Sorry, you are right. I wrote 645321 instead of 654321. Aug 4 '17 at 19:54
• Can I take input as a list of digits? Aug 4 '17 at 20:07
• @totallyhuman Yes, see the second spec. Aug 4 '17 at 20:08
• Interesting challenge. Glad you were inspired by mine! Aug 5 '17 at 23:09

# Pari/GP, 20 bytes

Takes the input as a list of digits.

a->a*Colrev([1..#a])


Try it online!

# Pari/GP, 17 bytes

Takes the input as a polynomial. For example, 5*x^4 + 3*x^3 + 7*x^2 + x + 6 means 53716.

a->x=1;eval(a+a')


Try it online!

## 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

• Literally exactly what I had. However I do also have a 1-byte-shorter solution taking input as a string... Aug 4 '17 at 20:01
• @ETHproductions Ah, I first tried with a string but ended up with something longer. Aug 4 '17 at 20:06

# Gaia, 4 3 bytes

…_Σ


Function accepting a list of digits and leaving the result on the stack.

Try it online!

### Explanation

…    Prefixes
_   Flatten
Σ  Sum

• Crossed out 4 is still 4 :( Aug 4 '17 at 20:14
• What's the footer for?
– Okx
Aug 7 '17 at 12:30
• @Okx Each line in Gaia is a function (like Jelly), so the footer is just calling it. Aug 7 '17 at 15:34
• So, why doesn't it work without the footer?
– Okx
Aug 7 '17 at 15:44
• @Okx Because I haven't yet made it detect a list from the input format, so I have to eval it (e) first Aug 7 '17 at 15:47

# PowerShell, 5448 40 bytes

param($a)$a|%{$o+=$_*($a.count-$i++)};$o  Try it online! 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.

# 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;}


Try it online!

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.

# Python 3, 58 bytes

lambda n:sum(-~i*int(c)for i,c in enumerate(str(n)[::-1]))


Try it online!

# 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;


Try it online!

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.

# 05AB1E, 3 bytes

### Code

ηSO


Try it online!

### Explanation

η      # Take the prefixes of the number
S     # Split into single numbers
O    # Sum all the numbers


# 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;  Try it online! 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.

_(n/10)>n
regsub .n "" n} [string len $n] set s}  Try it online! # Tcl, 105 bytes proc T n {time {incr s [expr [join$L +]]
set L [lreplace $L e e]} [llength [set L [split$n ""]]]
set s}


Try it online!

# Gol><>, 8 bytes

IEh@+:@+


Try it online!

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


# 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--];}


Try it online!

# EXCEL, 55 bytes

Using Immediate Window.

[A65:A89]="=MID(REPT(CHAR(ROW())&CHAR(ROW()+1),2),1,3)"


# Zsh, 60 bytes

f(){echo $((${(j:+:)${(@f)$(for i in $@;echo$((x+=i)))}}))}


Try it online!

Takes input as individual parameters