Code Golf Asked on December 21, 2021
Write a function/program that, given three positive integers a
, b
and c
, prints a Truthy value if a triangle (any triangle) could have side lengths a
, b
and c
and outputs a Falsy value otherwise.
Three positive integers in any sensible format, for example:
f(2, 3, 5)
[2,3,5]
"2,3,5"
You may not assume the inputs are sorted.
A Truthy value if a triangle could have the sides with the lengths given, Falsy otherwise.
1, 1, 1 -> Truthy
1, 2, 3 -> Falsy
2, 1, 3 -> Falsy
1, 3, 2 -> Falsy
3, 2, 1 -> Falsy
3, 1, 2 -> Falsy
2, 2, 2 -> Truthy
3, 4, 5 -> Truthy
3, 5, 4 -> Truthy
5, 3, 4 -> Truthy
5, 4, 3 -> Truthy
10, 9, 3 -> Truthy
100, 10, 10 -> Falsy
This is code-golf so shortest solution in bytes, wins. Consider upvoting this challenge if you have fun solving it and… Happy golfing!
Answered by Razetime on December 21, 2021
Answered by Glen O on December 21, 2021
8B D0 MOV DX, AX ; DX = a
03 C3 ADD AX, BX ; AX = a + b
3B C1 CMP AX, CX ; is a + b > c?
76 0C JBE NOT_TRI ; if so, not triangle
03 C1 ADD AX, CX ; AX = a + b + c
D1 E2 SHL DX, 1 ; DX = 2a
3B C2 CMP AX, DX ; is a + b + c > 2a?
76 04 JBE NOT_TRI ; if so, not triangle
D1 E3 SHL BX, 1 ; BX = 2b
3B C3 CMP AX, BX ; is a + b + c > 2b?
NOT_TRI:
C3 RET ; return to caller
Callable function, input AX
, BX
and CX
. Result is in ZF
: NZ
if triangle, ZR
if not.
Uses Arnauld's method to check.
For some reason the test program returns "Hotdog"
if a triangle and "Not Hotdog"
if not.
Answered by 640KB on December 21, 2021
function(x)sum(x)>2*max(x)
If the sum of 3 numbers is greater than twice the greater of those numbers, then the triangular inequality holds.
Answered by Rui Barradas on December 21, 2021
REeEREeEREeEREEEEeEeEREEEeREEEEEEEREEEEEEEErREEEEEerEEEEEerEeEeErEEEEeeRErREErEEEEeREeReEreEE
Input: a;b;c
Output: -1
for falsy, 1
for truthy
Explanation:
REeEREeEREeE # Push 3 inputs (num) -> (R)
REEEEeEeE # Sort the (R) stack
REEEe # Move pointer pos to the last -> (R)
REEEEEEE # Move last item of (R) (num c in this case) to (r)
REEEEEEEE # Sum the stack (note: does not remove the stack completely!) -> (R)
# For some reason, the pointer pos of (R) keeps sticking to the last item
rREEEEEe # Subtract active item of (R) (a+b) to (r) (num c) -> (R)
# ((a+b)-c)
# r flag: Preserve the items
rEEEEEe # Same as above, but put it to (r)
# This switches the expression: (c-(a+b)) or -((a+b)-c)
# No more r flag this time, so remove these two items
rEeEeE # Absolute of (r)
rEEEEee # Divide active item of (R) (a+b) and (r) (num c) -> (r)
RE # Push 0 -> (R)
rREE # Create loop labelled 0 -> (R)
# r flag: Skip until Ee (or REe)
rEEEEe # Decrement (0 -> -1) -> (r)
REe # Return to where a goto was called last
ReE # If the division of (r) returned 0, go inside the loop
reEE # Output (r) as number
Answered by u-ndefined on December 21, 2021
=SUM(A:A)>MAX(A:A)*2
Input in A1
, A2
and A3
.
Alternatively, with input on A1
, B1
and C1
:
=SUM(1:1)>MAX(1:1)*2
Answered by Wernisch on December 21, 2021
Returns 1 for true and 0 for false
DECLARE @ table(x INT)
INSERT @ values(1),(1),(1)
SELECT-sum(x)/~max(2*x)FROM @
Answered by t-clausen.dk on December 21, 2021
Never done one of these before. Here are two algorithms borrowed from other answers implemented in JavaScript.
(a,b,c)=>a+b+c>Math.max(a,b,c)*2
(a,b,c)=>a=a+b>c&a+c>b&b+c>a
Answered by James Coyle on December 21, 2021
›Σθ⊗⌈θ
Try it online! Link is to verbose version of code. Takes input as an array and outputs a Charcoal boolean, i.e. -
for true, nothing for false. Uses @xnor's algorithm. Explanation:
θ Input array
Σ Summed
› Is greater than
θ Input array
⌈ Maximum
⊗ Doubled
Implicitly print
Answered by Neil on December 21, 2021
Answered by user92069 on December 21, 2021
-hr
, ÷⑭$->
Woot! Port of the 5-byte golfscript answer. TIO won't work because the version stored has a bug with the ⑭
command, while the most recent version doesn't. (For those interested, I believe TIO is ~20 commits behind the official repo).
This gets converted into the following python program:
from KegLib import *
from Stackd import Stack
stack = Stack()
printed = False
item_split(stack)
sort_stack(stack)
swap(stack)
maths(stack, '-')
comparative(stack, '>')
if not printed:
raw(stack)
÷
First, we take the sides as input. Keg doesn't do lists very well at the moment (I'm working on improving that though), so each number needs to be taken individually. The Forget I ever said that. One can actually take a list as input lol. So instead of taking three individual numbers, we go ahead and item split the implicit input list.¿
takes the input and evaluates it as a literal.
⑭$->
Then, like the golfscript answer, we sort the stack (⑭
), swap the top two items ($
) subtract those two items (-
) and then finally compare the two with >
.
Answered by lyxal on December 21, 2021
Answered by Arnauld on December 21, 2021
Array/vector
solution as shown in other answers.
#import<regex>
int f(std::vector<int>v){std::sort(&v[0],&*end(v));return v[2]<v[1]+v[0];}
int f(int a,int b,int c){a=a+b+c>2*((a=a>b?a:b)>c?a:c);}
Like the C solution but with more boilerplate.
-2 bytes thanks (indirectly) to Arnauld!
Answered by S.S. Anne on December 21, 2021
Answered by Citty on December 21, 2021
-2 bytes thanks to Arnauld: stores the result of max
in a
. Eliminates the variable m
used in the previous solution.
f(a,b,c){a=a+b+c>2*((a=a>b?a:b)>c?a:c);}
f(a,b,c){a=a+b+c>2*(a>b?a>c?a:c:b>c?b:c);}
Answered by S.S. Anne on December 21, 2021
$~->
Why bother with any of the other math? If the longest side is longer (or equal) than the sum of the shorter two, it can't be a triangle. Otherwise it can.
Input is an array. Output is 0 or 1. If you have the list necessarily sorted, then you can just input as a dumped array (straight onto the stack) and the first two characters are unneeded. If the list can be entered in reverse order, then you can remove the first three characters and flip the greater-than to a lesser-than, leaving only 2 characters needed.
$~-> #Check if side lengths could make a triangle
$ #Sort
~ #Dump array onto stack (stack is now 1 2 3)
#Swap the top two elements (1 3 2)
- #Subtract the top from the second (1 3-2)
> #Check if that difference is strictly greater than the smallest value
This is equivalent to my last answer, but instead of a+b>c, I did a>c-b, which saves a character of stack-shuffling.
Answered by Mathgeek on December 21, 2021
Saved 6 bytes thanks to ceilingcat!!!
f(a,b,c){a=a+b+c>2*fmax(a>b?a:b,c);}
Uses xnor's formula.
Answered by Noodle9 on December 21, 2021
I haven't written Wren in a long time...
Fn.new{|x|x.reduce{|a,b|a+b}>x.reduce{|a,b|a>b?a:b}*2}
Answered by user92069 on December 21, 2021
Answered by ZaMoC on December 21, 2021
Answered by Shaggy on December 21, 2021
d
, I'm quite satisfied because W doesn't have a max function. OR a function sorting the array.
▼╪m╜w♣S×∟╖
<a&b|R % Max.
2* % Double.
S % Swap.
+r % Sum.
< % Less than.
% It's in the wrong order because
% I can golf a few bytes off the max function.
% BTW the max function is implemented like this:
(b<a) && (a) || (b)
% The reduction reduces this function over the whole list.
Answered by user92069 on December 21, 2021
func[a][(sum sort a)>(2 * a/3)]
A port of @xnor's algorithm
Thanks to S.S. Anne for finding an unnecessary newline!
Answered by Galen Ivanov on December 21, 2021
A port of the Ruby answers.
~.{+}*$);2*>
~ # Evaluate the input
. # Copy it twice
{+}* # Sum the input
$ # Sort the other input
); # Select the last item
# in the sorted list
2* # Double this item
> # Compare
Answered by user92069 on December 21, 2021
(a,b,c)->a+b>c&a+c>b&b+c>a
Port of @xnor's formula turns out to be shorter after all, by taking the input as three loose integers instead of a List/array.
-12 bytes thanks to @xnor for reminding me that the first 6-bytes formula I used in my 05AB1E answer is actually shorter in Java:
$(a+b>c)land(a+c>b)land(b+c>a)$
Explanation:
(a,b,c)-> // Method with three integer parameters and boolean return-type
a+b>c // Return whether the sum of a and b is larger than c
&a+c>b // and the sum of a and c is larger than b
&b+c>a // and the sum of b and c is larger than a
Previous 52 49 bytes answer:
S->{S.sort(null);return S.pop()<S.pop()+S.pop();}
Port of @GB's Ruby answer with input as a Stack of Integers.
Explanation:
S->{ // Method with Integer-Stack parameter and boolean return-type
S.sort(null); // Sort the input-Stack
return S.pop() // Check if the last largest value
<S.pop()+S.pop();} // is smaller than the lowest two values added together
Answered by Kevin Cruijssen on December 21, 2021
d+
$*
O`1+
^(?!(1+),(1+),12)
Try it online! Link includes test cases. Explanation:
d+
$*
Convert to unary.
O`1+
Sort.
^(?!(1+),(1+),12)
Check that the sum of the first two is greater than the third.
Answered by Neil on December 21, 2021
fn($a)=>max($a)*2<array_sum($a);
Lambda function that takes an array [a, b, c]
and outputs empty string if false or "1" if true, with xnor's formula.
Note that in PHP the ;
is only necessary when the function is attributed to a variable, so it's placed in the footer (provided code is valid as it is).
EDIT: thanks to Guillermo Phillips for introducing me to PHP 7.4 short notation! (as a declaration without {}
, it now needs the ;
)
Answered by Kaddath on December 21, 2021
A routine taking $(a,b,c)$ into R0, R1 and R2 respectively and setting the carry if $(a,b,c)$ is not a triangle, or clearing it otherwise.
083 | MOVR R0, R3
0CB | ADDR R1, R3
15A | CMPR R3, R2
02F | ADCR R7
0D3 | ADDR R2, R3
049 | SLL R1
159 | CMPR R3, R1
201 002 | BC @@rtn
048 | SLL R0
158 | CMPR R3, R0
0AF | @@rtn JR R5
Instead of testing:
$$cases{a+b>c\a+c>b\b+c>a}$$
We test:
$$cases{a+b>c\a+b+c>2b\a+b+c>2a}$$
The left parts of the inequalities are stored into R3. If the first test fails (when $a+b$ is stored into R3 and compared with R2), the carry is set and added to the program counter (R7), forcing the next instruction to be skipped. Consequently, R3 is not updated to $a+b+c$ and the last 2 comparisons are turned into:
$$cases{a+b>2b\a+b>2a}Leftrightarrowcases{a>b\b>a}$$
which of course is never true, so the test is guaranteed to fail as expected.
All in all, this saves a branch which would have cost 1 extra DECLE.
ROMW 10 ; use 10-bit ROM width
ORG $4800 ; map this program at $4800
;; ------------------------------------------------------------- ;;
;; main code ;;
;; ------------------------------------------------------------- ;;
main PROC
SDBD ; set up an interrupt service routine
MVII #isr, R0 ; to do some minimal STIC initialization
MVO R0, $100
SWAP R0
MVO R0, $101
EIS ; enable interrupts
MVII #$200, R4 ; R4 = pointer into backtab
SDBD ; R5 = pointer into test cases
MVII #tc, R5
MVII #13, R3 ; R3 = number of test cases
@@loop MVI@ R5, R0 ; R0 = a
MVI@ R5, R1 ; R1 = b
MVI@ R5, R2 ; R2 = c
PSHR R3 ; save R3 and R5 on the stack
PSHR R5
CALL tr ; invoke our routine
PULR R5 ; restore R3 and R5
PULR R3
MVII #$80, R0 ; R0 = '0'
BC @@draw
MVII #$88, R0 ; or '1' if the carry is not set
@@draw MVO@ R0, R4 ; draw this character
DECR R3 ; next test case
BNEQ @@loop
DECR R7 ; loop forever
;; ------------------------------------------------------------- ;;
;; test cases ;;
;; ------------------------------------------------------------- ;;
tc PROC
DECLE 1, 1, 1 ; true
DECLE 1, 2, 3 ; false
DECLE 2, 1, 3 ; false
DECLE 1, 3, 2 ; false
DECLE 3, 2, 1 ; false
DECLE 3, 1, 2 ; false
DECLE 2, 2, 2 ; true
DECLE 3, 4, 5 ; true
DECLE 3, 5, 4 ; true
DECLE 5, 3, 4 ; true
DECLE 5, 4, 3 ; true
DECLE 10, 9, 3 ; true
DECLE 100, 10, 10 ; false
ENDP
;; ------------------------------------------------------------- ;;
;; ISR ;;
;; ------------------------------------------------------------- ;;
isr PROC
MVO R0, $0020 ; enable display
CLRR R0
MVO R0, $0030 ; no horizontal delay
MVO R0, $0031 ; no vertical delay
MVO R0, $0032 ; no border extension
MVII #$D, R0
MVO R0, $0028 ; light-blue background
MVO R0, $002C ; light-blue border
JR R5 ; return from ISR
ENDP
;; ------------------------------------------------------------- ;;
;; our routine ;;
;; ------------------------------------------------------------- ;;
tr PROC
MOVR R0, R3 ; R3 = a
ADDR R1, R3 ; R3 = a + b
CMPR R3, R2 ; is R3 greater than c?
ADCR R7 ; if not, skip the next instruction
ADDR R2, R3 ; R3 = a + b + c (or still a + b if skipped)
SLL R1 ; R1 = 2b
CMPR R3, R1 ; is R3 greater than 2b?
BC @@rtn ; if not, return with the carry set
SLL R0 ; R0 = 2a
CMPR R3, R0 ; is R3 greater than 2a?
; if not, the carry is set
@@rtn JR R5 ; return
ENDP
screenshot from jzIntv
1. A CP-1610 opcode is encoded with a 10-bit value (0x000 to 0x3FF), known as a 'DECLE'.
Answered by Arnauld on December 21, 2021
raJ++j>]2.*.>
ra # Read as array
J # Duplicate
++ # Sum
j # Swap
>] # Maximum
2.*# Double
.> # Greater than
Answered by DeathIncarnate on December 21, 2021
->*a{2*a.max<a.sum}
You can try it online!
Uses the fact that this Ruby answer said it didn't want to implement the port of xnor's answer and at the same time taught me enough syntax to guess how the max of an array is calculated :)
Answered by RGS on December 21, 2021
b=io.read()t={}for r in b.gmatch(b,"([^,]+)")do
table.insert(t,r) end
print(t[1]+t[2]+t[3]+0>math.max(t[1],t[2],t[3])*2)
@Jo King's solution using TIO properly and following the rules of the challenge.
a,b,c=...print(a+b+c>math.max(...)*2)
Answered by ouflak on December 21, 2021
ṀḤ<S
A monadic link taking a list of integers and returning a Jelly boolean (1
= True, 0
= False).
Based on @xnor's Python answer so be sure to upvote that one too!
Ṁ | Maximum
Ḥ | Doubled
< | Less than:
S | - Sum of original argument
Answered by Nick Kennedy on December 21, 2021
->*a{a.sort!.pop<a.sum}
A different approach, xnor's formula would be shorter but I'm satisfied with that.
Answered by G B on December 21, 2021
Answered by Galen Ivanov on December 21, 2021
Σ╙∞>
Exact port, including same explanation, as my 5-byte 05AB1E answer: sum; swap; max; double; a>b.
Answered by Kevin Cruijssen on December 21, 2021
-alp
-MList::Util=max,sum
, 18 bytes$_=sum(@F)>2*max@F
-alp
, 32 bytesOtherwise without imports
/ .* /;$_=!grep$_*2>=$`+$&+$',@F
Answered by Nahuel Fouilleul on December 21, 2021
+/>2×⌈/
Also uses xnor's formula of sum(a,b,c) > 2 * max(a,b,c)
.
+/>2×⌈/
+/ ⍝ Is sum
> ⍝ greater than
2× ⍝ twice of
⌈/ ⍝ max?
∧/+/>+⍨
∧/+/>+⍨
+/ ⍝ Is sum
> ⍝ greater than
+⍨ ⍝ twice each number?
∧/ ⍝ All of them?
Answered by Bubbler on December 21, 2021
lambda l:sum(l)>max(l)*2
Checks if a+b+c > max(a,b,c)*2
. If, say, c
is the biggest one, this is equivalent to a+b+c>2*c
, or a+b>c
, which is want we want for the triangle inequality.
Answered by xnor on December 21, 2021
O;‹ß
-1 byte by porting @xnor's algorithm.
-1 byte thanks to @Grimmy using a derived formula.
Try it online or verify all test cases.
Explanation:
The formula this program uses to determine if three side-lengths $a,b,c$ form a triangle is:
$t=frac{a+b+c}{2}$
$(a<t)land(b<t)land(c<t)$
O # Take the sum of the (implicit) input-list
; # Halve it
‹ # Check if it's larger than each of the values of the (implicit) input-list
ß # Get the minimum of those checks
# (`P` can be used as alternative to check if all are truthy instead)
# (after which this result is output implicitly)
Original 6-byter:
ÀĆü+‹P
Try it online or verify all test cases.
Explanation:
The formula this program uses to determine if three side-lengths $a,b,c$ form a triangle is:
$a+b>c$
$a+c>b$
$b+c>a$
Which translates to the following code for 05AB1E:
À # Rotate the (implicit) input-list once towards the right: [a,b,c] → [b,c,a]
Ć # Enclose it; appending its head to itself: [b,c,a] → [b,c,a,b]
ü+ # Sum each overlapping pair: [b,c,a,b] → [b+c,c+a,a+b]
‹ # Check for each whether it's larger than the (implicit) input-list:
# [a<b+c,b<c+a,c<a+b]
P # And check if all three are truthy by taking the product
# (`ß` could be used as alternative, like in the 4-byter program above)
# (after which the result is output implicitly)
Answered by Kevin Cruijssen on December 21, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP