Count the number of trianglesNumber of holes in a polygonIdentifying the trianglesStacking Pythagorean TrianglesIntegral triangles and integral mediansBuild a triangle without any trianglesEvaluate the aspect ratio of a triangleWhat are my dimensions?Integer triangles with perimeter less than nThe Digit Triangles
Did the Apollo Guidance Computer really use 60% of the world's ICs in 1963?
Can I take a boxed bicycle on a German train?
How to prevent a hosting company from accessing a VM's encryption keys?
How to determine algebraically whether an equation has an infinite solutions or not?
What's the point of fighting monsters in Zelda BoTW?
Find feasible point in polynomial time in linear programming
Videos of surgery
Many many thanks
Term used to describe a person who predicts future outcomes
Drawing probabilities on a simplex in TikZ
Can a paladin prepare more spells if they didn't cast any the previous day?
How to say "I only speak one which is English." in French?
Why does matter stay collapsed in the core, following a supernova explosion?
Grep contents before a colon
How to pass 2>/dev/null as a variable?
Toroidal Heyacrazy: Rainstorm
Biological refrigeration?
Why does Windows store Wi-Fi passwords in a reversible format?
Why is getting a PhD considered "financially irresponsible" by some people?
Force SQL Server to use fragmented indexes?
Is there a word or phrase that means "use other people's wifi or Internet service without consent"?
Stolen MacBook should I worry about my data?
Do sharpies or markers damage soft rock climbing gear?
Is it unusual for a math department not to have a mail/web server?
Count the number of triangles
Number of holes in a polygonIdentifying the trianglesStacking Pythagorean TrianglesIntegral triangles and integral mediansBuild a triangle without any trianglesEvaluate the aspect ratio of a triangleWhat are my dimensions?Integer triangles with perimeter less than nThe Digit Triangles
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
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.)
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 numbersa[i], a[j], a[k]
(wherei,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!
Input, Output:
[1,2,3] 0
[1,1,1] 1
[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.
For the input of [1,1,...,1]
(length n
) this is A000292.
For the input of the first n
Fibonacci numbers (A000045) this is A000004.
code-golf sequence array-manipulation integer geometry
$endgroup$
add a comment |
$begingroup$
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.)
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 numbersa[i], a[j], a[k]
(wherei,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!
Input, Output:
[1,2,3] 0
[1,1,1] 1
[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.
For the input of [1,1,...,1]
(length n
) this is A000292.
For the input of the first n
Fibonacci numbers (A000045) this is A000004.
code-golf sequence array-manipulation integer geometry
$endgroup$
2
$begingroup$
I think the challenge could be clearer about what counts as a distinct triangle. From the A000292 link, I take it[1,1,1,1]
allows 4 "different" triangles, all[1,1,1]
, to be chosen using any three of the 1's? But, it's not 24 because the three 1's are chosen unordered, i.e. it's a subset of three indices rather than an ordered list?
$endgroup$
– xnor
7 hours ago
1
$begingroup$
@xnor Thatnks for pointing this out, that all seems correct - I just added a point in the details. I hope that makes it clearer now.
$endgroup$
– flawr
7 hours ago
1
$begingroup$
I think[1,1,1,1]
should be added as a test case, to further clarify xnor's point
$endgroup$
– Luis Mendo
4 hours ago
add a comment |
$begingroup$
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.)
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 numbersa[i], a[j], a[k]
(wherei,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!
Input, Output:
[1,2,3] 0
[1,1,1] 1
[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.
For the input of [1,1,...,1]
(length n
) this is A000292.
For the input of the first n
Fibonacci numbers (A000045) this is A000004.
code-golf sequence array-manipulation integer geometry
$endgroup$
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.)
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 numbersa[i], a[j], a[k]
(wherei,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!
Input, Output:
[1,2,3] 0
[1,1,1] 1
[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.
For the input of [1,1,...,1]
(length n
) this is A000292.
For the input of the first n
Fibonacci numbers (A000045) this is A000004.
code-golf sequence array-manipulation integer geometry
code-golf sequence array-manipulation integer geometry
edited 7 hours ago
flawr
asked 8 hours ago
flawrflawr
29.1k6 gold badges75 silver badges202 bronze badges
29.1k6 gold badges75 silver badges202 bronze badges
2
$begingroup$
I think the challenge could be clearer about what counts as a distinct triangle. From the A000292 link, I take it[1,1,1,1]
allows 4 "different" triangles, all[1,1,1]
, to be chosen using any three of the 1's? But, it's not 24 because the three 1's are chosen unordered, i.e. it's a subset of three indices rather than an ordered list?
$endgroup$
– xnor
7 hours ago
1
$begingroup$
@xnor Thatnks for pointing this out, that all seems correct - I just added a point in the details. I hope that makes it clearer now.
$endgroup$
– flawr
7 hours ago
1
$begingroup$
I think[1,1,1,1]
should be added as a test case, to further clarify xnor's point
$endgroup$
– Luis Mendo
4 hours ago
add a comment |
2
$begingroup$
I think the challenge could be clearer about what counts as a distinct triangle. From the A000292 link, I take it[1,1,1,1]
allows 4 "different" triangles, all[1,1,1]
, to be chosen using any three of the 1's? But, it's not 24 because the three 1's are chosen unordered, i.e. it's a subset of three indices rather than an ordered list?
$endgroup$
– xnor
7 hours ago
1
$begingroup$
@xnor Thatnks for pointing this out, that all seems correct - I just added a point in the details. I hope that makes it clearer now.
$endgroup$
– flawr
7 hours ago
1
$begingroup$
I think[1,1,1,1]
should be added as a test case, to further clarify xnor's point
$endgroup$
– Luis Mendo
4 hours ago
2
2
$begingroup$
I think the challenge could be clearer about what counts as a distinct triangle. From the A000292 link, I take it
[1,1,1,1]
allows 4 "different" triangles, all [1,1,1]
, to be chosen using any three of the 1's? But, it's not 24 because the three 1's are chosen unordered, i.e. it's a subset of three indices rather than an ordered list?$endgroup$
– xnor
7 hours ago
$begingroup$
I think the challenge could be clearer about what counts as a distinct triangle. From the A000292 link, I take it
[1,1,1,1]
allows 4 "different" triangles, all [1,1,1]
, to be chosen using any three of the 1's? But, it's not 24 because the three 1's are chosen unordered, i.e. it's a subset of three indices rather than an ordered list?$endgroup$
– xnor
7 hours ago
1
1
$begingroup$
@xnor Thatnks for pointing this out, that all seems correct - I just added a point in the details. I hope that makes it clearer now.
$endgroup$
– flawr
7 hours ago
$begingroup$
@xnor Thatnks for pointing this out, that all seems correct - I just added a point in the details. I hope that makes it clearer now.
$endgroup$
– flawr
7 hours ago
1
1
$begingroup$
I think
[1,1,1,1]
should be added as a test case, to further clarify xnor's point$endgroup$
– Luis Mendo
4 hours ago
$begingroup$
I think
[1,1,1,1]
should be added as a test case, to further clarify xnor's point$endgroup$
– Luis Mendo
4 hours ago
add a comment |
17 Answers
17
active
oldest
votes
$begingroup$
R, 62 52 bytes
function(l)sum(combn(l,3,function(x)x[3]<x[1]+x[2]))
Try it online!
combn
to the rescue!
combn
generates all combinations of l
of size n
(in this case 3), optionally applying a function to each combination. In this case, we apply a triangle inequality test, and sum the resulting list to get the number out.
$endgroup$
$begingroup$
What about this?
$endgroup$
– Robert S.
5 hours ago
2
$begingroup$
x[3]<x[1]+x[2]
is equivalent to2*x[3]<sum(x)
: 51 bytes
$endgroup$
– Robin Ryder
5 hours ago
$begingroup$
47 bytes by callingcombn
twice and avoiding the inner function definition.
$endgroup$
– Robin Ryder
5 hours ago
1
$begingroup$
Actually, make that 45 bytes. Sorry for the multiple comments!
$endgroup$
– Robin Ryder
5 hours ago
add a comment |
$begingroup$
Stax, 8 7 bytes
Thanks to recursive for -1!
é═rê÷┐↨
Run and debug it at staxlang.xyz!
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 b:r<-scanr(:)[]t,c<-r,a+b>c]
f _=0
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 the third element.
$endgroup$
add a comment |
$begingroup$
Perl 6, 35 bytes
+*.combinations(3).flat.grep(*+*>*)
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.)
$endgroup$
add a comment |
$begingroup$
Excel VBA, 171 bytes
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)
If a+b>c And b+c>a And c+a>b Then r=r+1
Next:Next:Next
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.
$endgroup$
add a comment |
$begingroup$
Jelly, 9 bytes
œc3+>ƭ/€S
Try it online!
A monadic link taking a sorted list of integers as its argument and returning the number of triangles.
Alternative 9s:
œc3Ṫ€<§ƊS
œc3Ṫ<SƊ€S
$endgroup$
add a comment |
$begingroup$
Japt -x
, 9 bytes
à3 ËÌÑ<Dx
Try it
à3 ®o <Zx
Try it
$endgroup$
add a comment |
$begingroup$
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!
$endgroup$
add a comment |
$begingroup$
Charcoal, 17 bytes
IΣ⭆θ⭆…θκ⭆…θμ›⁺νλι
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
I Cast to string for implicit print
$endgroup$
add a comment |
$begingroup$
Octave / MATLAB, 33 bytes
@(x)sum(nchoosek(x,3)*[1;1;-1]>0)
Try it online!
$endgroup$
add a comment |
$begingroup$
Zsh, 66 bytes
for a;z=$y&&for b ($@:2+y++)for c ($@:3+z++)((t+=c<a+b))
<<<$t
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))
$endgroup$
add a comment |
$begingroup$
05AB1E, 12 10 bytes
Oh, dear. My first time using 05AB1E. This is (less) ugly (now). You have been warned.
3.Æʒy`Š+‹}g
Try it online! or test suite
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 long side, then the two shorter ones
+ Add the short ones
‹ Long side is shorter than this sum?
I'll be looking into shortening this. There's gotta be a way to drop the }
.
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 37 35 bytes
Tr@Boole[2#3<+##&@@@#~Subsets~3]&
Try it online!
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "200"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f190949%2fcount-the-number-of-triangles%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
17 Answers
17
active
oldest
votes
17 Answers
17
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
R, 62 52 bytes
function(l)sum(combn(l,3,function(x)x[3]<x[1]+x[2]))
Try it online!
combn
to the rescue!
combn
generates all combinations of l
of size n
(in this case 3), optionally applying a function to each combination. In this case, we apply a triangle inequality test, and sum the resulting list to get the number out.
$endgroup$
$begingroup$
What about this?
$endgroup$
– Robert S.
5 hours ago
2
$begingroup$
x[3]<x[1]+x[2]
is equivalent to2*x[3]<sum(x)
: 51 bytes
$endgroup$
– Robin Ryder
5 hours ago
$begingroup$
47 bytes by callingcombn
twice and avoiding the inner function definition.
$endgroup$
– Robin Ryder
5 hours ago
1
$begingroup$
Actually, make that 45 bytes. Sorry for the multiple comments!
$endgroup$
– Robin Ryder
5 hours ago
add a comment |
$begingroup$
R, 62 52 bytes
function(l)sum(combn(l,3,function(x)x[3]<x[1]+x[2]))
Try it online!
combn
to the rescue!
combn
generates all combinations of l
of size n
(in this case 3), optionally applying a function to each combination. In this case, we apply a triangle inequality test, and sum the resulting list to get the number out.
$endgroup$
$begingroup$
What about this?
$endgroup$
– Robert S.
5 hours ago
2
$begingroup$
x[3]<x[1]+x[2]
is equivalent to2*x[3]<sum(x)
: 51 bytes
$endgroup$
– Robin Ryder
5 hours ago
$begingroup$
47 bytes by callingcombn
twice and avoiding the inner function definition.
$endgroup$
– Robin Ryder
5 hours ago
1
$begingroup$
Actually, make that 45 bytes. Sorry for the multiple comments!
$endgroup$
– Robin Ryder
5 hours ago
add a comment |
$begingroup$
R, 62 52 bytes
function(l)sum(combn(l,3,function(x)x[3]<x[1]+x[2]))
Try it online!
combn
to the rescue!
combn
generates all combinations of l
of size n
(in this case 3), optionally applying a function to each combination. In this case, we apply a triangle inequality test, and sum the resulting list to get the number out.
$endgroup$
R, 62 52 bytes
function(l)sum(combn(l,3,function(x)x[3]<x[1]+x[2]))
Try it online!
combn
to the rescue!
combn
generates all combinations of l
of size n
(in this case 3), optionally applying a function to each combination. In this case, we apply a triangle inequality test, and sum the resulting list to get the number out.
edited 6 hours ago
answered 8 hours ago
GiuseppeGiuseppe
19.2k3 gold badges16 silver badges67 bronze badges
19.2k3 gold badges16 silver badges67 bronze badges
$begingroup$
What about this?
$endgroup$
– Robert S.
5 hours ago
2
$begingroup$
x[3]<x[1]+x[2]
is equivalent to2*x[3]<sum(x)
: 51 bytes
$endgroup$
– Robin Ryder
5 hours ago
$begingroup$
47 bytes by callingcombn
twice and avoiding the inner function definition.
$endgroup$
– Robin Ryder
5 hours ago
1
$begingroup$
Actually, make that 45 bytes. Sorry for the multiple comments!
$endgroup$
– Robin Ryder
5 hours ago
add a comment |
$begingroup$
What about this?
$endgroup$
– Robert S.
5 hours ago
2
$begingroup$
x[3]<x[1]+x[2]
is equivalent to2*x[3]<sum(x)
: 51 bytes
$endgroup$
– Robin Ryder
5 hours ago
$begingroup$
47 bytes by callingcombn
twice and avoiding the inner function definition.
$endgroup$
– Robin Ryder
5 hours ago
1
$begingroup$
Actually, make that 45 bytes. Sorry for the multiple comments!
$endgroup$
– Robin Ryder
5 hours ago
$begingroup$
What about this?
$endgroup$
– Robert S.
5 hours ago
$begingroup$
What about this?
$endgroup$
– Robert S.
5 hours ago
2
2
$begingroup$
x[3]<x[1]+x[2]
is equivalent to 2*x[3]<sum(x)
: 51 bytes$endgroup$
– Robin Ryder
5 hours ago
$begingroup$
x[3]<x[1]+x[2]
is equivalent to 2*x[3]<sum(x)
: 51 bytes$endgroup$
– Robin Ryder
5 hours ago
$begingroup$
47 bytes by calling
combn
twice and avoiding the inner function definition.$endgroup$
– Robin Ryder
5 hours ago
$begingroup$
47 bytes by calling
combn
twice and avoiding the inner function definition.$endgroup$
– Robin Ryder
5 hours ago
1
1
$begingroup$
Actually, make that 45 bytes. Sorry for the multiple comments!
$endgroup$
– Robin Ryder
5 hours ago
$begingroup$
Actually, make that 45 bytes. Sorry for the multiple comments!
$endgroup$
– Robin Ryder
5 hours ago
add a comment |
$begingroup$
Stax, 8 7 bytes
Thanks to recursive for -1!
é═rê÷┐↨
Run and debug it at staxlang.xyz!
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 improve this answer
$endgroup$
Brachylog, 11 bytes
⊇Ṫ.k+>~tᶜ
Try it online!
I may have forgotten to take advantage of the sorted input in my old solution:
Brachylog, 18 17 15 bytes
⊇Ṫ¬p.k+≤~tᶜ
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.
edited 6 hours ago
answered 7 hours ago
Unrelated StringUnrelated String
3,4642 gold badges3 silver badges19 bronze badges
3,4642 gold badges3 silver badges19 bronze badges
add a comment |
add a comment |
$begingroup$
Perl 6, 35 bytes
+*.combinations(3).flat.grep(*+*>*)
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.)
$endgroup$
add a comment |
$begingroup$
Perl 6, 35 bytes
+*.combinations(3).flat.grep(*+*>*)
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.)
$endgroup$
add a comment |
$begingroup$
Perl 6, 35 bytes
+*.combinations(3).flat.grep(*+*>*)
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.)
$endgroup$
Perl 6, 35 bytes
+*.combinations(3).flat.grep(*+*>*)
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.)
answered 6 hours ago
RamilliesRamillies
1,7317 silver badges15 bronze badges
1,7317 silver badges15 bronze badges
add a comment |
add a comment |
$begingroup$
Excel VBA, 171 bytes
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)
If a+b>c And b+c>a And c+a>b Then r=r+1
Next:Next:Next
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.
$endgroup$
add a comment |
$begingroup$
Excel VBA, 171 bytes
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)
If a+b>c And b+c>a And c+a>b Then r=r+1
Next:Next:Next
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.
$endgroup$
add a comment |
$begingroup$
Excel VBA, 171 bytes
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)
If a+b>c And b+c>a And c+a>b Then r=r+1
Next:Next:Next
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.
$endgroup$
Excel VBA, 171 bytes
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)
If a+b>c And b+c>a And c+a>b Then r=r+1
Next:Next:Next
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.
answered 6 hours ago
Engineer ToastEngineer Toast
5,20514 silver badges38 bronze badges
5,20514 silver badges38 bronze badges
add a comment |
add a comment |
$begingroup$
Jelly, 9 bytes
œc3+>ƭ/€S
Try it online!
A monadic link taking a sorted list of integers as its argument and returning the number of triangles.
Alternative 9s:
œc3Ṫ€<§ƊS
œc3Ṫ<SƊ€S
$endgroup$
add a comment |
$begingroup$
Jelly, 9 bytes
œc3+>ƭ/€S
Try it online!
A monadic link taking a sorted list of integers as its argument and returning the number of triangles.
Alternative 9s:
œc3Ṫ€<§ƊS
œc3Ṫ<SƊ€S
$endgroup$
add a comment |
$begingroup$
Jelly, 9 bytes
œc3+>ƭ/€S
Try it online!
A monadic link taking a sorted list of integers as its argument and returning the number of triangles.
Alternative 9s:
œc3Ṫ€<§ƊS
œc3Ṫ<SƊ€S
$endgroup$
Jelly, 9 bytes
œc3+>ƭ/€S
Try it online!
A monadic link taking a sorted list of integers as its argument and returning the number of triangles.
Alternative 9s:
œc3Ṫ€<§ƊS
œc3Ṫ<SƊ€S
edited 5 hours ago
answered 6 hours ago
Nick KennedyNick Kennedy
6,1571 gold badge9 silver badges15 bronze badges
6,1571 gold badge9 silver badges15 bronze badges
add a comment |
add a comment |
$begingroup$
Japt -x
, 9 bytes
à3 ËÌÑ<Dx
Try it
à3 ®o <Zx
Try it
$endgroup$
add a comment |
$begingroup$
Japt -x
, 9 bytes
à3 ËÌÑ<Dx
Try it
à3 ®o <Zx
Try it
$endgroup$
add a comment |
$begingroup$
Japt -x
, 9 bytes
à3 ËÌÑ<Dx
Try it
à3 ®o <Zx
Try it
$endgroup$
Japt -x
, 9 bytes
à3 ËÌÑ<Dx
Try it
à3 ®o <Zx
Try it
answered 4 hours ago
ShaggyShaggy
21.3k3 gold badges21 silver badges72 bronze badges
21.3k3 gold badges21 silver badges72 bronze badges
add a comment |
add a comment |
$begingroup$
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!
$endgroup$
add a comment |
$begingroup$
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!
$endgroup$
add a comment |
$begingroup$
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!
$endgroup$
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!
answered 5 hours ago
ArnauldArnauld
91.3k7 gold badges107 silver badges372 bronze badges
91.3k7 gold badges107 silver badges372 bronze badges
add a comment |
add a comment |
$begingroup$
Charcoal, 17 bytes
IΣ⭆θ⭆…θκ⭆…θμ›⁺νλι
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
I Cast to string for implicit print
$endgroup$
add a comment |
$begingroup$
Charcoal, 17 bytes
IΣ⭆θ⭆…θκ⭆…θμ›⁺νλι
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
I Cast to string for implicit print
$endgroup$
add a comment |
$begingroup$
Charcoal, 17 bytes
IΣ⭆θ⭆…θκ⭆…θμ›⁺νλι
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
I Cast to string for implicit print
$endgroup$
Charcoal, 17 bytes
IΣ⭆θ⭆…θκ⭆…θμ›⁺νλι
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
I Cast to string for implicit print
answered 5 hours ago
NeilNeil
88.1k8 gold badges46 silver badges186 bronze badges
88.1k8 gold badges46 silver badges186 bronze badges
add a comment |
add a comment |
$begingroup$
Octave / MATLAB, 33 bytes
@(x)sum(nchoosek(x,3)*[1;1;-1]>0)
Try it online!
$endgroup$
add a comment |
$begingroup$
Octave / MATLAB, 33 bytes
@(x)sum(nchoosek(x,3)*[1;1;-1]>0)
Try it online!
$endgroup$
add a comment |
$begingroup$
Octave / MATLAB, 33 bytes
@(x)sum(nchoosek(x,3)*[1;1;-1]>0)
Try it online!
$endgroup$
Octave / MATLAB, 33 bytes
@(x)sum(nchoosek(x,3)*[1;1;-1]>0)
Try it online!
answered 4 hours ago
Luis MendoLuis Mendo
77.8k8 gold badges95 silver badges303 bronze badges
77.8k8 gold badges95 silver badges303 bronze badges
add a comment |
add a comment |
$begingroup$
Zsh, 66 bytes
for a;z=$y&&for b ($@:2+y++)for c ($@:3+z++)((t+=c<a+b))
<<<$t
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))
$endgroup$
add a comment |
$begingroup$
Zsh, 66 bytes
for a;z=$y&&for b ($@:2+y++)for c ($@:3+z++)((t+=c<a+b))
<<<$t
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))
$endgroup$
add a comment |
$begingroup$
Zsh, 66 bytes
for a;z=$y&&for b ($@:2+y++)for c ($@:3+z++)((t+=c<a+b))
<<<$t
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))
$endgroup$
Zsh, 66 bytes
for a;z=$y&&for b ($@:2+y++)for c ($@:3+z++)((t+=c<a+b))
<<<$t
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))
answered 3 hours ago
GammaFunctionGammaFunction
9118 bronze badges
9118 bronze badges
add a comment |
add a comment |
$begingroup$
05AB1E, 12 10 bytes
Oh, dear. My first time using 05AB1E. This is (less) ugly (now). You have been warned.
3.Æʒy`Š+‹}g
Try it online! or test suite
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 long side, then the two shorter ones
+ Add the short ones
‹ Long side is shorter than this sum?
I'll be looking into shortening this. There's gotta be a way to drop the }
.
$endgroup$
add a comment |
$begingroup$
05AB1E, 12 10 bytes
Oh, dear. My first time using 05AB1E. This is (less) ugly (now). You have been warned.
3.Æʒy`Š+‹}g
Try it online! or test suite
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 long side, then the two shorter ones
+ Add the short ones
‹ Long side is shorter than this sum?
I'll be looking into shortening this. There's gotta be a way to drop the }
.
$endgroup$
add a comment |
$begingroup$
05AB1E, 12 10 bytes
Oh, dear. My first time using 05AB1E. This is (less) ugly (now). You have been warned.
3.Æʒy`Š+‹}g
Try it online! or test suite
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 long side, then the two shorter ones
+ Add the short ones
‹ Long side is shorter than this sum?
I'll be looking into shortening this. There's gotta be a way to drop the }
.
$endgroup$
05AB1E, 12 10 bytes
Oh, dear. My first time using 05AB1E. This is (less) ugly (now). You have been warned.
3.Æʒy`Š+‹}g
Try it online! or test suite
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 long side, then the two shorter ones
+ Add the short ones
‹ Long side is shorter than this sum?
I'll be looking into shortening this. There's gotta be a way to drop the }
.
edited 2 hours ago
answered 2 hours ago
Khuldraeseth na'BaryaKhuldraeseth na'Barya
2,0858 silver badges32 bronze badges
2,0858 silver badges32 bronze badges
add a comment |
add a comment |
$begingroup$
Wolfram Language (Mathematica), 37 35 bytes
Tr@Boole[2#3<+##&@@@#~Subsets~3]&
Try it online!
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 37 35 bytes
Tr@Boole[2#3<+##&@@@#~Subsets~3]&
Try it online!
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 37 35 bytes
Tr@Boole[2#3<+##&@@@#~Subsets~3]&
Try it online!
$endgroup$
Wolfram Language (Mathematica), 37 35 bytes
Tr@Boole[2#3<+##&@@@#~Subsets~3]&
Try it online!
edited 40 mins ago
answered 6 hours ago
attinatattinat
2,1872 silver badges10 bronze badges
2,1872 silver badges10 bronze badges
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f190949%2fcount-the-number-of-triangles%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
$begingroup$
I think the challenge could be clearer about what counts as a distinct triangle. From the A000292 link, I take it
[1,1,1,1]
allows 4 "different" triangles, all[1,1,1]
, to be chosen using any three of the 1's? But, it's not 24 because the three 1's are chosen unordered, i.e. it's a subset of three indices rather than an ordered list?$endgroup$
– xnor
7 hours ago
1
$begingroup$
@xnor Thatnks for pointing this out, that all seems correct - I just added a point in the details. I hope that makes it clearer now.
$endgroup$
– flawr
7 hours ago
1
$begingroup$
I think
[1,1,1,1]
should be added as a test case, to further clarify xnor's point$endgroup$
– Luis Mendo
4 hours ago