g | x | w | all
Bytes Lang Time Link
027AWK250304T163142Zxrs
043Tcl180524T090446Zsergiol
015Vyxal220320T121954Zophact
187Shakespeare Programming Language220320T073706Zjimmy230
025C gcc180526T002322ZGeo
034QBasic180619T220604ZDLosc
035QBasic180619T194914ZDLosc
038QBasic 1.1180619T132819ZErik the
020Glee180530T201728ZM L
040Q'Nial7180602T220512ZM L
027Prolog SWI180529T072127ZASCII-on
208Shakespeare Programming Language180527T020239ZJo King
004Husk180524T095549ZMr. Xcod
040PHP180527T210021ZTitus
034Python 2180527T172015ZAvahW
024JavaScript180528T061524ZLilith
018Retina 0.8.2180525T134439Zovs
028Python 2180524T083617ZElPedro
027C#180524T091044Zjjk_char
052PHP180527T140200ZDave
017Befunge93180527T020643ZJo King
030Python 3180524T002611Zhyperneu
296Shakespeare Programming Language180525T142802ZGuillaum
020Befunge93180526T040940ZMercyBea
nan180525T214614Zaaaaa sa
020Javascript ES6180524T171528ZMattH
035R180525T191513ZJayCe
007Pyth180525T142255ZSok
00605AB1E180525T153520ZSok
035Javascript180525T131601Zdavid
023GNU sed180524T164128ZKernelPa
00805AB1E180524T074518ZKevin Cr
032BrainFlak180524T211954ZNitrodon
025Perl 5 p180524T175753ZXcali
025Haskell180524T175023Zlynn
028dc180524T174709Zbrhfl
034Elixir180524T162047ZOkx
009Japt180523T233017ZOliver
03612basic180524T123906Z12Me21
021Ruby180524T081057ZG B
017JavaScript Node.js180523T232116Zl4m2
nan180524T103201ZArnauld
009MATL180524T075757ZStewie G
00705AB1E180524T093107ZEmigna
007J180524T073147ZGalen Iv
010Brachylog180524T083801ZErik the
020JavaScript Node.js180524T081524ZJoost K
030Batch180523T234500ZNeil
028Octave180524T074404ZStewie G
008Stax180524T061636Zrecursiv
027Haskell180524T055413ZAngs
010Pyth180524T045513ZMr. Xcod
022C gcc180524T042752ZDennis
026C gcc180524T040524Zpizzapan
020Java 8180524T024724ZJakob
025Python 3180524T013452ZDennis
118Swift180524T000700Zonnoweb
010TIBasic180524T024203ZTimtech
011Pyth180524T021230Zuser4854
004Jelly180524T005627ZDennis
025brainfuck180524T001040ZKSab
006Jelly180523T233301Zdylnan
042Clean180523T234033ZΟurous
005APL Dyalog180523T233510ZH.PWiz
032Attache180523T233939ZConor O&

AWK, 27 bytes

1,$0=$1~$2?$1:$1*$2?0:$1+$2

Attempt This Online!

Tcl, 43 bytes

proc M a\ b {expr !$a?$b:!$b?$a:$a-$b?0:$a}

Try it online!

Vyxal, 15 bytes

?0⁼?0⁼∨??⁼∨??∴*

Try it Online!

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

Try it Online!

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

Try it online!

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

Prolog (SWI), 27 bytes

N+N+N.
N+0+N.
0+N+N.
_+_+0.

Try it online!

The most naive translation ever.

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

Try it online!

As psuedo-code:

f(a,b):
  if a==b:
    b=0
  if a*b<1: 
    b=a+b
  else:
    b=0
  return b

Husk, 4 bytes

-1 thanks to H.PWiz.

!4uΘ

Try it online!

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

Retina 0.8.2, 20 18 bytes

-2 bytes thanks to Martin Ender.

(.)\1|0
$1
..|^$
0

Try it online!

