90
votes
\$\begingroup\$

The challenge: Define x in such a way that the expression (x == x+2) would evaluate to true.

I tagged the question with C, but answers in other languages are welcome, as long as they're creative or highlight an interesting aspect of the language.

I intend to accept a C solution, but other languages can get my vote.

  1. Correct - works on standard-compliant implementations. Exception - assuming an implementation of the basic types, if it's a common implementation (e.g. assuming int is 32bit 2's complement) is OK.
  2. Simple - should be small, use basic language features.
  3. Interesting - it's subjective, I admit. I have some examples for what I consider interesting, but I don't want to give hints. Update: Avoiding the preprocessor is interesting.
  4. Quick - The first good answer will be accepted.

After getting 60 answers (I never expected such prticipation), It may be good to summarize them.

The 60 answers divide into 7 groups, 3 of which can be implemented in C, the rest in other languages:

  1. The C preprocessor. #define x 2|0 was suggested, but there are many other possibilities.
  2. Floating point. Large numbers, infinity or NaN all work.
  3. Pointer arithmetic. A pointer to a huge struct causes adding 2 to wrap around.

    The rest don't work with C:

  4. Operator overloading - A + that doesn't add or a == that always returns true.
  5. Making x a function call (some languages allow it without the x() syntax). Then it can return something else each time.
  6. A one-bit data type. Then x == x+2 (mod 2).
  7. Changing 2 - some language let you assign 0 to it.
\$\endgroup\$

closed as off-topic by cat, DJMcMayhem, mbomb007, Erik the Outgolfer, user902383 Nov 30 '16 at 15:09

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions without an objective primary winning criterion are off-topic, as they make it impossible to indisputably decide which entry should win." – cat, DJMcMayhem, mbomb007, Erik the Outgolfer, user902383
If this question can be reworded to fit the rules in the help center, please edit the question.

locked by Dennis Oct 28 '17 at 1: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.

  • 29
    \$\begingroup\$ Why 4. Quick? You mean "Whoever knows one and is lucky enough to read this question first"? \$\endgroup\$ – Luc Sep 7 '12 at 21:43
  • 6
    \$\begingroup\$ @ugoren Let the community vote (and vote yourself for ones you like), then choose the top answer after 7 days or so :) \$\endgroup\$ – Luc Sep 8 '12 at 21:48
  • 3
    \$\begingroup\$ Regarding possibility 2: NaN doesn't work. NaN+2 is again NaN, but NaN==NaN is false. \$\endgroup\$ – Martin B Oct 26 '12 at 13:51
  • 2
    \$\begingroup\$ The Scala solution, where x is a Set containing '2', and + means add to Set by the standard library, without redefining + yourself, doesn't fit into these 7 categories, IMHO. \$\endgroup\$ – user unknown Dec 19 '12 at 21:20
  • 2
    \$\begingroup\$ Why are this question and all the answers Community Wiki? \$\endgroup\$ – mbomb007 Jul 2 '15 at 18:49

98 Answers 98

4
votes
\$\begingroup\$

Ada

procedure test() is
  type x_type is mod 2;
  var x: x_type := 0; -- 0 or 1
begin
  if x /= x + 2 then
    put('Error');
  else
    put('Equal!');
  end if;
end test;

This is similar to Sage. We use a 1 bit integer which is allowed to wrap. We could also use a floating point, obviously.

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

Java

double x = Double.POSITIVE_INFINITY; // Double.NEGATIVE_INFINITY also works
System.out.println(x == x + 2);      // prints true
\$\endgroup\$
4
votes
\$\begingroup\$

Common Lisp

(let ((x   42)
      (x+2 42))
  (= x x+2))
;=> true
\$\endgroup\$
3
votes
\$\begingroup\$

C#

I didn't see any rule stating x had to be a number:

class Program {
    public static Program operator +(Program x, int y) {
        return x;
    }

    static void Main(string[] args) {
        var x = new Program();
        Console.WriteLine(x == x + 2);
    }
}

I defined x in such a way, but I also defined the + operator for the object stored in x.

http://ideone.com/ylNyM

\$\endgroup\$
3
votes
\$\begingroup\$

JavaScript

Here's a way do it without exploiting Infinity:

x = {
    value: 0,
    valueOf: function () {
        this.value += 2;
        return this.value;
    }
};

alert(x == x+2);
\$\endgroup\$
3
votes
\$\begingroup\$

C

let the computer find a value for x

int main()
{
  float x=0;
  while(x!=x+2)x+=2;
  printf("%f\n",x);
}
\$\endgroup\$
3
votes
\$\begingroup\$

Haskell

Prelude> instance Num Bool where (+) _ _ = True
Prelude> let x = True
Prelude> (x==x+2)
True

