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
Zsh, 25 bytes >2 <$@ …
9mo ago
Vyxal `s`, 5 bytes ``` 2=A …
9mo ago
Ruby, 20 bytes ->a,b{4[a …
4mo ago
[C (gcc)], 30 bytes …
8mo ago
Japt `-x`, 6 5 bytes Takes …
8mo ago
Lua, 43 bytes ``` lua func …
18d ago
JavaScript, 18 bytes ``` ja …
18d ago
C (gcc), 28 bytes ``` C f( …
18d ago
J, 7 bytes ```J ++4&=@+ ` …
4mo ago
Ruby, 19 bytes ``` ->a,b{ …
4mo ago
[Haskell], 13 bytes …
8mo ago
[Ruby], 23 bytes ```ruby - …
9mo ago
[Husk], 7 bytes +±Λ=2¹Σ …
9mo ago
[C (gcc)], 33 31 bytes …
9mo ago
[Python 3], 24 bytes …
9mo ago
JavaScript, 19 bytes …
9mo ago
[Java (JDK)], 51 50 23 21 byte …
7mo ago
17 answers
You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.
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.
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
C (gcc), 30 bytes
f(x,y){return x+y|!(x^y|x^2);}
In case the "preferably on a function" requirement can be dropped, then
#define f(x,y)x+y|!(x^y|x^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.
0 comment threads
JavaScript, 18 bytes
x=>y=>x*y==4?5:x+y
Math explanation here.
Credits to @Shaggy for x=>y=>
.
0 comment threads
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.
1 comment thread