Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs

Dashboard
Notifications
Mark all as read
Challenges

Make $2 + 2 = 5$

+2
−1

In this challenge, make a program that adds 2 integers, preferably on a function, and if the statement is 2 + 2, return or print 5. Shortest program 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

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?

1 comment thread

"Preferably" on a function? (2 comments)

12 answers

+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.

0 comment threads

+3
−0

Vyxal s, 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.

1 comment thread

Dang it I was just about to post that! (3 comments)
+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.

0 comment threads

+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.

0 comment threads

+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.

0 comment threads

+1
−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.

0 comment threads

+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 comment thread

[31 bytes](https://tio.run/##TYrRCoIwGEbv9xQfRrGfWUR0N@1Juonp6h81Yyko4rOvaQTefB@cc8z@bkyMVvb5QGOo2y54... (2 comments)
+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.

0 comment threads

+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.

0 comment threads

+0
−0

PHP, 73 72 68 67 bytes

<?php function f($x,$y){if($x==2&$y==2){echo 5;}else{echo$x+$y;}}?>

Try it online!

Golfed a byte thanks to @Quintec.

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

1 comment thread

-1 byte to compare $x to 2 instead (1 comment)
+0
−0

C (clang), 50 bytes

f(x,y){if(x==y&&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.

1 comment thread

This outputs `5` if _either_ of the inputs is `2`. (2 comments)
+0
−0

Lua, 77 69 66 bytes

function f(x,y)if x==y and y==2then print(5)else print(x+y)end end

Try it online!

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

0 comment threads

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!