28
2
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 in66
. 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 code-golf, 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 – 2017-08-04T19:52:48.830@Rod Sorry, you are right. I wrote
645321
instead of654321
. – Mr. Xcoder – 2017-08-04T19:54:35.6231Can I take input as a list of digits? – totallyhuman – 2017-08-04T20:07:19.867
@totallyhuman Yes, see the second spec. – Mr. Xcoder – 2017-08-04T20:08:03.363
Why the downvote? I don't mind being downvoted, I just want to improve the challenge if anyone has suggestions. – Mr. Xcoder – 2017-08-05T09:33:08.180
1Interesting challenge. Glad you were inspired by mine! – Gryphon – 2017-08-05T23:09:07.710