Make $2 + 2 = 5$
In this challenge, add 2 integers, but if both the integers are 2
, output 5
. Shortest code in each language wins!
Example ungolfed program in Python 3.x
def add(x, y):
if x == 2 and y == 2:
return 5
else:
return x + y
[C (gcc)], 30 bytes …
1y ago
Zsh, 25 bytes >2 <$@ …
1y ago
Vyxal `s`, 5 bytes ``` 2=A …
1y ago
Ruby, 20 bytes >a,b{4[a …
1y ago
Japt `x`, 6 5 bytes Takes …
1y ago
Lua, 43 bytes ``` lua func …
11mo ago
JavaScript, 18 bytes ``` ja …
11mo ago
C (gcc), 28 bytes ``` C f( …
11mo ago
J, 7 9 bytes ```J ++2&=@+. …
10mo ago
Ruby, 19 bytes ``` >a,b{ …
1y ago
[Haskell], 13 bytes …
1y ago
[Ruby], 23 bytes ```ruby  …
1y ago
[Husk], 7 bytes +±Λ=2¹Σ …
1y ago
[C (gcc)], 33 31 bytes …
1y ago
[Python 3], 24 bytes …
1y ago
JavaScript, 19 bytes …
1y ago
J, 17 char ``` +/`5:@.(2./ …
10mo ago
[Java (JDK)], 51 50 23 21 byte …
1y ago
18 answers
Zsh, 25 bytes
>2
<$@&&<<<5<<<$[$1+$2]
Explanation:

>2;<$@
: if both arguments are2
:
&&<<<5
: then print 5 
<<<$[$1+$2]
: otherwise print$1 + $2

0 comment threads
Vyxal s
, 5 bytes
2=A[5
You're not the only one who can abuse flags, Shaggy...
2= # Foreach, is it equal to to?
A[ # If all are 2
5 # Push a 5.
# (s flag) sum of top of stack. If 5, then 5 is outputted, else a+b is outputted.
C (gcc), 30 bytes
f(x,y){return x+y!(x^yx^2);}
In case the "preferably on a function" requirement can be dropped, then
#define f(x,y)x+y!(x^yx^2)
is 28 bytes.
0 comment threads
C (gcc), 28 bytes
f(x,y){return x*y==4?5:x+y;}
This solution is based on the simple fact that: $$x \in \mathbb{N}, y \in \mathbb{N}$$ $$x=2, y=2 \Longrightarrow x \times y = 4 \text{ and } x + y = 5 \text{ (exception)}$$ $$x=1, y=4 \Longrightarrow x \times y = 4 \text{ and } x + y = 5 \text{ (deduction)}$$ $$x=4, y=1 \Longrightarrow x \times y = 4 \text{ and } x + y = 5 \text{ (deduction)}$$
but not on tricky binary operators.
1 comment thread
J, 7 9 bytes
++2&=@+.~
Dyadic fork that executes with the form (x + y) + (2 = x +. y)
.
Thanks to torres for pointing out the obvious flaw.
Java (JDK), 51 50 23 21 bytes
x>y>x==y&y==2?5:x+y
Golfed 27 bytes thanks to @Moshi's advice. Golfed 2 bytes thanks to @user's advice on Discord.
J, 17 char
+/`5:@.(2*./@:=])
How it works: What's within parentheses tests if both numbers on the right are 2. If so, the verb agenda @. selects the verb 5: which is always 5. If not, the verb agenda @. selects the verb +/ which sums the numbers.
+/`5:@.(2*./@:=]) 2 2
5
+/`5:@.(2*./@:=]) 2 3
5
+/`5:@.(2*./@:=]) 1 3
4
+/`5:@.(2*./@:=]) 0 4
4
+/`5:@.(2*./@:=]) 3 7
10
1 comment thread