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 …
3y ago
[Haskell], 13 bytes …
3y ago
Vyxal `s`, 5 bytes ``` 2=A …
3y ago
Ruby, 20 bytes ->a,b{4[a …
3y ago
[Python 3], 24 bytes …
3y ago
Zsh, 25 bytes >2 <$@ …
3y ago
Japt `-x`, 6 5 bytes Takes …
3y ago
[Pyth], 6 bytes +!-Q2s …
9mo ago
Dyalog APL, 9 bytes ```apl …
1y ago
UiuaSBCS `# Experimental!`, 8 …
1mo ago
[AWK], 28 26 21 bytes $ …
14d ago
ESCR - not a golfing answer …
11mo ago
x86-32 assembly, 14 bytes S …
1y ago
J, 7 9 bytes ```J ++2&=@+. …
2y ago
Ruby, 19 bytes ``` ->a,b{ …
3y ago
[Ruby], 23 bytes ```ruby - …
3y ago
[Husk], 7 bytes +±Λ=2¹Σ …
3y ago
[C (gcc)], 33 31 bytes …
3y ago
JavaScript, 19 bytes …
3y ago
C (gcc), 41 bytes ```c f(x …
12mo ago
BQN (CBQN), 9 bytes ``` ++ …
9d ago
J, 17 char ``` +/`5:@.(2./ …
2y ago
[Java (JDK)], 51 50 23 21 byte …
3y ago
23 answers
C (gcc), 41 bytes
f(x,y){int r=x*y;return r&&r==x+y?5:x+y;}
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
0 comment threads
- ← Previous
- 1
- 2
- Next →
2 comment threads