Communities

tag:snake search within a tag
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
created:<1w created < 1 week ago
post_type:xxxx type of post
Challenges

Make $2 + 2 = 5$

+4
−2

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

if x == 2 and y == 2:
return 5
else:
return x + y

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

"Preferably" on a function? (2 comments)

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.

+2
−0

Ruby, 20 bytes

->a,b{4[a]*4[b]+a+b}

Attempt This Online!

Why does this post require moderator attention?
You might want to add some details to your flag.

+3
−0

Zsh, 25 bytes

>2
<$@&&<<<5||<<<$[$1+$2]

Attempt This Online!

Explanation:

• >2;<$@: if both arguments are 2: • &&<<<5: then print 5 • ||<<<$[$1+$2]: otherwise print $1 +$2
Why does this post require moderator attention?
You might want to add some details to your flag.

+3
−0

Vyxals, 5 bytes

2=A[5

Try it Online!

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.
Why does this post require moderator attention?
You might want to add some details to your flag.

Dang it I was just about to post that! (3 comments)
+3
−0

C (gcc), 30 bytes

f(x,y){return x+y|!(x^y|x^2);}

Try it online!

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.

Why does this post require moderator attention?
You might want to add some details to your flag.

+2
−0

Japt-x, 6 5 bytes

Takes input as an array of integers

p!UdÍ

Try it

p!UdÍ     :Implicit input of array U
p         :Push
!        :  Logical NOT of
Ud      :  Any true (not zero) when
Í     :    Subtracted from 2
:Implicit output of sum of resulting array
Why does this post require moderator attention?
You might want to add some details to your flag.

+1
−0

C (gcc), 33 31 bytes

f(x,y){return x+y+(x==2&y==2);}

Try it online!

Saved two bytes thanks to Shaggy

Why does this post require moderator attention?
You might want to add some details to your flag.

+1
−0

JavaScript, 18 bytes

x=>y=>x*y==4?5:x+y

Attempt This Online!

Math explanation here.

Credits to @Shaggy for x=>y=>.

Why does this post require moderator attention?
You might want to add some details to your flag.

+1
−0

Lua, 43 bytes

function(x,y)return x*y==4 and 5 or x+y end

Attempt This Online!

Why does this post require moderator attention?
You might want to add some details to your flag.

+1
−0

C (gcc), 28 bytes

f(x,y){return x*y==4?5:x+y;}

Attempt This Online!

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.

Why does this post require moderator attention?
You might want to add some details to your flag.

+1
−0

Python 3, 24 bytes

lambda x,y:x+y+(x==y==2)

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

+1
−0

JavaScript, 19 bytes

x=>y=>x-2|y-2?x+y:5

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

+1
−0

Ruby, 23 bytes

->a,b{a==2&&b==2?5:a+b}

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

+1
−0

Ruby, 19 bytes

->a,b{a|b==2?5:a+b}

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

Wrong answer for 0 and 2. (1 comment)
+1
−0

Husk, 7 bytes

+±Λ=2¹Σ

Try it online!

Input as a list.

Why does this post require moderator attention?
You might want to add some details to your flag.

+1
−0

2!2=5
x!y=x+y

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

+1
−0

J, 7 9 bytes

++2&=@+.~

Try it online!

Dyadic fork that executes with the form (x + y) + (2 = x +. y).

Thanks to torres for pointing out the obvious flaw.

Why does this post require moderator attention?
You might want to add some details to your flag.

 3 (++4&=@+) 1 5  (1 comment)
+0
−0

Java (JDK), 515023 21 bytes

x->y->x==y&y==2?5:x+y

Try it online!

Golfed 27 bytes thanks to @Moshi's advice. Golfed 2 bytes thanks to @user's advice on Discord.

Why does this post require moderator attention?
You might want to add some details to your flag.

18-byte version (1 comment)
Lambda (1 comment)
+0
−0

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
Why does this post require moderator attention?
You might want to add some details to your flag.