This question already has an answer here:

Sum-It is a language I created (quite late) for the Language Design Contest in TNB, for which the theme was "Range". Naturally I interpreted this as "Mountain Range" and created a language about mountains.

Sum-It code takes the form of several ASCII art mountains, using only the / and \ characters. The mountains are then parsed to a number representing the size of the mountain, and that number is used as either an opcode or a data value.

While developing the parser for this language, I noticed something quite interesting regarding mountain sizes:

The size of a standalone (ie: not overlapped) mountain is equivalent to double the sum of all integers lower than the mountain's height in characters.

So for example, the following mountain:

  /\   <- 1
 /  \  <- 2
/    \ <- 3

Has a character height of 3. Its "size" as far as Sum-It is concerned, is calculated as such:

  /\
 /##\
/####\

And is therefore 6.
This lines up with the above formula, because the sum of all integers lower than 3 is 1 + 2 = 3, which doubled is 6.

The challenge

Your challenge is, given a positive integer representing a mountain's height, output the Sum-It size of the mountain, using the above formula.

Scoring

This is , so fewest bytes wins!

Test Cases

Test cases are represented as input => output => formula

1 => 0 => 2()
2 => 2 => 2(1)
3 => 6 => 2(1+2)
4 => 12 => 2(1+2+3)
5 => 20 => 2(1+2+3+4)
6 => 30 => 2(1+2+3+4+5)
7 => 42 => 2(1+2+3+4+5+6)
8 => 56 => 2(1+2+3+4+5+6+7)
9 => 72 => 2(1+2+3+4+5+6+7+8)
10 => 90 => 2(1+2+3+4+5+6+7+8+9)
15 => 210 => 2(1+2+3+4+5+6+7+8+9+10+11+12+13+14)
20 => 380 => 2(1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19)
25 => 600 => 2(1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24)
30 => 870 => 2(1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29)
40 => 1560 => 2(1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39)
50 => 2450 => 2(1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49)
60 => 3540 => 2(1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59)
70 => 4830 => 2(1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69)
80 => 6320 => 2(1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79)
90 => 8010 => 2(1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+81+82+83+84+85+86+87+88+89)
100 => 9900 => 2(1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99)

marked as duplicate by Skidsdev, AdmBorkBork code-golf Dec 6 at 16:01

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    "using the above formula." so, the code has to calculate all these additions? – Felix Palmen Dec 6 at 15:29
  • Basically 2×triangular number. – user202729 Dec 6 at 15:40
  • 2
    @FelixPalmen no, provided it outputs the correct answer – Skidsdev Dec 6 at 15:51
  • @Skidsdev good! unfortunately, makes it too simple once you found the simpler formula ;) – Felix Palmen Dec 6 at 15:53
  • 1
    @user202729 hmm good point, given that challenge I think this one is really just a trivial modification to that challenge, with context around it. Guess I should've stuck to my original idea which was to take the ASCII mountains as input and output the size of the mountains. – Skidsdev Dec 6 at 15:56

APL+WIN, 8 6 bytes

+/2×⍳⎕

Prompts for input integer, creates a vector of integers from 1 to integer -1, multiplies by 2 and sums the result.

Saved 2 bytes thanks to Adam by switching to index origin zero.

  • Save two bytes by using ⎕IO←0 and removing -1. Save an additional byte by switching to Dyalog APL and removing . Try it online! – Adám Dec 6 at 15:38
  • 1
    @Adám Thanks for the two bytes which was of course trivial. Switching to Dyalog not so trivial ;( – Graham Dec 6 at 15:42
  • Why not? Apply for a free license or use unregistered version or just use TryAPL. – Adám Dec 6 at 18:06
  • @Adám I am conscious that we should take this elsewhere but I was sent a copy of v13 several years ago by some guys I know in Dyalog but the effort of converting the vast library of code I have in APL+WIN seemed overwhelming. I commented before that I might migrate when an Android version emerges – Graham Dec 6 at 21:40

C (gcc), 31 bytes

i;f(n,a){for(;--n;i+=n);a=i*2;}

Try it online!

EDIT: By using a global variable multiple execution of the functions wil lget wrong results. I'm not sure wether that's ok or no. Pretty new to PPCG.

Here's the corrected version:

C (gcc), 34 bytes

f(n,i,a){for(i=0;--n;i+=n);a=i*2;}

Try it online!

C (gcc), 13 bytes

f(x){x*=x--;}

Try it online!

Haskell, 10 bytes

(*)=<<pred

Try it online, using (<*>) would work too since multiplication is commutative..

Alternative, 9 bytes

I'm not completely sure if this is allowed, according to this I believe it is:

id<>(-1+)

Takes input as Product type, try it online!

Catholicon, 2 bytes

*Ẏ

Explanation:

*    multiply (the implicit input)
 Ẏ   by the implicit input decremented

Japt -mx, 1 bytes

Ñ

-m implicity flag creates a range from 0 to input - 1

Ñ multiply each value in the range by 2

Flag -x implicity outputs the sum of the result

Try it online!

Not the answer you're looking for? Browse other questions tagged or ask your own question.