[Question]
  [
Given a list of positive integers, find the number of triangles we can form such that their side lengths are represented by three distinct entries of the input list.
(Inspiration comes from [CR](https://codereview.stackexchange.com/questions/226885/counting-the-triangles-that-can-be-formed-from-segments-of-given-lengths).)
### Details
* A triangle can be formed if *all* permutations of the three side lengths \$a,b,c\$ satisfy the *strict* triangle inequality $$a + b > c.$$ (This means \$a+b > c\$, \$a+c>b\$ and \$b+c>a\$ must all hold.)
* The three side lengths \$a,b,c\$ must appear at distinct positions in the list, but do not necessarily have to be pairwise distinct.
* The order of the three numbers in the input list does not matter. If we consider a list `a` and the three numbers `a[i], a[j], a[k]` (where `i,j,k` are pairwise different), then `(a[i],a[j],a[k]), (a[i],a[k],a[j]), (a[j], a[i], a[k])` etc. all are considered as the *same* triangle.
* The input list can assumed to contain at least 3 entries.
* You can assume that the input list is sorted in ascending order.
### Examples
A small test program can be found here on [Try it online!](https://tio.run/##ZY/NDoIwEITvPMV4MRgWIupRfQS9eCMctrXSxlISWuOFd8eKPzHxMpkvO/un2V@VteModdd5tYbFDlVqZzMmRBWTygUGqwLaWLTKNUHHXI6SwNjmqJZF0dYEMQFn5QvlhOKFdWL8qTfsGquQMgmKQ3fgTGAfk/M5RCaj5aeVGUcrEndrheqPl0@jn857X1AduqCNazAgPDd9PyD8rAp10rJxsc24oHqWAanX3R0F/qcX6BWfF@NYlbSiNW3qBw "Haskell – Try It Online")
```
Input, Output:
[1,2,3]  0
[1,1,1]  1
[1,1,1,1] 4
[1,2,3,4] 1
[3,4,5,7] 3
[1,42,69,666,1000000] 0
[12,23,34,45,56,67,78,89] 34
[1,2,3,4,5,6,7,8,9,10] 50
```
For the input of `[1,2,3,...,n-1,n]` this is [A002623](http://oeis.org/A002623).
For the input of `[1,1,...,1]` (length `n`) this is [A000292](http://oeis.org/A000292).
For the input of the first `n` Fibonacci numbers ([A000045](http://oeis.org/A000045)) this is [A000004](http://oeis.org/A000004).
  
[Answer]
# [Stax](https://github.com/tomtheisen/stax), ~~8~~ 7 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
Thanks to [recursive](https://codegolf.stackexchange.com/users/527/recursive) for -1!
```
é═rê÷┐↨
```
[Run and debug it at staxlang.xyz!](https://staxlang.xyz/#p=82cd7288f6bf17&i=[1+2+3]%0A[1+1+1]%0A[1+2+3+4]%0A[3+4+5+7]%0A[1+42+69+666+1000000]%0A[12+23+34+45+56+67+78+89]%0A[1+2+3+4+5+6+7+8+9+10]&a=1&m=2)
### Unpacked (8 bytes) and explanation:
```
r3SFE+<+
r           Reverse
 3S         All length-3 combinations
   F        For each combination:
    E         Explode: [5,4,3] -> 3 4 5, with 3 atop the stack
     +        Add the two shorter sides
      <       Long side is shorter? 0 or 1
       +      Add result to total
```
That's a neat trick. If you have a sequence of instructions that will always result in 0 or 1 and you need to count the items from an array that yield the truthy result at the end of your program, `F..+` is a byte shorter than `{..f%`.
Assumes the initial list is sorted ascending. Without this assumption, stick an `o` at the beginning for 8 bytes.
[Answer]
# [R](https://www.r-project.org/), ~~62~~ ~~52~~ ~~40~~ 34 bytes
```
sum(c(1,1,-1)%*%combn(scan(),3)>0)
```
[Try it online!](https://tio.run/##K/r/v7g0VyNZw1DHUEfXUFNVSzU5PzcpT6M4OTFPQ1PHWNPOQPO/oZGCkbGCsYmCiamCqZmCmbmCuYWCheV/AA "R – Try It Online")
Port of [Luis Mendo's Octave solution](https://codegolf.stackexchange.com/a/190966/67312)
Since `a<=b<=c`, the triangle condition is equivalent to `a+b-c>0`. The `a+b-c` is succinctly captured by the matrix product `[1,1,-1] * X`, where `X` is the 3-combinations of the input array.
There were a lot of suggestions for improvements made by 3 different people in the comments:
* [Robert S.](https://codegolf.stackexchange.com/users/81727/robert-s) for [suggesting `scan`](https://tio.run/##BcFLCoAgEADQq8xyhtz4N@gm4qKEoIUjZMLc3t571xqzYe3tYhz1ZCRl1T25fk9nFJJsyyFZl02yKURLGzAWrAPnwQcIEWKCtK8f).
* [Robin Ryder](https://codegolf.stackexchange.com/users/86301/robin-ryder) for suggesting [improvements to the triangle inequality](https://tio.run/##PYnbCoMwEETf@xU@ZssU6ibZKNgvEcE2ECiYCG0F/z6mN2dezpx55FB1pyqHJfnXfU5qwtiPFz/HW6LnEhUfp14jXtehe0NRA@WgvKrB0ESHL5fuXDzMbxWChds/w5AWIoL6/Mn/YbCGNjAWViAOrkHTEuUN) and [this odd one](https://tio.run/##K/r/P8e2ODkxT0PTOiE6wTY5Pzcpz7q4NFfDSCsn2lgnNSfWBkQDRWI1/1tYKphbKJiZK5iaKZiYKhibKBgZKxga/QcA) which requires the input to be in *descending* order (which just goes to show how important a flexible input format is).
* and finally [Nick Kennedy](https://codegolf.stackexchange.com/users/42248/nick-kennedy) for the following:
# [R](https://www.r-project.org/), 40 bytes
```
y=combn(scan(),3);sum(y[3,]<y[1,]+y[2,])
```
[Try it online!](https://tio.run/##K/r/v9I2OT83KU@jODkxT0NTx1jTurg0V6My2lgn1qYy2lAnVrsy2kgnVvO/oZGCkbGCsYmCiamCqZmCmbmCuYWCheV/AA "R – Try It Online")
[Answer]
# [Haskell](https://www.haskell.org/), 49 bytes
```
([]%)
[c,b,a]%l|a+b>c=1
p%(h:l)=(h:p)%l+p%l
_%_=0
```
[Try it online!](https://tio.run/##LcVNCsIwEEDhfU4xCwMNHcH@VyFeJIYyDcYWp2WwLj27sQt58L2JtuedOUV7S5nz2igXcETymj@Uj9dgCyU6my5s7K4YzbloVoMe7CktNK9gQV7z@oYDLCQQ9ztXlFhWWNVYN9i02HbY9difPYIr8J/36Rsi02NLxyDyAw "Haskell – Try It Online")
Recursively generates all subsequences of `l` (reversed), and checks which length-3 ones form triangles.
**50 bytes**
```
f l=sum[1|[a,b,c]<-filter(>0)<$>mapM(:[0])l,a+b>c]
```
[Try it online!](https://tio.run/##LcpNDoIwEEDhq8yCBcQhofxroDfwBJMuSkO1cUoaijvPbmVh3uLbvKeOr5U5JQs8x7cn8SGNCxo1ldbxse65rIopk16He36jShWM@rJIo5LXboMZwu62AzI4D7CnRKLGusGmxbbDrsd@wGHE8aoQSOA/pdLXWNaPmEoTwg8 "Haskell – Try It Online")
The same idea, generating the subsequences with `mapM`, by mapping each value in `l` either to itself (include) or `0` (exclude).
**50 bytes**
```
([]%)
p%(b:t)=sum[1|c<-t,a<-p,a+b>c]+(b:p)%t
_%_=0
```
[Try it online!](https://tio.run/##Lcm9DsIgFEDhvU/BIElJL4n0X1N8ESQNJaKN0NwIbj672MGc4RvOw8Tnzfvs5DWXSlNWIC2Xc2IyvoMSHzvxBGbiCKZaLlZX@0RGUzHTWR5zMOtGJMHXuiVyIMEgcbtKiRrqBpoW2g66HvoBhhHGkwaiBPzTOn@t8@YeM7eIPw "Haskell – Try It Online")
Tries every partition point to take the middle element `b`.
**51 bytes**
```
f(a:t)=f t+sum[1|b:r<-scanr(:)[]t,c<-r,a+b>c]
f _=0
```
[Try it online!](https://tio.run/##LcVLDsIgEADQvaeYhYs2nSbSv6R4EULMlIg2AiGAO88uujBv8R6UnjdrSzEV8VwLA7lJLyfZe@NxbZMmHyteS5VRr21EaraLVgcDV3EqjnYPAkLcfYYjOApgfkvJOux67AccRhwnnGacF1zOCkEy/FOqfLSxdE@l1SF8AQ "Haskell – Try It Online")
The function `q=scanr(:)[]` generates the list of suffixes. A lot of trouble comes from needing to consider including equal elements the right number of times.
**52 bytes**
```
q=scanr(:)[]
f l=sum[1|a:r<-q l,b:s<-q r,c<-s,a+b>c]
```
[Try it online!](https://tio.run/##LcZNDsIgEEDhvaeYhQuNw4L@S4oXISymxGpTIJTRnWcXNTFv8b078Xr1vpRNs6OYD@po7G4Gr/kZjHyRyqPYwOOk@DcZ3SgY6TRdnC2BlggaUl7iA/YQKMH81RhZYVVj3WDTYtth12M/4HC2CEbiP2vL282eblyES@kD "Haskell – Try It Online")
The helper function `q=scanr(:)[]` generates the list of suffixes.
**57 bytes**
```
import Data.List
f l=sum[1|[a,b,c]<-subsequences l,a+b>c]
```
[Try it online!](https://tio.run/##LcW9DsIgEADg3ae4wc2rCf03sU6OvgFhOAgoKVTs0c1nFx3MN3wP4tmGUIqP6blmuFKm481z3jkIE29Rirck1GjUueJNs31tdjGWISAd9MWoEskvMEFa/ZJhD5ESuN9SihrrBpsW2w67HvsBhxHHk0KQAv@UKh/jAt25VCalLw "Haskell – Try It Online")
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 11 bytes
```
{⊇Ṫ.k+>~t}ᶜ
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/pfXa6koGunoFRu/6htw6Ompoe7Omsfbp3wv/pRV/vDnav0srXt6kpqH26b8/9/tEK0oY6RjnGsDpAGQjAN5OuYAFlAUsdUxxwsZmKkY2apY2ZmpmNoAAYgUSMdI2MdYxMdE1MdUzMdM3MdcwsdC0uEGUDdZjrmOhY6lkBdsQqxAA "Brachylog – Try It Online")
I may have forgotten to take advantage of the sorted input in my old solution:
# [Brachylog](https://github.com/JCumin/Brachylog), ~~18~~ ~~17~~ 15 bytes
```
{⊇Ṫ¬{p.k+≤~t}}ᶜ
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/pfXa6koGunoFRu/6htw6Ompoe7Omsfbp3wv/pRV/vDnasOraku0MvWftS5pK6ktvbhtjn//0crRBvqGOkYx@oAaSAE00C@jgmQBSR1THXMwWImRjpmljpmZmY6hgZgABI10jEy1jE20TEx1TE10zEz1zG30LGwRJgB1G2mY65joWMJ1BWrEAsA "Brachylog – Try It Online")
```
{            }ᶜ    The output is the number of ways in which
 ⊇                 a sublist of the input can be selected
  Ṫ                with three elements
   ¬{       }      such that it is not possible to show that
     p             for some permutation of the sublist
       k+          the sum of the first two elements
         ≤         is less than or equal to
      .   ~t}      the third element.
```
[Answer]
# [Perl 6](https://github.com/nxadm/rakudo-pkg), 35 bytes
```
+*.combinations(3).flat.grep(*+*>*)
```
[Try it online!](https://tio.run/##TYtNCoMwGETXzSlcuMgXh1ATTRRpr1LSokXwJ6gbKT17GmwXZRbzeMz4dhlMGPck7ZJLyIR8zOO9n9zWz9PKNclucJt8Lq3nIhNXQaFhnJ14DgVNOCjmR9GhODg2StivV1AaukBRojQwFrZCVf9/4tbAokKN/EyM5Og8f6UdT28kV7e/qWHhAw "Perl 6 – Try It Online")
# Explanation
It's a Whatever code, i. e. a concise notation for lambda functions (that works only in very simple cases). Each `*` is a placeholder for one argument. So we take the list of lengths (that appears at the first `*`), make all combinations of 3 elements (they always come out in the same order as in the original list, so that means the combinations are sorted too), flatten the list, and then take the list 3-by-3, and filter (`grep`) only those triplets that satisfy `*+*>*`, i. e. that the sum of the first two arguments is greater than the third. That gives all the triplets, and we finally count them with forcing numeric context with a `+`.
(Of course we need to test it only for the case of "sum of two smaller > the largest". If this holds, the other hold trivially, if this does not, the triplet does not denote correct triangle lengths and we don't need to look further.)
[Answer]
# [Python 3](https://docs.python.org/3/), 73 bytes
```
lambda l:sum(a+b>c for a,b,c in combinations(l,3))
from itertools import*
```
[Try it online!](https://tio.run/##TZDBboMwEETv/oo94nYOgMFAJPIjaVQZYlQkgxF2pPbr6UKjpLuXfTvakcfLT/zys9qG9mNzZupuhtwp3KfEvHfnnga/kkGHnsaZej9142zi6OeQOCgpxbD6icZo1@i9CzROi1/j2xZtiJ@9CTZQSxeRXDLkUFdQKnEQN1P2INZQPJlnlKiY1UMvcugGWmtk6VEvpxy5gipQlCg1dIWqRt3st8V/czbUqFCjYQtWy1SKqxB7PAey34vto73tIV9PPwni8vfIGYbEyQNNCBz2b9s@D0H8H/vy5SW3Xw "Python 3 – Try It Online")
This is the first naive, brute-force approach that comes to my mind. I'll update the post if I find a shorter solution using a different approach. Note that since the input is sorted, the tuple \$(a,b,c)\$ is also in the ascending order so it suffices to only check whether \$a+b>c\$ holds.
[Answer]
# [Retina](https://github.com/m-ender/retina/wiki/The-Language), 55 bytes
```
\d+
*
L$`_+
$<'
%L$w`(,_+)\b.*\1(_*)\b(?<=^_+\2,.*)
_
_
```
[Try it online!](https://tio.run/##NYu9CsIwGEX37zlSTJNLyZfmr1BxdOkjBBOlDi4OIvj4MQhyuXDOcF739@N55TbIc21516RoE7VoEuuBhk18qkTRY75NKrMsqpM8rcdL0dliUiMVKq0xLGZi9NGP4agfHrG7w4IQwMYQW9gZs4Pz8AEhIiak5R/1ICAi9YDNFw "Retina – Try It Online") Link includes test cases, but with the values in the 5th case reduced to allow it to finish today. Assumes sorted input. Explanation: Regexes don't really like matching more than one thing. A normal regex would be able to find all of the values that could be a shortest leg of a triangle. Retina's `v` option doesn't help here, except to avoid a lookahead. However Retina's `w` option is slightly more helpful, as it would be able to find both the shortest and longest leg at the same time. That's not enough for this challenge though, as there might be multiple middle legs.
```
\d+
*
```
Convert the input to unary.
```
L$`_+
```
For each input number...
```
$<'
```
... create a line that's the original array truncated to start at that number. `$'` normally means the string after the match, but the `<` modifies it to mean the string after the previous separator, avoiding wasting 2 bytes on `$&`. Each line therefore represents all potential solutions using that number as the shortest leg.
```
%L$w`(,_+)\b.*\1(_*)\b(?<=^_+\2,.*)
_
```
For each of those lines, find all possible middle and longest legs, but ensuring that the difference is less than the first leg. Output a `_` for each matching combination of legs.
```
_
```
Count the total number of triangles found.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), ~~12~~ ~~10~~ 9 bytes
My first time using 05AB1E! Thanks to [Grimmy](https://codegolf.stackexchange.com/users/6484/) for -1!
```
3.Æʒ`α›}g
```
[Try it online!](https://tio.run/##yy9OTMpM/f/fWO9w26lJCec2PmrYVZv@/3@0oZGOkbGOsYmOiamOqZmOmbmOuYWOhWUsAA "05AB1E – Try It Online") or [test suite](https://tio.run/##yy9OTMpM/V9W@d9Y73DbqUkJ5zY@athVm/5f5390tKGOkY5xrA6QBkIwDeTrmABZQFLHVMccLGZipGNmqWNmZqZjaAAGIFEjHSNjHWMTHRNTHVMzHTNzHXMLHQtLhBlA3WY65joWOpZAXbGxAA)
A direct port of my Stax answer. Get all combinations of three entries and count those that could possibly form triangles. It's that counting part that really got me. I spend a load of bytes there. Bound to be some rookie mistake there.
```
3.Æʒ`α›}g
3.Æ          List of length-3 combinations
   ʒ   }g    Count truthy results under operation:
    `          Push the two shorter sides, then the long one
     α         Absolute difference (negated subtraction in this case)
      ›        Remaining short side is longer?
```
[Answer]
# [Python 2](https://docs.python.org/2/), 72 bytes
```
f=lambda l,p=[]:l>[]and(p==p[:2]<[sum(p)]>l)+f(l[1:],p)+f(l[1:],p+l[:1])
```
[Try it online!](https://tio.run/##TYxBCoMwFET3nsJlUmfRxBg1VC8S/sJipUJMQ6uUnj6NLZTOh8/jwUx4rdeblzFOnRuW8zjkDqGzZFxvafAjC10XrJF0so9tYYFT73gxMWeFIYQ/LJw1gnh8Xmd3yYUJ99mvbGKzD9vKOOfRCkiUlGcJ0n0hGSjKbPqoUNPulIRuobWGOH6yWwlZolRQFSoNXaNu0LT020htjRoN2tTaNR2qNw "Python 2 – Try It Online")
**73 bytes**
```
lambda l:sum(a+b>c for j,b in enumerate(l)for a in l[:j]for c in l[j+1:])
```
[Try it online!](https://tio.run/##PYxNCoMwFIT3niLLpM6iiRp/oL2IzSJaRSVGsUrp6dOkQufBMPPBvPWzD4sVrr89nNFz89TEVK9jpjpu7i3pl41MaMhoSWePudv03lHDAtYBmrqaVGjt2aaYV4q59zCajvBq3Ua7056Odj12yhhzNYdAokjkg78zeIJURbV3ZMhVYKmALCGlBL/@FKiASJCkSDNkEjJHXqAo1f@HX0vkKFD6VcDqkn0B "Python 2 – Try It Online")
[Answer]
# [Octave](https://www.gnu.org/software/octave/) / MATLAB, 33 bytes
```
@(x)sum(nchoosek(x,3)*[1;1;-1]>0)
```
[Try it online!](https://tio.run/##VYzNCsIwEITvPklSxtD8bRJKxfcoOUhpEURzqErfPi7VHty5fDPDThmfl/dU514pVc9ilcvrLh7jtZRluokVVjaD7nR31PnUyjqLQcPAZnnYkLUjp3BfwwCPsDfOgBKICLrd7lcYGAvr4Dw8gQJCREx/ezxDCIhI/Jtl/QA)
[Answer]
# JavaScript (ES6), 63 bytes
```
f=([v,...a],p=[])=>v?(!p[2]&p[0]+p[1]>v)+f(a,p)+f(a,[...p,v]):0
```
[Try it online!](https://tio.run/##jY5BDoIwFET3ngI3pg0jlNIWMEEP0vwFUTAaI42YXh8bNW4Q48zib17en3Pjm2F/O7n7@tof2nHsamY9kiRpCK62xOut37Gls5JWzgqKnc1o63ncsQbudWzAHTzxjRj3/XXoL21y6Y@sYzaDRE7RJJxHaRqJxQQPncWzxTc7FP2JBxQaxQyeT@1KwlQwxiATz9Cv7RIyR66gNLSBKVCUKCt629XM@LDIoECJKjyhj16L8QE "JavaScript (Node.js) – Try It Online")
[Answer]
# [Zsh](https://www.zsh.org/), 66 bytes
```
for a;z=$y&&for b (${@:2+y++})for c (${@:3+z++})((t+=c<a+b))
<<<$t
```
[Try it online!](https://tio.run/##qyrO@J@moanxPy2/SCHRuspWpVJNDcROUtBQqXawMtKu1Nau1QSJJENEjLWrQCIaGiXatsk2idpJmppcNjY2KiX/NbnSFAwhEMGCso0UjBVMECwFUwUzBXMFCwVLBUMDqGKQhClQyND4PwA "Zsh – Try It Online")
Relatively straightforward, taking advantage of the sorted input, and incrementing in the `for` header (the increment happens once per *parent* loop).
```
for a;{
  z=$y
  for b (${@:2+y++});{   # subarray starting at element after $a
    for c (${@:3+z++})   # subarray starting at element after $b
      ((t+=c<a+b))
  }
}
```
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), 14 bytes
```
*1sm>sPded.cQ3
```
[Try it online!](https://tio.run/##K6gsyfj/X8uwONeuOCAlNUUvOdD4//9oQyMdI2MdYxMdE1MdUzMdM3MdcwsdC8tYAA "Pyth – Try It Online")
```
          .cQ3  # All combinations of length 3 from Q (input), sorted in ascending order
   m            # map over that lambda d:
     sPd        #   sum(d[:-1])
    >   ed      #     > d[-1]
  s             # sum all of those (uses the fact that True = 1)
*1              # multiply by 1 so it doesn't output True if there's only one triangle
```
Alternative (also 14 bytes):
```
lfTm>sPded.cQ3
```
[Answer]
# [C (clang)](http://clang.llvm.org/), 83 bytes
```
x,y,q;f(*a,z){for(x=y=q=0;z;q+=z>1&a[x-=x?1:2-y--]+a[y]>a[z])y=y>1?y:--z;return q;}
```
[Try it online!](https://tio.run/##dZHRboIwFIbveYrGZAvM00ihFLQp3my3ewEkhigsJhsThglgfHbXtQzEsfYG/n5/z@n5d3j3nuRv10NeoS2JYnEm4IB74YsFspGhZEfLciuZdLLb00D1gdbpjy418MBXuqt1T/PUAbYExhgQWy1dSzNMMQ44LrgUqAceA@aDH0Cw1HdRDfo3xWUhBj4EsJRXKsrrrgv6zrve6bWGBgqemU8JtNY5@yzNWjSiEDZveTEXbUgek6jGol6TlYMbjON5EjVxmERtbDWiCcm6WWHc8jKtTmWOCn5Rw/tIDrlpobNhHEv5nyFz1o0SiRA97BEyUVof012V7uVkrU0@g8zcEnAti488P70OHtKjzl9Uz34KlvoY/g1kgN0epvfwVEqDcehe5nNn/Ce6m6q0dzMIJh80znOwekNhX55MzG00uaFQoN9nHE/Vlznb5AirjV5en5H@lqAEkFxdrDY3Ltdv "C (clang) – Try It Online")
Saved 1 thanks to @ceilingcat 
[Answer]
# Excel VBA, ~~171~~ ~~164~~ 152 bytes
*-26 bytes thanks to TaylorScott*
```
Sub z
t=[A:A]
u=UBound(t)
For i=1To u-2
For j=i+1To u-1
For k=j+1To u
a=t(i,1):b=t(j,1):c=t(k,1)
r=r-(a+b>c)*(b+c>a)*(c+a>b)
Next k,j,i
Debug.?r
End Sub
```
Input is in the range `A:A` of the active sheet. Output is to the immediate window.
Since this looks at every combination of every cell in a column that's 220 cells tall (which is nearly 260 combinations), this code is... not fast. You could make it *much* faster but at the expense of bytes.
[Answer]
# Java 8, ~~131~~ ~~130~~ 126 bytes
```
a->{int r=0,i=a.length,j,k,I,J,K;for(;i-->0;)for(I=a[j=i];j-->0;)for(J=a[k=j];k-->0;r+=I+J>K&I+K>J&J+K>I?1:0)K=a[k];return r;}
```
Straightforward approach. The triangle check is the same as [my answer for the *Is this a triangle?* challenge](https://codegolf.stackexchange.com/a/199374/52210).  
-4 bytes thanks to *@ceilingcat*.
[Try it online.](https://tio.run/##RVHBjoIwEL37FY0HA2EgiAhqA5vNnoCsF4@EQxerW8BiSnFjCN/OFtTYJm@mrzPte5mC3IhZHMshr0jToG/CeDdDiHFJxYnkFO3H40SgXFOYZojoWHH9TEEjiWQ52iOOgoGYYTfWicAGFhCrovwsf6GAEiKIIcGnWmiYmWZoY33Mo4CkRcAyXLy5WHFlUGS4nDhhBJERh8kiMpIwXsQKo4/lztaTsS7DgspWcCRwP@BR0LX9qZSgp65bzY7oojxpBykYP6cZ0R9@xq9uRCBJG/lFGrrj9A9N9tKs65bgwKoHFdV@xWembsBVmUJYgz9xrgPeFjzPg6U9rZF1wFnBygV3DWsPPB/8DWy27zdUtwc@bGCruvpen3QhdLg3kl6supXWVWmWFdcKNSSrlayyPoUg98aS9cOP9pKvG/Mdmhvcyt/Uc0r98A8)
**Explanation:**
```
a->{                  // Method with integer-array parameter and integer return-type
  int r=0,            //  Result-integer, starting at 0
      i=a.length,j,k, //  Index-integers
      I,J,K;          //  Temp-integers
  for(;i-->0;)        //  Loop `i` in the range (length,0]:
    for(I=a[j=i];     //   Set `I` to the `i`th value
        j-->0;)       //   Inner loop `j` in the range (i,0]:
      for(J=a[k=j];   //    Set `J` to the `j`'th value
          k-->0       //    Inner loop `k` in the range (j,0]:
          ;           //      After every iteration:
           r+=        //       Increase the result by:
              I+J>K&I+K>J&J+K>I?
                      //        If `I`, `J`, and `K` form a triangle:
               1      //         Increase the result by 1
              :       //        Else:
               0)     //         Keep the result the same by increasing with 0
        K=a[k];       //     Set `K` to the `k`'th value
  return r;}          //  After the nested loops, return the result
```
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 17 bytes
```
ＩΣ⭆θ⭆…θκ⭆…θμ›⁺νλι
```
[Try it online!](https://tio.run/##S85ILErOT8z5/z@gKDOvRMM5sbhEI7g0VyO4BMhP900s0CjUUUBwfPNzUkAi2ZrYRHOBou5FqYklqUUaATmlxRp5Ogo5QLFMTQiw/v8/OtrQSMfIWMfYRMfEVMfUTMfMXMfcQsfCMjb2v25ZDgA "Charcoal – Try It Online") Link is to verbose version of code. Assumes sorted input. Explanation:
```
   θ                Input array
  ⭆                 Map over elements and join
      θ             Input array
     …              Truncated to length
       κ            Outer index
    ⭆               Map over elements and join
          θ         Input array
         …          Truncated to length
           μ        Inner index
        ⭆           Map over elements and join
              ν     Innermost value
             ⁺      Plus
               λ    Inner value
            ›       Is greater than
                ι   Outer value
 Σ                  Take the digital sum
Ｉ                   Cast to string for implicit print
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt) [`-x`](https://codegolf.meta.stackexchange.com/a/14339/), 9 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
à3 ËÌÑ<Dx
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LXg&code=4DMgy8zRPER4&input=WzEyLDIzLDM0LDQ1LDU2LDY3LDc4LDg5XQ)
```
à3 ®o <Zx
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LXg&code=4DMgrm8gPFp4&input=WzEyLDIzLDM0LDQ1LDU2LDY3LDc4LDg5XQ)
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), ~~37~~ 35 bytes
```
Tr@Boole[2#3<+##&@@@#~Subsets~{3}]&
```
[Try it online!](https://tio.run/##TYxBCoMwFET3XiPgplNak/gToS2hJyi0O3Fhi6JQK2i6knj1NOqm8xfzePCnK21TdaVtX6Wvz/4xmGvfv6ucM3HaMRYbY9h8/z7Hyo7zJFwR@9vQfmzO9pfasCI@mCmaEnAIhwXCbRAM5IKhkEJtVnJQBiJCclyzag4uICRkipRACkpDZ387YYCgoJGFPxc5/wM "Wolfram Language (Mathematica) – Try It Online")
[Answer]
# [Ruby](https://www.ruby-lang.org/), 41 bytes
```
->l{l.combination(3).count{|a,b,c|c<a+b}}
```
[Try it online!](https://tio.run/##NUzLCsIwELz7FR4Vx2KTdNOA@iMhh6RQKPRFaQ@S9NvjWnAHdh4ws2zhk9tXvr/72BfNNIRu9Gs3jRd5ZbuNa0weAU1qnv4W9j1bW0JAOjAzDmYPxYo/KugjUwJkQEQoH8f9UgEhIRVUhYpAGrpGbRxO/xGuEzRqGK45Vwx@jmlJ87m1i9vzFw "Ruby – Try It Online")
[Answer]
# Perl 5 (`-p`), ~~55~~ 52 bytes
using regex backtracking, -3 bytes thanks to @Cows quack using `^` instead of `(?!)` to fail and backtrack.
```
$d='(\d++)';$_=/$d.* $d.* $d(?{$n++if$1+$2>$3})^/+$n
```
or
```
$_=/(\d++).* (\d++).* (\d++)(?{$n++if$1+$2>$3})^/+$n
```
[TIO](https://tio.run/##NYvNCsIwEITvfYo5LLQ1aM3fpqVUX0T0UoVCmwb1IuKrG0PVGWaGXfjC@TraOD3It5H6Li8OvRBl3tKpq6jfrPCrYv8kL8RwISlI7Ui/ymMlyMcooaAzieRvL5t@MFkKLFy6jQI3YGbI7aJMKigNbWAsLIMdXI26@bOJYzjUaBLxnsN9mP0trsfwAQ)
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 9 bytes
```
œc3+>ƭ/€S
```
[Try it online!](https://tio.run/##y0rNyan8///o5GRjbbtja/UfNa0J/v//v6GRjpGxjrGJjompjqmZjpm5jrmFjoUlAA "Jelly – Try It Online")
A monadic link taking a sorted list of integers as its argument and returning the number of triangles. 
## Explanation
```
œc3       | Combinations of length 3
     ƭ/€  | Reduce each using each of the following in turn:
   +      | - Add
    >     | - Greater than
        S | Sum (counts the 1s)
```
Alternative 9s:
```
œc3Ṫ€<§ƊS
œc3Ṫ<SƊ€S
```
[Answer]
# [Bash](https://www.gnu.org/software/bash/), 123 bytes
```
for a;do for((i=2;i<=$#;i++)){ b=${!i};for((j=$[i+1];j<=$#;j++)){ c=${!j};T=$[T+(a<b+c&b<a+c&c<a+b)];};};shift;done;echo $T
```
[Try it online!](https://tio.run/##JY3LCsMgFER/xVIpBlcxz3L1L9yVLNQmqIsKTXfBb7@9TRkYBs6B8W6PiFt5MwfPwmgIkYyCpA2/QpKyaQ7mDT8uqcJJs@GPJNsF8qnkvxJ@Sq5giVopnPYy3Lx21IHaNwtUyh7T9qGj1wpriIVxi4itQtVh12M/4DDiOOE043z/Ag "Bash – Try It Online")
A fun one.
[Answer]
# [J](http://jsoftware.com/), 40 bytes
```
1#.](+/*/@(->])])@#~2(#~3=1&#.)@#:@i.@^#
```
[Try it online!](https://tio.run/##VY3LDoIwEEX3fMWNTQRUCn1NgQTTxMSVK/e4MRJ14x/w67VSDNpJF/dk7pmnX/F0QNcixQ4V2vALjsP5dPSC8T7blpvSZcW@z/vcsVFmbFSdWDMeUuse3F2Yz5Pkdr2/QrXDAAEJFYGYQZgI9AK@SCwl6IjUhEKEgf1XawlqQEQQ1fTmxiyWkOoTtIEhkIWtUTeIS6b6PRXcBIsaTVDBvwE "J – Try It Online")
[Answer]
# [Nekomata](https://github.com/AlephAlpha/Nekomata) + `-n`, 6 bytes
```
SƆ$đ+<
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m70iLzU7PzexJHFNtJJurpKOgpJunlLsgqWlJWm6FiuCj7WpHJmobbOkOCm5GCq44GZJtKGOkY5xLBeQBkIYDWUBZXRMgCwgqWOqYw4WMzHSMbPUMTMz0zE0AAOQqJGOkbGOsYmOiamOqZmOmbmOuYWOhSXCDKBuMx1zHQsdS6CuWIjlAA)
```
SƆ$đ+<
S       Find a subset of the input
 Ɔ      Split the subset into the last element and the rest
  $đ    Check if the rest has exactly two elements
    +   Add these two elements
     <  Check if the last element is less than the sum
```
`-n` counts the number of solutions.
[Answer]
# [SNOBOL4 (CSNOBOL4)](http://www.snobol4.org/csnobol4/), 181 bytes
```
	S =TABLE()
R	X =X + 1
	S<X> =INPUT	:S(R)
I	I =J =K =I + 1	LT(I,X)	:F(O)
J	J =K =J + 1	LT(J,X)	:F(I)
K	K =K + 1	LT(K,X - 1)	:F(J)
	T =T + 1 GT(S<I> + S<J>,S<K>)	:(K)
O	OUTPUT =T
END
```
[Try it online!](https://tio.run/##Nc4/D4IwEIfh@e5T3NhGHPgrkpZEI5q2BAwtSWdnI4PfP7Uobpd73uH3fi2P5VmEAJakO537jnGcwJP0tKMUwQrfklTDfXbQWDZxVKBIapImvtcGesdU4jk0VzZy1PAz/Te9meJowKy2gUk87Sn9ouYILi5YjW6OWaHaeFuh28QK08aIGY4jjLOLU2KJ3XAJIc0wyzEvsCixrLA64KHG@vgB "SNOBOL4 (CSNOBOL4) – Try It Online")
Brute force \$O(n^3)\$ algorithm. Takes input as a newline separated list and outputs the number of triangles, or an empty line for `0`. This is probably allowable since SNOBOL treats the empty string as `0` for numerical calculations.
[Answer]
# [Scala](http://www.scala-lang.org/), 280 bytes
280 bytes, it can be golfed much more.
---
Golfed version. [Try it online!](https://tio.run/##dVLLbtswELzrK/ZkcGFasONHYiU00PZUoD0VySUIAoqiEhY0JYhU0dTQt7tLKk2awCHEh3Z2ZpcjeSWtPJp923QBfHzJVWOtVsE0Lt/3QZZW55@6Tj597utad1nWlD8Jhu/SODgcK12DYq746gK3xTfjwy0d73A8vrzfCVMzJ8QcY4ilBVFbryECO5t780eP4GtYiAXafC/bkXA/Mg6sbjpmrmY2N64ySnt8MtpWh1/SwqOwzOBlPAZh86prWmamC7ykJmcLHjDJPRbFPQ6YhyYK57WVIWg3ZPE2N@zNPWgVii25RXKmd@GgJHWX@pG85ArFTk7LnZpM5FTtysmkpE0OR4AotieXmOwefAHJxNsfoTPugXTh2pkAAg4Z0Ej9ah@@kLinaNJPCMB4@QWHMw5L5DBH/h6JD6Z5isNhdQKMYQ5rDueYdN8zV0TdbGluNkSep3GqOqWdkdaSxFaktqbszTmpXnC42Ebp1YddpfKUT@mUvY1liLCeY8rHLG30sYEx6wMH/bulX09XCFezV7vw2cPRxaYPbR@NvYkcfEak97oL7B8oXqQ4jNIj8l@JkTlkcQ7Hvw)
```
def c(n:Int,l:List[Int]):List[List[Int]]=if(n==0)List(List())else if(n>l.size)List()else if(n==1)l.map(List(_))else{(for(i<-l.indices)yield{val h=l(i);val t=l.drop(i+1);c(n-1,t).map(h::_)}).toList.flatten}
def V(l:List[Int]):Int=c(3,l).count{case List(a,b,c)=>a+b>c&&a+c>b&&b+c>a}
```
Ungolfed version. [Try it online!](https://tio.run/##dVNNb9swDL37V/BUWJhqpM1HG2MusO40YDvt41IUgWzLmTZZMiR5WDfkt2cUnRhOkwVxHIqPj8@PtK@EFvu9ajvrAvgYZZXVWlZBWZO1fRClltk758TLY9800iUn2D4ojQUmOKuzRyfFT59tksSWP5ABPgll4G8CUMsGKtuWyojIm5ocPpjAQfuQw0flwxOGz@zwfzx4hoLKAVQDqYGigBkjTEo/jFFSai8PiIdImXn1Rx5wZwjkuGGEakU30GymPEM/gLSxDlIFb68JrEytKukZvCip6xEF8Eto@C5FjVIRlyp2kglC6SGT1c52yPcG24@QE0/gGm44VTDSRqx5DpsjfseyYKPirNEiBGnofJfEazS5N@Gb0Kr@4pQwWy19emYy3kZjo8iJCo@Jqag5zWgQMIVl1Gi0oRLoHHkpOJQcKgbFAwh82BJHUsHVFQUVBmUMykMgzp@gxZ1Jhdv6HGjrnj4Hp8w26v5q1KnwIH14j62jaup@nB0FaOYthznjuDT8dSZ@GV2XajgsLiTjMYclhztGvK8rF1i6WuO1WmHxjD6XuiPsFrnmSLZAtiWiV3fIes/hfh2pF/9VRe0Rj3BEr2MbLFjOhgmxhG60uXHsHOTvDl9EWbO4x6NdbBxcdNH2oesDDf7i8hzXT3gvXUiP8GIk5zA0GzKTpmwy3t1@/w8)
```
import scala.collection.mutable.ArrayBuffer
import scala.util.control.Breaks._
object Main {
  def combination(n: Int, lst: List[Int]): List[List[Int]] = {
    if (n == 0) List(List())
    else if (n > lst.size) List()
    else if (n == 1) lst.map(List(_))
    else {
      (for (i <- lst.indices) yield {
        val head = lst(i)
        val tail = lst.drop(i + 1)
        combination(n - 1, tail).map(head :: _)
      }).toList.flatten
    }
  }
  def countValidTriangles(lst: List[Int]): Int = {
    val combinations = combination(3, lst)
    combinations.count {
      case List(a, b, c) => a + b > c && a + c > b && b + c > a
    }
  }
  def main(args: Array[String]): Unit = {
    val testCases = List(
      (List(1, 2, 3), 0),
      (List(1, 1, 1), 1),
      (List(1, 2, 3, 4), 1),
      (List(3, 4, 5, 7), 3),
      (List(1, 42, 69, 666, 1000000), 0),
      (List(12, 23, 34, 45, 56, 67, 78, 89), 34),
      (List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 50)
    )
    for ((lst, expected) <- testCases) {
      val output = countValidTriangles(lst)
      assert(output == expected, (lst, output, expected))
    }
  }
}
```
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 49 bytes
```
f=([v,...x],a,b)=>v?f(x,a,b)+(b?a+b>v:f(x,v,a)):0
```
[Try it online!](https://tio.run/##jY5BDoIwEEX3noJlG75QSmmBBDgI6aIgGA0BI6bh9ojEuEGMfzaTycubfzXWjPX9cnsc@@HUzHObkdLC87xJw6CiWW6Llkzr7pKqMG6V2/R1sTCUpmyuh34cusbrhjNpSRmAI9TOJpQ6vu@wwwZfZhcPDt/sEPpPfEERQe3g4dYuOGQCKSUCtkb/6s7BQ4QCIkIkIRVUjDjRb7vYKb80klCIkSxP9EcfsfkJ "JavaScript (Node.js) – Try It Online")
[Answer]
# [Uiua](https://www.uiua.org/), 27 bytes (SBCS)
```
/+≡(|1<+/∘⇌)≐▽▽=3≡/+.⋯⇡ⁿ⧻,2
```
[Try it online!](https://www.uiua.org/pad?src=VHJpYW5nbGVzIOKGkCAvK-KJoSh8MSA8Ky_iiJjih4wp4omQ4pa94pa9PTPiiaEvKy7ii6_ih6Higb_ip7ssMgoKVHJpYW5nbGVzIFsxIDIgM10gICAgICAgICAgICAgICAgICAgIyBFeHBlY3RlZDogMApUcmlhbmdsZXMgWzEgMSAxXSAgICAgICAgICAgICAgICAgICAjIEV4cGVjdGVkOiAxClRyaWFuZ2xlcyBbMSAxIDEgMV0gICAgICAgICAgICAgICAgICMgRXhwZWN0ZWQ6IDQKVHJpYW5nbGVzIFsxIDIgMyA0XSAgICAgICAgICAgICAgICAgIyBFeHBlY3RlZDogMQpUcmlhbmdsZXMgWzMgNCA1IDddICAgICAgICAgICAgICAgICAjIEV4cGVjdGVkOiAzClRyaWFuZ2xlcyBbMSA0MiA2OSA2NjYgMTAwMDAwMF0gICAgICMgRXhwZWN0ZWQ6IDAKVHJpYW5nbGVzIFsxMiAyMyAzNCA0NSA1NiA2NyA3OCA4OV0gIyBFeHBlY3RlZDogMzQKVHJpYW5nbGVzIFsxIDIgMyA0IDUgNiA3IDggOSAxMF0gICAgIyBFeHBlY3RlZDogNTAK)
**Explanation**
```
            ≐▽▽=3≡/+.⋯⇡ⁿ⧻,2   # Get all length 3 combinations
                           2   # Put 2 on the stack
                          ,    # Copy the array onto the stack
                         ⧻     # Get the length of the array
                        ⁿ      # Raise 2 to the power of the length
                       ⇡       # Create an array of all natural numbers less than the result
                     ⋯         # Convert to binary
                    .          # Copy the result
                 ≡/+           # For each binary number, add up the bits
               =3              # Check which add up to three
              ▽                # Keep rows of the binary array where the number of 1s is 3
            ≐▽                 # For each of the remaining binary numbers,
                               # keep items in the original array
/+≡(|1<+/∘⇌)                   # Calculate number of triangles
  ≡(|1     )                   # For each row in the resulting array:
          ⇌                    # Reverse the array
        /∘                     # Put the items onto the stack
       +                       # Add the first two numbers
      <                        # Check if the third number is less than the result
/+                             # Add up all the triangles
```
As part of this, I made the combinations part generic. This might be useful for future golfing. If someone can find a shorter way of doing this part, it may help many people.
```
≐▽▽=⊙(≡/+.⋯⇡ⁿ⧻,2)
```
[Try it online!](https://www.uiua.org/pad?src=Q29tYmluYXRpb25zIOKGkCDiiZDilr3ilr094oqZKOKJoS8rLuKLr-KHoeKBv-KnuywyKQoKQ29tYmluYXRpb25zIDMgM180XzVfNwpDb21iaW5hdGlvbnMgNCA5XzhfNF83XzFfNgo=)
]