| Bytes | Lang | Time | Link |
|---|---|---|---|
| 027 | AWK | 250304T163142Z | xrs |
| 043 | Tcl | 180524T090446Z | sergiol |
| 015 | Vyxal | 220320T121954Z | ophact |
| 187 | Shakespeare Programming Language | 220320T073706Z | jimmy230 |
| 025 | C gcc | 180526T002322Z | Geo |
| 034 | QBasic | 180619T220604Z | DLosc |
| 035 | QBasic | 180619T194914Z | DLosc |
| 038 | QBasic 1.1 | 180619T132819Z | Erik the |
| 020 | Glee | 180530T201728Z | M L |
| 040 | Q'Nial7 | 180602T220512Z | M L |
| 027 | Prolog SWI | 180529T072127Z | ASCII-on |
| 208 | Shakespeare Programming Language | 180527T020239Z | Jo King |
| 004 | Husk | 180524T095549Z | Mr. Xcod |
| 040 | PHP | 180527T210021Z | Titus |
| 034 | Python 2 | 180527T172015Z | AvahW |
| 024 | JavaScript | 180528T061524Z | Lilith |
| 018 | Retina 0.8.2 | 180525T134439Z | ovs |
| 028 | Python 2 | 180524T083617Z | ElPedro |
| 027 | C# | 180524T091044Z | jjk_char |
| 052 | PHP | 180527T140200Z | Dave |
| 017 | Befunge93 | 180527T020643Z | Jo King |
| 030 | Python 3 | 180524T002611Z | hyperneu |
| 296 | Shakespeare Programming Language | 180525T142802Z | Guillaum |
| 020 | Befunge93 | 180526T040940Z | MercyBea |
| nan | 180525T214614Z | aaaaa sa | |
| 020 | Javascript ES6 | 180524T171528Z | MattH |
| 035 | R | 180525T191513Z | JayCe |
| 007 | Pyth | 180525T142255Z | Sok |
| 006 | 05AB1E | 180525T153520Z | Sok |
| 035 | Javascript | 180525T131601Z | david |
| 023 | GNU sed | 180524T164128Z | KernelPa |
| 008 | 05AB1E | 180524T074518Z | Kevin Cr |
| 032 | BrainFlak | 180524T211954Z | Nitrodon |
| 025 | Perl 5 p | 180524T175753Z | Xcali |
| 025 | Haskell | 180524T175023Z | lynn |
| 028 | dc | 180524T174709Z | brhfl |
| 034 | Elixir | 180524T162047Z | Okx |
| 009 | Japt | 180523T233017Z | Oliver |
| 036 | 12basic | 180524T123906Z | 12Me21 |
| 021 | Ruby | 180524T081057Z | G B |
| 017 | JavaScript Node.js | 180523T232116Z | l4m2 |
| nan | 180524T103201Z | Arnauld | |
| 009 | MATL | 180524T075757Z | Stewie G |
| 007 | 05AB1E | 180524T093107Z | Emigna |
| 007 | J | 180524T073147Z | Galen Iv |
| 010 | Brachylog | 180524T083801Z | Erik the |
| 020 | JavaScript Node.js | 180524T081524Z | Joost K |
| 030 | Batch | 180523T234500Z | Neil |
| 028 | Octave | 180524T074404Z | Stewie G |
| 008 | Stax | 180524T061636Z | recursiv |
| 027 | Haskell | 180524T055413Z | Angs |
| 010 | Pyth | 180524T045513Z | Mr. Xcod |
| 022 | C gcc | 180524T042752Z | Dennis |
| 026 | C gcc | 180524T040524Z | pizzapan |
| 020 | Java 8 | 180524T024724Z | Jakob |
| 025 | Python 3 | 180524T013452Z | Dennis |
| 118 | Swift | 180524T000700Z | onnoweb |
| 010 | TIBasic | 180524T024203Z | Timtech |
| 011 | Pyth | 180524T021230Z | user4854 |
| 004 | Jelly | 180524T005627Z | Dennis |
| 025 | brainfuck | 180524T001040Z | KSab |
| 006 | Jelly | 180523T233301Z | dylnan |
| 042 | Clean | 180523T234033Z | Οurous |
| 005 | APL Dyalog | 180523T233510Z | H.PWiz |
| 032 | Attache | 180523T233939Z | Conor O& |
Vyxal, 15 bytes
?0⁼?0⁼∨??⁼∨??∴*
Surprisingly no Vyxal answer yet. Return the max, multiplied by the boolean value indicating whether the values are equal or one of them is zero.
Vyxal, 6 bytes
s0pU3i
Thanks @lyxal for this version which ports the Pyth answer.
Shakespeare Programming Language, 187 bytes
,.Ajax,.Puck,.Act I:.Scene I:.[Enter Ajax and Puck]Ajax:Listen tothy!Puck:Listen tothy!Be
you nicer zero?If notyou be I.Am I worse a cat?If notam I as big as you?If notyou zero.Open heart
C (gcc), 25 bytes
f(a,b){a=a^b&&a*b?0:a|b;}
pseudo-code:
foo(A,B)
if A XOR B and A*B are > 0
return 0
else
return A OR B`
QBasic, 34 bytes
Different approach!
INPUT a,b
?(a OR b)*-(a*b=0OR a=b)
Observe that the nonzero values in the output grid are all the bitwise OR of the two input numbers. This is just a OR b in QBasic. We want to output this value when a*b=0 OR a=b, and 0 otherwise, which we can do by multiplying by the negative of the aforementioned conditional (negative, since truthy is -1 in QBasic).
QBasic, 38 36 35 bytes
INPUT a,b
?(a*b>0)*(b-a*(a<>b))+a+b
Partly inspired by Erik's IF ... THEN ... ELSE answer, here's a math-only solution.
How I got here
Important note for understanding math-with-conditionals: in QBasic, the results of comparison operators are 0 and -1, not 0 and 1.
We start with Erik's code:
IF a*b THEN?a*-(a=b)ELSE?a+b
In other words, if a and b are both nonzero, then print a*-(a=b) (a if a=b, otherwise 0); else (at least one of a and b is zero), output a+b (the nonzero number, or 0 if they're both zero).
There's already some math with conditionals going on here. Let's take it a step farther and see if we can eliminate the IF statement entirely. We'll have to use a*b>0 for the outer condition: a*b can have multiple different truthy values, which is fine for IF but causes problems for math.
c=a*b>0
?c*a*(a=b)+(c+1)*(a+b)
This is the standard trick of IF-elimination. When c is true, c*a*(a=b) is -a*(a=b) and (c+1)*(a+b) is 0; when c is false, c*a*(a=b) is 0 and (c+1)*(a+b) is a+b. So this expression gives the same results as the IF ... THEN ... ELSE. The only problem is, it makes our program 40 bytes instead of 38. Maybe we can shorten it by rearranging the math.
c=a*b>0
?c*a*(a=b)+c*(a+b)+a+b
Still 40 bytes...
c=a*b>0
?c*(a+b+a*(a=b))+a+b
Now our program is back to 38 bytes. But since we're only using c once, we don't have to assign it to a variable anymore:
?(a*b>0)*(a+b+a*(a=b))+a+b
Now we're down to 36 bytes.
But wait there's more... That a+b+a*(a=b) expression looks a bit redundant. a*(a=b) is -a if a=b and 0 otherwise. When we add it to a, we get 0 if a=b and a otherwise. Maybe we can achieve the same thing in fewer bytes by reversing the condition.
b+a*-(a<>b)
At first, this doesn't look shorter. But we can save a byte by subtracting instead of adding a negative:
b-a*(a<>b)
And there we have our 35-byte solution.
QBasic 1.1, 38 bytes
INPUT A,B
IF A*B THEN?A*-(A=B)ELSE?A+B
-1 thanks to DLosc.
Input is two comma-separated integers.
Glee, 24 22 20 bytes
Idea to append zero taken from Sok’s Pyth solution.
=>n,0& *>0\+ >1~ *n\>>
=>n,0& *>0\+ <2*n\>> $$<2 instead of >1~ (not > 1)
Explanation:
a b=>n,0& *>0\+ <2*n\>>
a b=>n 3 1 2 2 0 3 0 0 assign a b to n
,0 3 1 0 2 2 0 0 3 3 0 0 0 catenate 0
& 0 1 3 0 2 0 3 0 unique elements
*>0 011 01 01 0 mark all > 0 (boolean)
\+ 2 1 1 0 reduce by addition
<2 0 1 1 1 result <2 ?
*n 0 0 2 2 0 3 0 0 multiply with n
\>> 0 2 3 0 largest element
Old version
=>n--[2]=0|(n\* =0)*n\>>
Explanation for a pair of numbers (a b):
result for a b pairs
3 1 2 2 0 3
a b=>n--[2]=0|(n\* =0)*n\>>
a b 3 1 2 2 0 3 create vector (a b)
=>n assign vector to n
-- 0 _2 0 0 0 3 calculate the first difference vector of n (monadic --)
[2]=0 0 1 0 result vector at index 2 equal to 0 ? (boolean)
(n\* 3 4 0 n reduced by multiplication
=0) 0 0 1 equal to 0 ? (boolean)
| 0 1 1 OR (boolean) (dyadic |)
*n 0 0 2 2 0 3 multiplied by n (dyadic *)
\>> 0 2 3 largest element (monadic \>>)
Q'Nial7, 40 bytes
n is OP A B{2>sum(cull A B 0>0)*max A B}
n is OP A B{ } operation n with parameters A B (pair of numbers)
A B 0 append 0 to pair of numbers
cull create list of unique elements
>0 create booleans of elements that are > 0
sum( ) sum up the booleans
2> sum > 2 ? (boolean)
*max A B multiply boolean with max of A B
result:
n 0 3
3
n 1 2
0
n 2 2
2
n 0 0
0
Shakespeare Programming Language, 216 208 bytes
,.Ajax,.Ford,.Act I:.Scene I:.[Enter Ajax and Ford]Ajax:Listen tothy!Ford:Listen tothy!Am I as bad as you?If soyou zero.Is the product ofyou I worse a cat?If soyou be the sum ofyou I.If notyou zero.Open heart
As psuedo-code:
f(a,b):
if a==b:
b=0
if a*b<1:
b=a+b
else:
b=0
return b
PHP, 42 40 bytes
[,$a,$b]=$argv;echo$a*$b&&$a-$b?0:$a|$b;
Run with php -r '<code>' <a> <b> or Try it online.
breakdown
[,$a,$b]=$argv; // import input
echo
$a*$b // if both not 0
&&$a-$b // and unequal
?0 // then print 0
:$a|$b // else print the non-zero value, if there is one
;
Python 2, 42 34 bytes
lambda a,b:(0in(a,b)or a==b)*(a|b)
EDIT: -8 thanks to @TIO and @JonathanAllan
JavaScript, 24 Bytes
x=>y=>!x*y|!y*x|(x==y)*x
Allows all numbers, no conditional, doesn't require sorted input
C#, 4827 bytes
(x,y)=>x==y?x:(x*y>0?0:x|y)
Thanks @Charlie for the TIO link & helping trim 21 chars off the code
PHP - 52 bytes
<?=($a=$argv[1])==($b=$argv[2])?$a:($a*$b?0:$a|$b);
Explanation:
$a==$b Check for equality, display first value if equal
$a*$b If multiplication equates to non-zero, display 0 (Unknown) else...
$a|$b Display the non-zero value.
Befunge-93, 17 bytes
&::&-!_&*!\&+*.@.
As psuedo-code:
f(a,b):
if a-b==0:
print(b)
else:
print((!(a*b))*(a+b))
Python 3, 30 bytes
lambda a,b:0==a*b*(a-b)and a|b
-5 bytes thanks to Oliver
-1 byte thanks to Jo King
Shakespeare Programming Language, 296 bytes
Z.Ford,.Ajax,.Act I:.Scene I:.[Enter Ford and Ajax]Ajax:Listen to thy heart.Ford:Listen to thy heart.Am I as fat as you?Ajax:If so,let us Scene C.Am I as fat as zero?If so,let us Scene C.Ford:Am I as fat as zero?If not,you zero.open heart.let us Scene V.Scene C:.Ajax:open heart.Scene V:.[Exeunt]
First participation to a code-golf challenge, so let's start with one of my favorite joke languages !
Explanation : Declaration of the two variables Ford and Ajax (shortest variable names available)
Z.Ford,.Ajax,.
First Scene : Put the two values into the variable, then test them for equality, then test Ajax against 0. If the value that we have to return is stored in the variable Ford, go to Scene C.
Act I:.
Scene I:.
[Enter Ford and Ajax]
Ajax:
Listen to thy heart.
Ford:Listen to thy heart.
Am I as fat as you?
Ajax:
If so,let us Scene C.
Am I as fat as zero?
If so,let us Scene C.
If Ford is 0, print Ajax, else set Ajax as 0 then print Ajax. Then go to the end of the program.
Ford:Am I as fat as zero?
If not,you zero.
open heart.
let us Scene V.
Scene C : Print Ford
Scene C:.
Ajax:open heart.
Scene V: End of the program.
Scene V:.
[Exeunt]
Befunge-93, 20 bytes
&::&*#v_&+.@
@.*!-&<
Explanation
this boils down to:
if(A * B = 0) {
output (A + B)
end
} else {
output A * (A = B)
end
}
(first submission so please don't kick too hard)
Python 2, 57 44 43 bytes
lambda a,b:(0 if a*b else a+b)if a-b else a
(compressed a bit after looking at first python answer)
Javascript ES6, 25 22 21 20 bytes
a=>b=>a?b-a?!b*a:a:b
14 13 bytes, If the arguments are provided in sorted order
a=>b=>a%b?0:b
Pyth, 8 7 bytes
@{+0SQ3
@{+0SQ3 Implicit: Q=input()
SQ Sort input
+0 Prepend 0
{ Deduplicate
@ 3 Get 4th element (index 3), modular indexing
Case 1 - Both values nonzero and equal
Sorted Input [3,3]
Prepend 0 [0,3,3]
Deduplicate [0,3] - index 3 yields 3
Case 2 - Both values nonzero and unequal
Sorted Input [1,2]
Prepend 0 [0,1,2]
Deduplicate [0,1,2] - index 3 yields 0
Case 3 - Exactly one value zero
Sorted Input [0,1]
Prepend 0 [0,0,1]
Deduplicate [0,1] - index 3 yields 1
Case 4 - Both values zero
Sorted Input [0,0]
Prepend 0 [0,0,0]
Deduplicate [0] - index 3 yields 0
Alternate solution, also 7 bytes
*eSQ}s{
*eSQ}s{QQ Trailing Q's inferred
{Q Deduplicate input
s Take the sum
} Q Is this in the input? True treated as 1, false as 0
* Multiplied by
eSQ Max from input (end of sorted input)
Previous version, 8 bytes
@+0{-QZ3
Javascript, 35 bytes
f=(m,n)=>(m||n)&&(m!=n)?(m>n?m:n):0
05AB1E, 9 8 bytes
àIËIP_+*
-1 byte thanks to @MagicOctopusUrn.
Try it online or verify all test cases.
Explanation:
à # Take the maximum of the input-list
# [0,2] → 2
IË # Are all elements in the input-list equal?
# [0,2] → 0
IP_ # Take the product of the input-list, and verify if it equals 0
# (`_` transforms 0 into 1; everything else into 0)
# [0,2] → 0 (product) → 1 (==0)
+ # Add them together (since only 1 is truthy in 05AB1E, this is basically an OR)
# 0+1 → 1
* # Multiply both values on the stack
# 2*1 → 2
Generalized explanation:
IËIP_+ # If both values are equal, or one of them is a zero:
à # Output the maximum of the two values
# Else:
# Output 0
dc, 28 bytes
[pq]sqsidli=qd0rli*0!=qli++p
I'll keep mulling over this, it feels like it must be golfable. [pq]sq just makes a print & quit macro. sidli stores one of our values in the register i, duplicates the other value, and then recalls i. =q consumes two values on the stack, and executes macro q if they're equal (so, prints the one value left behind). d0r duplicates the value on the stack again, and then puts a 0 on the stack in between them. li*0!= loads i, multiplies the two values, and runs q to print the 0 that we left on the stack if the two values were not equal. If none of these things has happened yet, we li to load i, and we now have three values on the stack - two zeroes and our one non-zero value. ++ add the three together, and print.
Elixir, 34 bytes
Pretty simple in a functional programming language, just get the first match. No maths here!
fn a,a->a
a,0->a
0,a->a
_,_->0 end
12-basic, 36 bytes
FUNC M(A,B)?(A or B)*(!A~!B|A==B)END
This can probably be shortened.
~ is bitwise XOR (as well as bitwise NOT when used as a unary operator) (Just like in Lua)
Ruby, 21 bytes
->a,b{(a|b)*531[a*b]}
Because Ruby
Short explanation:
a|bis bitwse OR, so it gives us the right number if a==b or one of them is zero.Magic number
531is 2^9+2^4+2^1+2^0, and the [] operator extracts a single bit. This means: multiply by 1 if a*b is 0, 1, 2, 4 or 9, multiply by 0 otherwise.This won't work for values > 3
JavaScript (Node.js), 17 bytes, port somehow from Python answer
a=>b=>a*b&2?0:a|b
JavaScript (Node.js), 21 bytes
a=>b=>a-b?a*b?0:a+b:a
JavaScript (ES6)
27 bytes
A rather long formula, but without any conditional statement.
a=>b=>(a|4)*(b|4)*6%61%27&3
17 bytes
Simply a port of Dennis' method.
a=>b=>a*b&2?0:a|b
MATL, 9 bytes
dGp*~GX>*
Explanation:
% Implicit input as a vector with two elements implicitly. Stack: [0,2]
d % The difference between the two elements. Stack: [2]
G % Push input again. Stack: [2], [0,2]
p % The product of the last element (the input). Stack: [2], [0]
* % Multiply the two elements on the stack. Stack: [0]
~ % Negate. Stack: [1]
G % Push input again. Stack: [1], [0,2]
X> % Maximum value. Stack: [1], [2]
* % Multiply the two elements on the stack. Stack: [2]
% Implicit output
05AB1E, 7 bytes
`α*P_*Z
Explanation
`α # absolute difference of the inputs
* # multiplied by the inputs
P # product of the result
_ # logically negated
* # multiplied by the inputs
Z # max
J, 8 7 bytes
1 byte saved by H.PWiz.
>.*=^<.
A J port of H.PWiz's APL solution
= are the numbers equal? (results in 1 or 0)
^ to the power of
<. the smaller number
* multiplied by
>. the larger number
Batch, 38 36 35 30 bytes
@cmd/cset/a"(%1|%2)>>(%1*%2&2)
Port of @Dennis's Python answer, as conditionals are too expensive in Batch.
Octave, 28 bytes
@(a,b)~(a*b*(a~=b))*max(a,b)
or
@(a,b)(a==b|~(a*b))*max(a,b)
Explanation:
The maximum value should be outputted if a*b=0 or if a==b. The function takes a and b as inputs. It multiplies a*b, thus getting a zero if one of them is zero, and checks that a~=b, thus getting a zero if the two are equal. We negate both, so that both conditions gives us a truthy value telling us that we should output the maximum value. We then multiply this boolean (0/1) by the maximum value, and outputs it.
Stax, 8 bytes
Ç∞∟∙◄╥*♣
Unpacked, ungolfed, and commented, it looks like this.
e.g. [2, 0]
c:s [2, 0] 2 calculate the "span" of the input array (max(a) - min(a))
+ [2, 0, 2] append the span to the input array
o [0, 2, 2] sort the 3-element array
E 0 2 2 explode the 3 elements into 3 separate stack entries
a 2 2 0 rotate the third stack element to the top of stack
! 2 2 1 logical not, produces 1 iff the top value was 0
* 2 2 multiply
implicitly print top of stack
C (gcc), 26 bytes
f(a,b){a=a*b?a-b?0:a:a+b;}
Expanation / Ungolfed:
int f(int a, int b) { // implicit-int (C89)
// return replaced with assignment: link
return a*b ? // if a and b are both not zero, then
a-b ? // if a != b
0 : // a != b, so return 0
a // a == b, so return a
: a+b // one of a,b is zero, so return whichever is nonzero
;
}
Swift, 118 bytes
func c(n1:Int,n2:Int){n1==n2 ? print("\(n1)") : (n1*n2 != 0 ? print("0") : (n1==0 ? print("\(n2)") : print("\(n1)")))}
TI-Basic, 10 bytes
max(Ans)not(prod(Ans)ΔList(Ans
Multiply maximum value by result of (some element equals zero OR elements are equal)
Pyth, 11 bytes
e+Z.-+xFQQ{
Explanation
e+Z.-+xFQQ{
+xFQQ Prepend the XOR of the inputs to the inputs.
.- {Q Remove one copy of the deduplicated (implicit) input.
+Z Prepend 0.
e Take the last.
Alternative solution, 11 bytes
e+0.-F{B+xF
Jelly, 4 bytes
gf|S
How it works
gf|S Main link. Left argument: x. Right argument: y.
g Compute a, the gcd of x and y.
| Compute b, the bitwise OR of x and y.
f Filter; yield all common elements of [a] and [b].
S Take the sum.
brainfuck, 25 bytes
,>,[>]<<[[->->+<<]>[>]]>.
Input is two byte values (not ascii)
Jelly, 7 6 bytes
׬o=a»
Try it online! or Try all combinations!
How?
׬o=a» Dyadic link
× Multiply the two arguments.
¬ Logical not. Gives 1 if one argument is 0, 1 otherwise.
= Are the two arguments equal?
o Logical or the result of = and ¬.
» Greater of the two arguments.
a Logical and. Gives the greater of the two arguments if they are equal
or if one of them is zero and gives 0 otherwise.
Using the method in the APL answer, we get the same byte count. One byte longer than that answer because lowest common multiple is two bytes.
6 bytes
«=æl×»
Clean, 46 43 42 bytes
import StdEnv
?[a,b]|a<1||a==b=b=0
?o sort
Anonymous composition :: [Int] -> Int, sorts the pair and then matches off the first member.
Doing it as a composed lambda is the same length:
import StdEnv
(\[a,b]|a<1||a==b=b=0)o sort
APL (Dyalog), 5 bytes
⌈×∧=⌊
Useful reference
∧=⌊: Returns 1 if the lowest common multiple is equal to the minimum. This is only true if one of the values is zero, or both are equal. Alternatively I could have =*⌊
⌈×: The maximum multiplied by the above.
Attache, 32 bytes
{If[_=_2,_,If[Min!__,0,Max!__]]}
Takes input as two arguments, such as f[0, 1] = 1.
Other solutions
34 bytes
{If[Same!_,_@0,If[Min!_,0,Max!_]]}
Try it online! The same as above, but takes input as an array
39 bytes
{ToBase[47483073034,5][FromBase&4!_]-1}
Hardcodes the solution in a base-5 integer. More interesting IMO but longer.