Live data from Hacker News

Write a program that makes 2 + 2 = 5

codegolf.stackexchange.com

71–76 of 76 posts

Re: Write a program that makes 2 + 2 = 5

#71

Ruby solution (don't have enough rep to post to codegolf): def mad_addr(a,b) a == b && a == 2 ? (a.object_id + b.object_id)/a : a + b end Explanation: If a and b are both equal to 2 we can sum the values of the object_id's (the object_id of 2 is 5), and then divide the sum by 2. [Edit to add] For all other numbers this should behave as expected. I just learned the object_id of 2 is 5 while doing this exercise, and I…

a == b && a = 2 ? (a.object_id + b.object_id)/a : a + b It looks like you are missing an equals sign on a == 2. I just learned the object_id of 2 is 5 while doing this exercise, and I would like to continue learning more about how Ruby works. So if you have feedback, criticisms, or other ideas regarding this solution please feel free to share :) The object_id appears to be the tagged pointer that contains the value c…

Thanks, nitrogen. Following your info led to a couple of other neat discoveries, and a bunch more useful material [1].

As a result here are some new ways I learned to get 5 from 2 + 2:

  require 'fiddle'
  a = 2
  b = 2
  Fiddle::Pointer.new(a.object_id  5
Note: This happens because calling `to_value` on the `Fiddle::Pointer` object returns a Float (2.0000000000000004) instead of Fixnum (which was a surprise to me).

And here's another using `Object#tap`:

  a = 2
  (a + a).tap{|x| a = x + 1}.send(:eval,'a')
  => 5
[1] These links were helpful for me. The last one is an MIT licensed book called, "Kestrels, Quirky Birds, and Hopeless Egocentricity."

http://stackoverflow.com/questions/1872110/is-ruby-pass-by-r...

http://patshaughnessy.net/2014/1/9/how-big-is-a-bignum

http://stackoverflow.com/questions/2818602/in-ruby-why-does-...

http://combinators.info/#kestrels

Edit: line formatting

Re: Write a program that makes 2 + 2 = 5

#72
post #32

Earlier quoted context omitted.

I think all these bytes are not creative. !![]+2+2 ? !''|2+2 ? 1|2+2 ? Why the use of new operators? Why not just write 1+2+2 ?

To make many more examples of why Javascript is not a language for beginners.

  var tens = ['10','10','10','10','10','10','10','10'].map(parseInt);
  var ten = tens[tens.length-1];
  var two = ten % 4;
  console.log(two + 2);

Re: Write a program that makes 2 + 2 = 5

#73
post #59
post #56

Earlier quoted context omitted.

It also leads to some interesting programming errors in Java. class A { public static void main(String[] args) { Integer a = 100, b = 100; Integer c = 10000, d = 10000; System.out.println(a == b); System.out.println(c == d); } } Prints: true false For those unaware, the == check above is doing an Object check (are they the same object). The cache makes the '100' return the same instance of an object. To do actual val…

what a dumb language! operator overloading should have bound the equals method to the == operator without the programmer having to be aware of this. when in java do you ever want to compare if two objects are the same referenced with the same ptr? Never, so why not do the right thing by the user of the language. At least be consistent between primitives and objectives. Sadly, I have to use this language daily. My hea…

Python has exactly the same behavior if you compare integers using the is operator. Two int-variables with same value are only "is-equal" up until somewhere around 100000 also. Many beginners do this mistake as they think you should always use is in python and don't even know that == also exist.

Re: Write a program that makes 2 + 2 = 5

#74

Earlier quoted context omitted.

a == b && a = 2 ? (a.object_id + b.object_id)/a : a + b It looks like you are missing an equals sign on a == 2. I just learned the object_id of 2 is 5 while doing this exercise, and I would like to continue learning more about how Ruby works. So if you have feedback, criticisms, or other ideas regarding this solution please feel free to share :) The object_id appears to be the tagged pointer that contains the value c…

Thanks, nitrogen. Following your info led to a couple of other neat discoveries, and a bunch more useful material [1]. As a result here are some new ways I learned to get 5 from 2 + 2: require 'fiddle' a = 2 b = 2 Fiddle::Pointer.new(a.object_id 5 Note: This happens because calling `to_value` on the `Fiddle::Pointer` object returns a Float (2.0000000000000004) instead of Fixnum (which was a surprise to me). And here'…

If you want to dig even deeper into the way Ruby works, I recommend trying to write a C extension (or at least reading the documentation):

http://www.ruby-doc.org/docs/ruby-doc-bundle/ProgrammingRuby...

Post reply on HN