393
votes
\$\begingroup\$

Write a program that seemingly adds the numbers 2 and 2 and outputs 5. This is an underhanded contest.

Your program cannot output any errors. Watch out for memory holes! Input is optional.

Redefining 2+2 as 5 is not very creative! Don't doublethink it, try something else.

\$\endgroup\$

closed as off-topic by Dennis Mar 23 '16 at 4:50

  • This question does not appear to be about programming puzzles or code golf within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

locked by Dennis Mar 23 '16 at 4:50

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. See the help center for guidance on writing a good question.

Read more about locked posts here.

  • 48
    \$\begingroup\$ This is question in book "1984" by George Orwelll. \$\endgroup\$ – bacchusbeale May 30 '14 at 12:23
  • 97
    \$\begingroup\$ Why is this underhanded? Two plus two is five. \$\endgroup\$ – Geobits May 30 '14 at 16:23
  • 79
    \$\begingroup\$ THERE! ARE! FOUR! LIGHTS! \$\endgroup\$ – Nick T May 30 '14 at 19:57
  • 105
    \$\begingroup\$ @Geobits - I agree: 2+2=5 you can see it in this calculator. \$\endgroup\$ – MT0 May 30 '14 at 20:03
  • 47
    \$\begingroup\$ echo "2+2=5"; \$\endgroup\$ – user3459110 May 31 '14 at 12:16

156 Answers 156

-1
votes
\$\begingroup\$

PHP

Here are 2 methods I found working

Method 1

echo (2+2 === '4' ? 4 : 5);

Method 2

$a = $b = 2;
echo ($a + ++$b);
\$\endgroup\$
-1
votes
\$\begingroup\$

Python 2.7 a emotional answer to the problem

# Code and error numbers for all errors that can occur.
error_number = 53
error_descrption = "Error occured"

try:
    bird;
    """

    This is the lead motive of our application.
    This was made by jrei (http://www.chris.com/ascii/index.php?art=animals/birds%20(land))


                               _,---._      __,...-----...___
                           _,-:::,,--.`,--''                 `'--._
                         ,':::::/((##)):                           `-.
                       ,':.::::/  `--' :         _____.....______ (:::\
                      /:::::::/        :__,.-''''..- - - - --  -- .`_-:\
                     /:,:::.::|        ::.          ____....-----.....`.
                    /,:::::::/          ::::__.--'''
                    |:::::::|           _:'
                    |:.:::::|         ,'
                    |:::::::|         |
                   /::::.:::|         |
              __,-'::.::::::|         |
       _,.--''::::_::::::::::\        |
     ''::_::,:--''  `'--.:::::\       ;
     -'''::::::::::::::::\:::::`.     ;
     :::::::::::::::::::::|:.::::`-..-
     ::;::::::::::::::::::|::::::::::/
     :/::::::/::::::;:::::|::::.::::/
     (::::::/::::::/::::::):.::::::'
     :`:__,;::::::;:::::,':::::::'
     ,-':::`.__,-'::::,'::::::-'
     :::::,-'::::::,-':::_:-'
     _,-':::::::,-'::_:-'
     jrei::::,-_:--''
      ::,::--''



    I think it resembles our application the best.
    The strength and the speed of the bird are the same characteristics as our
    application. 


    So dear reader enough talk, now comes the code which makes the application so
    incredible! Buckle up your seatbelts! It will be a tough ride.




    """



    print "{}".format(2+2)



    """
    Oh dear reader i forgot to show you, how this program actually is structured.
    I think the best way is UML, because everybody loves it.

    +--------+       +---------------------+ *
    | Client |------>| Component           |<-------------------------+
    +--------+       +---------------------+                          |
                     | Operation()         |                          |
                     | Add(Component)      |                          |
                     | Remove(Component)   |                          |
                     | GetChild(int)       |                          |
                     +---------------------+                          |
                               #                                      |
                               |                                      |
                    +----------+--------------+                       |
                    |                         |                       |
                +------+------+   +----------+----------+  children   |
                | Leaf        |   | Composite           |O------------+
                +-------------+   +---------------------+
                | Operation() |   | Operation()         |
                +-------------+   | Add(Component)      |
                                  | Remove(Component)   |
                                  | GetChild(int)       |
                                  +---------------------+



    I especially want to say thank you to all people, which where part of this project.
    It took me several weeks to code this stuff and went through a hard time.
    My wife broke up with me and my dog died. I am a very emotional person,
    and this hit me really hard. But it is very nice, that i have somebody like you,
    someon i can talk to. I think this code is the fastest implementation on out there.
    I am very proud of it and it gives me the strength to go on with my life.
    Although i miss my wife, this is the past. I need to find a way to look into the future.
    I am a little bit overweight, but i do workouts to get fit.
    I try to avoid hotdogs, but they are just so jummy. But i will find the inner
    strength to avoid them.


    Thank you very much.
    Sincerely (insert name here)




    """
