Friday, October 2, 2009

Name That Function


let a, b, c be Non-Negative Integers
let f(x, y) be a function such that:
f(a, b) = c
f(b, a) = c
f(a, c) = b

what is f?

I'm not going to mess around with the formal notation of math, because the answer isn't something you can't figure out.

How to check your answer: pick arbitrary values for a and b. Use what you think f is and the formula f(a, b) = c to calculate c. Then see if the other two formulas are true with those values of a, b, and c.

3 comments:

thejoshwolfe said...

Golf (in sourcecode bytes)
Perl: 18 (assuming I know what I'm doing)
Ruby: 18 (assuming I know what I'm doing)
Jax: 21 (just the function)
Haskell: 22
Python: 28
C/C++: 31 (just the function)

Andy said...

sub f{return abs($_[0]-$_[1])}

thejoshwolfe said...

Andy:
f(3, 8) = 5
f(8, 3) = 5
f(3, 5) = 2