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 …
11mo ago
Dyalog APL, 9 bytes ```apl …
1y ago
BQN (CBQN), 9 bytes ``` ++ …
2mo ago
UiuaSBCS `# Experimental!`, 8 …
3mo ago
[AWK], 28 26 21 bytes $ …
2mo ago
ESCR - not a golfing answer …
1y 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 …
1y ago
J, 17 char ``` +/`5:@.(2./ …
2y ago
[Java (JDK)], 51 50 23 21 byte …
3y ago
23 answers
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
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
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
Pyth, 6 bytes
+!-Q2s
I think there might be a way to shave a byte, but I can't find it.
s Sum the input
+ Add
-Q2 Remove the twos from the input
! Negate
0 comment threads
Dyalog APL, 9 bytes
+/⊢,2 2≡⊢
Takes the input as a pair
-
+/
sum reduce -
⊢
the input -
,
concatenated with -
2 2≡⊢
whether the input is equal to the list2 2
(1
if true,0
if false)
0 comment threads
x86-32 assembly, 14 bytes
Straightforward solution with custom calling convention: inputs in eax and ebx, output in eax. cmp
opcode could be smaller if limited to 8-bit integers?
00000000 <add225>:
0: 83 f8 02 cmp eax,0x2
3: 75 06 jne b <add>
5: 83 fb 02 cmp ebx,0x2
8: 75 01 jne b <add>
a: 40 inc eax
0000000b <add>:
b: 01 d8 add eax,ebx
d: c3 ret
0 comment threads
BQN (CBQN), 9 bytes
++2⊸=∧=⟜2
This utilizes the "3-train"- any functions in the form w fgh x
is turned into (w f x) g (w h x)
.
In this case, 2⊸=
and =⟜2
are functions that check if the right and left arguments are equal to 2
respectively, and
ed together with ∧
. This is 1
if so, 0
if not.
+
is the function to add numbers together.
+ + (2⊸= ∧ =⟜2 )
sum of the args added to (right = 2 and left = 2)
0 comment threads
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.
2 comment threads