# Just some basic handling error in the case something goes wrong
# My Code is perfectly fine (insert link to xkcd comic here), but you never know
except NameError:
    try:
        print chr(error_number), error_description
    except NameError:
        pass
\$\endgroup\$
  • 1
    \$\begingroup\$ Took me 30 seconds to see the error. \$\endgroup\$ – kirbyfan64sos Nov 6 '15 at 15:47
-2
votes
\$\begingroup\$

Because we love to trust in the docstring and our debug decorators.

from functools import wraps

def debug(func):
    @wraps(func)
    def wrapper(*args):
        print "*DEBUG"
        print "**Calling help on %s" % func.__name__
        help(func)
        print "** Executing %s(a=%s,b=%s)" % (func.__name__,args[0],args[1])
        print "**%s + %s = " % (args[0], args[1]),
        return func(args[0] + 1, args[1])

    return wrapper

@debug
def Add(a=2, b=2):
    """
    :param a: default 2
    :type a: int
    :param b: default 2
    :type b: int
    :returns: a+b
    :rtype: int
    """
    return a + b

print Add(2, 2)
\$\endgroup\$
  • 1
    \$\begingroup\$ This is not hidden at all: return func(args[0] + 1, args[1]). \$\endgroup\$ – Kyle Kanos Jul 17 '14 at 14:56
  • \$\begingroup\$ It's a practical joke. Because we usually trust in the docstring and the debugger. If something like this happens deep down a big program, you don't have a simple way to track it down because the @wraps changes the 'wrapped' to the add(). \$\endgroup\$ – f.rodrigues Jul 17 '14 at 15:27
-2
votes
\$\begingroup\$

C#

Console.WriteLine(" 2+2=".Length);
\$\endgroup\$
  • 9
    \$\begingroup\$ That doesn't really look like it should print 4, does it? ;) \$\endgroup\$ – Martin Ender Dec 31 '14 at 10:17
-3
votes
\$\begingroup\$

Mathcad

Simply write it down using the boolean equality operator or define an another addition function using the + character ...

enter image description here

\$\endgroup\$
-4
votes
\$\begingroup\$

C#

Didn't see anyone write this I think...

    static void Main(string[] args)
    {
        int two = 2;

        int answer = two + two;

        if (answer == 4)
        {
            answer++;
        }

        Console.WriteLine(two + " + " + two + " = " + answer);
        Console.ReadLine();
    }
\$\endgroup\$
  • 6
    \$\begingroup\$ This question is an underhanded contest. Answers should appear that they will output 4, but in reality output 5. Your submission does not seem to be trying to hide the fact that it will output 5, so it may not be considered a very good answer for this question. Anyway, welcome to PPCG! :) \$\endgroup\$ – ace May 31 '14 at 21:49
  • \$\begingroup\$ Sorry everybody else took the good ones. :( \$\endgroup\$ – kenevil1 Jun 1 '14 at 9:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.