You could call that cheating but I assume that's what this is about :D

To make it work with more than just bool:

import Prelude hiding ((==),(+))
infixl 9 ==
(==) _ _ = True
(+) _ _ = True
\$\endgroup\$
3
votes
\$\begingroup\$

GAP

gap> x:=[];
[  ]
gap> x=x+2;
true

Here x is an empty array.

gap> x:=Z(2);
Z(2)^0
gap> x=x+2;
true

Here x is a generator of GF(2).

gap> x:=Integers;
Integers
gap> x=x+2;
true

Here x is the set of integers.

\$\endgroup\$
3
votes
\$\begingroup\$

Bash (0 character)

According to the rule, it looks like I can suggest my 0 character definition of x in bash, since without any definition, I have:

if (x==x+2); then echo "true"; fi

Output

true

Is it fair?

\$\endgroup\$
  • 1
    \$\begingroup\$ You need the expression x == x+2 to evaluate to true in some way. What you are doing is trying to output true regardless of the comparison result. \$\endgroup\$ – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ Mar 11 '14 at 12:07
  • \$\begingroup\$ Your comment and downvote isn't totally fair; my piece of bash code is merely here for proving the test is true (because bash doesn't display 'true' by itself); if you replace the expression with something evaluating to false, the result will be different: if false; then echo "true"; fi \$\endgroup\$ – Thomas Baruchel Mar 11 '14 at 12:40
  • \$\begingroup\$ Sorry, I misunderstood your code. What you actually do is define x to be =x+2, and the definition is always true (the rule allows this, since it shows an interesting aspect of the language). I mistakenly thought the true on its own is a command. \$\endgroup\$ – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ Mar 11 '14 at 12:49
3
votes
\$\begingroup\$

C

If you evaluate x == x+2 directly you will get a false, but I think it is interesting because it makes usage of a different language feature.

#include <stdio.h>

typedef struct {
  int bit:1;
} bitpack;

#define x a.bit
#define y b.bit

main() 
{
  bitpack a, b;
  y = x + 2;
  if (x==y) 
    printf("true\n");
  else 
    printf("false\n");
}
\$\endgroup\$
2
votes
\$\begingroup\$

PHP

<?php
  $x = 1e17;
  echo $x==$x+2;

Works in many other languages as well.

\$\endgroup\$
2
votes
\$\begingroup\$

Ruby

def x;1.0/0;end
puts (x == x+2) #=> true
\$\endgroup\$
2
votes
\$\begingroup\$

Scala

case object x {
  def +(n:Int) = this
}
//defined module x

x == x + 2
//res0: Boolean = true
\$\endgroup\$
2
votes
\$\begingroup\$

Java:

Float x = Float.MAX_VALUE;
System.out.println(x == (x + 2));

It prints "true"

System.out.println(x); 

prints 3.4028235E38

System.out.println(x+2); 

prints 3.4028235E38

I think this is caused by the loss of precision.

\$\endgroup\$
2
votes
\$\begingroup\$

In my opinion, x must have a data type of just ONE BIT.

Since C99, there is the data type _Bool ; So this should probably work:

_Bool x;
x = 0;
if (x == x + 2) ...
\$\endgroup\$
  • \$\begingroup\$ shouldn't x be promoted to int in expresseon x+2? \$\endgroup\$ – Vovanium Jun 10 '14 at 16:37
2
votes
\$\begingroup\$

Tcl, 22 chars.

