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

Comments on Make $2 + 2 = 5$

Parent

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
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

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)
Post
+1
−0

ESCR - not a golfing answer

This isn't really an answer to the question, and it's not an attempt at golfing. However, it shows a cute trick for solving this problem that falls out of the way the ESCR language works.

ESCR has no "operators" in the sense of a traditional programming language, only functions. ESCR functions are enclosed in [ ]. The first token within the [ ] is the function name, which is then followed by whatever parameters are passed to the function. Functions result in a value, which can always be expressed in string format. The entire [...] function reference is replaced by its value.

Addition is done with the built-in function "+". For example, [+ 3 4] results in 7.

Another wrinkle in ESCR is that new versions of existing symbols can be defined. Each symbol is essentially a stack, with the most recently defined version on top of the stack.

New versions of functions can be defined, just like other symbols such as variables. When inside a routine (function, subroutine, or macro), the default version of that routine is the next-older than currently in. Nested routines of the same name therefore call each other down the stack. It is possible to reference a particular version, like the current to do recursion, but requires a deliberate syntax. The default makes it easy to "hook" existing routines.

Built-in functions aren't special in this regard. They are seeded into the function symbol table before user code is run, and are implemented with compiled code, but are otherwise indistinguishable from user-defined functions. New versions of these functions can be created, just like with other ESCR symbols.

The trick here is to define a new version of the "+" function that returns 5 when both arguments are 2, but the normal sum otherwise. (The actual ESCR "+" function can take any number of arguments, but we'll ignore that). Here is the code that creates a new "+", and demonstrates its use:

function +
  if [and [= [arg 1] 2] [= [arg 2] 2]]
    then
      funcval 5
    else
      funcval [+ [arg 1] [arg 2]]
    endif
  endfunc

show [+ 1 2]
show [+ 2 2]
show [+ 2 1]

The FUNCTION and ENDFUNC commands start and end the new function definition. Arguments to routines are accessed inside those routines with the ARG function. [arg 1] expands to the first argument, [arg 2] to the second, etc. The IF statement takes the THEN branch when both arguments to the function are 2. In that case, the FUNCVAL command sets the function value to 5 explicitly. Otherwise, the ELSE case is run. Here the function value becomes the sum of the two arguments.

Note the use of the "+" function in the ELSE clause to do the addition. The default version referenced inside a routine is the previous version, which in this case is the built-in "+" function.

The three SHOW commands write the result of "+" function uses to standard output. The output is:

3
5
3
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

OP's comment (1 comment)
OP's comment
General Sebast1an‭ wrote 11 months ago

Albeit not what I expected, this is actually interesting.