Python 2, 34 28 bytes

lambda i,j:(i|j,0)[0<i!=j>0]

Try it online!

-6 with thanks to @Dennis

C#, 4827 bytes

(x,y)=>x==y?x:(x*y>0?0:x|y)

Try it online

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);

Try it online!

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

&::&-!_&*!\&+*.@.

Try it online!

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

Try it online!

-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]

Try it online!

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_&+.@
@.*!-&<

Try it online!

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

Try it online!

(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

R, 35 bytes

rep(unique(sort(c(0,scan()))),4)[4]

Try it online!

Shameless port of Sok's well-explained Pyth answer.

Pyth, 8 7 bytes

@{+0SQ3

Try it online!

@{+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{

Try it online

*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

05AB1E, 6 bytes

ê0ýÙ7è

Try it online!

Port(ish) of my Pyth answer.

TIO header/footer taken from Emigna's answer

Javascript, 35 bytes

f=(m,n)=>(m||n)&&(m!=n)?(m>n?m:n):0

GNU sed, 23 bytes

s/^0?(.)\1?0?$/\1/
t
c0

(must be run with -r flag)

Try it online!

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

Brain-Flak, 32 bytes

{(({}[({})]<>)){{}<>}(<>)}{}({})

Try it online!

Perl 5 -p, 25 bytes

/ /;$_=($'|$`)>>($'*$`&2)

Try it online!

Steals the method from @Dennis's Python answer.

Haskell, 25 bytes

x!y=0^((x-y)^2*x)*y+0^y*x

Try it online!

dc, 28 bytes

[pq]sqsidli=qd0rli*0!=qli++p

Try it online!

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

Try it online!

Japt, 12 9 bytes

Takes input as an array of integers.

×&2?0:Ur|

Try it online!

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]}

Try it online!

Because Ruby

Short explanation:

JavaScript (Node.js), 17 bytes, port somehow from Python answer

a=>b=>a*b&2?0:a|b

Try it online!

JavaScript (Node.js), 21 bytes

a=>b=>a-b?a*b?0:a+b:a

Try it online!

JavaScript (ES6)

27 bytes

A rather long formula, but without any conditional statement.

a=>b=>(a|4)*(b|4)*6%61%27&3

Try it online!

17 bytes

Simply a port of Dennis' method.

a=>b=>a*b&2?0:a|b

Try it online!

MATL, 9 bytes

dGp*~GX>*

Try it online!

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

Try it online!

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.

>.*=^<.

Try it online!

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

Brachylog, 10 bytes

=h|∋0&⌉|∧0

Try it online!

JavaScript (Node.js), 20 bytes

a=>b=>a-b&&a*b?0:a|b

Try it online!

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)

Try it online!

or

@(a,b)(a==b|~(a*b))*max(a,b)

Try it online!


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

Ç∞∟∙◄╥*♣

Run and debug it

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

Run this one

Haskell, 27 bytes

a#b=max a b*0^(a*b*(a-b)^2)

Try it online!

Pyth, 10 bytes

-iFQ&.AQnF

Try it online!

C (gcc), 22 bytes

f(x,y){x=x*y&2?0:x|y;}

Try it online!

C (gcc), 26 bytes

f(a,b){a=a*b?a-b?0:a:a+b;}

Try it online!

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 
    ;
}

Java 8, 20 bytes

Curried lambda. Stolen from here.

a->b->(a|b)>>(a*b&2)

Python 3, 27 25 bytes

lambda x,y:(x|y)>>(x*y&2)

Try it online!

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{

Try it here

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

Try it online!

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×»

Try it online!

Clean, 46 43 42 bytes

import StdEnv
?[a,b]|a<1||a==b=b=0

?o sort

Try it online!

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

⌈×∧=⌊

Try it online!

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!__]]}

Try it online!

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}

Try it online!

Hardcodes the solution in a base-5 integer. More interesting IMO but longer.