proc (x args return\ 1
\$\endgroup\$
  • \$\begingroup\$ I don't know tcl but where is x+2 here? \$\endgroup\$ – phuclv Jun 4 '14 at 5:01
  • \$\begingroup\$ if you execute (x == x+2) after that, it will return 1. \$\endgroup\$ – Johannes Kuhn Jun 9 '14 at 22:40
2
votes
\$\begingroup\$

J

x =: 1

There is no == operator in J, sox==x+2 would actually mean x=(=(x+2)). Unary = is an operator, that will return 1 acting on any number, and as binary it is just an equality. So 1=(=(1+2)) => 1=(=3) => 1=1 => 1.

\$\endgroup\$
2
votes
\$\begingroup\$

SWI-Prolog

In query mode:

?- X = X+2, X == X+2.
X = X+2.

You just need to "assign" X = X + 2, then X == X + 2 will be true. Why make things so complicated?


Real explanation

In Prolog, x is an atom, so it is not possible to make x == x + 2 returns true. Variables in Prolog must start with an uppercase letter or underscore.

Therefore, my solution will define (uppercase) X so that X == X+2.

In Prolog, == compares 2 terms according to standard order of terms. Therefore, we only need to unify (= operator) X and X+2 before doing the comparison.

Note that the unification X = X+2 unifies X with the infinite term X = (((...)+2)+2)+2, much like how the unification X = f(X) unifies X with the infinite term X = f(f(f(...))).

In Prolog, when a query succeeds, it will return true if there is no variable, or it will return the variable binding which satisfies the query. This explains the output X = X+2.

\$\endgroup\$
2
votes
\$\begingroup\$

JavaScript

x=Math.log(0)
x==x+2

Prints

True

\$\endgroup\$
1
vote
\$\begingroup\$

C#

Using infinity

Double x = Double.PositiveInfinity;
Console.WriteLine(String.Format("Result: {0}", x == x + 2));

Also works with

Double x = Double.NegativeInfinity

(this is actually the same as 1.0 / 0.0 approach)

\$\endgroup\$
1
vote
\$\begingroup\$

Python:

>>> x=float('inf')
>>> x==x+2
True
\$\endgroup\$
1
vote
\$\begingroup\$

Haskell

f x = x == (x+2) where (==)=(<)

Always returns True, except when x is 1/0.

\$\endgroup\$
1
vote
\$\begingroup\$

Pascal

Here's a solution that takes a different approach (not the usual infinity trick).

Program xplus2;

Var value: integer;

Function x: integer;
Begin
   value := value-2;
   x := value;
End;

Begin
   If x=x+2 then
      Writeln('They are the same')
   else
      WriteLn('They are different');
End.

Works with 2+x too.

\$\endgroup\$
  • \$\begingroup\$ Nice idea but I don't the think the order of evaluation for your x=x+2 expression is guaranteed, so whether this works or not will be compiler-dependent. \$\endgroup\$ – Paul R Jun 9 '13 at 7:32
1
vote
\$\begingroup\$

Python

>>> x=1e999
>>> x==x+2
True
\$\endgroup\$
1
vote
\$\begingroup\$

Haskell

f x = (x == x + 2)
    where x + 2 = x

Calling f with any argument (whose type is in the Eq typeclass) will evaluate to True.

\$\endgroup\$
1
vote
\$\begingroup\$

Missing GolfScript?

0:2;
Then e.g. 15 2+ -> 15

\$\endgroup\$
  • \$\begingroup\$ Because in GS you can't write x == x+2 with any sense... Nice idea though, does it redefine 2? \$\endgroup\$ – tomsmeding May 6 '13 at 4:48
  • \$\begingroup\$ @tomsmeding well the syntax is different, but you can write e.g. x.2+= and yes, basically it does "2=0". \$\endgroup\$ – aditsu May 6 '13 at 6:11
1
vote
\$\begingroup\$

JavaScript

var x = Infinity;

So if you add anything to infinity it will remain infinity.

\$\endgroup\$
1
vote
\$\begingroup\$

Trying to come up with something that doesn't fall under one of the existing seven groups is tough:

def x
  rand(1..3)
end

puts x==x+2

33% of the time, it's right all the time...

\$\endgroup\$
  • 1
    \$\begingroup\$ I get 100% of the time “TypeError: can't convert Range into Integer”. (At least with Ruby 1.9.2.) In case you correct the syntax to rand 3 (that will result range 0..2, but should not matter for the result), you will get 11.11% correct answers. That is because you are comparing 2 random numbers and both have to take a certain value: the 1st number must be 0 and the 2nd 2. The 1st will be 0 in 33.33% of the tries, the 2nd will be 2 also in 33.33% of the tries. The 1st will be 0 and the 2nd will be 2 in 11.11% of the tries. \$\endgroup\$ – manatwork Jun 9 '13 at 12:13
  • 1
    \$\begingroup\$ Ha, yeah I was joking about the statistic - I knew it would be calling rand twice. I was just grasping at straws for something 'different'. Kernel#rand accepts ranges in 1.9.3 \$\endgroup\$ – jzig Jun 9 '13 at 23:48
  • \$\begingroup\$ Thanks for the version information. Didn't knew that. \$\endgroup\$ – manatwork Jun 10 '13 at 5:30
1
vote
\$\begingroup\$

$x == $х+2 for infinite pairs of numbers. Let's try it out.

PHP

$х = 1;
$x = 3;

if ($x == $х+2)
    echo "LOL it works. Sort of black magic?";

Nah, it's not black magic. Truth is that one x is Kha, that as Wikipedia states, it looks the same as the Latin letter X, in both uppercase and lowercase, both roman and italic forms.

There's actually no difference between them, so even if there are two different variables, rules are not broken, since I assumed that a x is a Kha.

\$\endgroup\$
1
vote
\$\begingroup\$

C

Defining x as 1==-2

#include<stdio.h>
#define x 1==-2

main()
{
  printf("%d", (x==x+2) ) ;
}
\$\endgroup\$

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