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
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Make $2 + 2 = 5$

+7
−3

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

Try it online!

History

2 comment threads

"integers" - should this work for negative integers? At least one answer I checked (but I suspect sev... (1 comment)
"Preferably" on a function? (2 comments)

23 answers

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

History

0 comment threads

+4
−0

Haskell, 13 bytes

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

Try it online!

History

0 comment threads

+4
−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.
History

1 comment thread

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

0 comment threads

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

0 comment threads

+3
−0

Ruby, 20 bytes

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

Attempt This Online!

History

0 comment threads

+3
−0

Python 3, 24 bytes

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

Try it online!

History

0 comment threads

+2
−0

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 list 2 2 (1 if true, 0 if false)
History

0 comment threads

+2
−0

Pyth, 6 bytes

+!-Q2s

Try it online!

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
History

0 comment threads

+1
−0

AWK, 28 26 21 bytes

$0=$1~2&&$2~2?5:$1+$2

Try it online!

History

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

History

1 comment thread

[31 bytes](https://tio.run/##TYrRCoIwGEbv9xQfRrGfWUR0N@1Juonp6h81Yyko4rOvaQTefB@cc8z@bkyMVvb5QGOo2y54... (2 comments)
+1
−0

Ruby, 23 bytes

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

Try it online!

History

0 comment threads

+1
−0

JavaScript, 19 bytes

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

Try it online!

History

0 comment threads

+1
−0

Ruby, 19 bytes

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

Try it online!

History

1 comment thread

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

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    
History

0 comment threads

+1
−0

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, anded 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)

Attempt This Online!

History

0 comment threads

+1
−0

Husk, 7 bytes

+±Λ=2¹Σ

Try it online!

Input as a list.

History

0 comment threads

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

History

1 comment thread

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

UiuaSBCS # Experimental!, 8 bytes

++↧∩=₂,,

Try it here!

Explanation

++↧∩=₂,,
      ,, Make a copy of the two arguments
   ∩=₂   Are they both equal to 2?
  ↧      Minimum
++       Add the three values
History

0 comment threads

+2
−1

C (gcc), 41 bytes

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

Attempt This Online!

History

2 comment threads

Incorrect answer (1 comment)
But the task asks for integers ($\mathbb Z$), not natural numbers ($\mathbb N$). Thus the code should... (1 comment)

Sign up to answer this question »