Given two variable a and b, construct a function f(a,b) which return the maximum one in {a,b}.

You can only use +- · / and abs() (to take the absolute value). NO if() is allowed in your construction.

Pure arithmetic ; )

{a+b+abs(a-b)}/2

Also, {a+b-abs(a-b)}/2 is the smaller one in {a,b}

Here's an intuitive explanation by @Grant Goodman (in comments): "(a+b)/2 gives you the average, which is directly in between a and b, and it is 1/2 the difference from either a or b. So to get to the larger one, just add the difference of the two divided by two, giving you (a+b)/2 + abs(a-b)/2, or (a+b+abs(a-b))/2"

## Comments

Sign uporlog in with Facebookto comment.ValentinGreat question! Thoroughly enjoyed thinking about this one.

sincerekamalsure it would b wrong...but i tried something...

int res=(a-b)>0?a:b;

it too doesn't have if()

AakashThat's a good point, but I think the idea is to avoid conditionals altogether. Your compiler turns the ternary ( ? : ) operator into the same code that it turns an if statement into (i.e. a conditional branch with two blocks).

WyveraldYou didn't say you could use a 2 in there.

So you'll need to make one with (a/a + b/b) or something like that.

RaviThat's a good point. More verbosely,

{a+b+abs(a-b)} / [{a+b+abs(a-b)}+{a+b+abs(a-b)}]MoralMachineGood point. Nod. But we have to exclude 0.

Whatever, the question should just emphasize on "pure arithmetic" or ask for an algebra expression.

ValentinOr: {a + b + abs(a-b)}/(a/a + a/a). Slightly shorter version.

ValentinThis, however, of course wouldn't work if a was 0. So perhaps the parent's reply is actually more general.

ValentinJeez, I'm going in circles here. ravi's reply is incorrect. That whole expression simply equals 1/2. As long as a and b are not both equal to 0, the following would suffice: {a + b + abs(a-b)}/{(a + a + b + b)/(a+b)}

AakashAgreed, unless both a and b are zero...

Aashka Kotechathis won't hold true if a and b both are odd

Jay ElliottIsn't that just 1/2?

Oliver MatthewsThinking about this, I'm fairly sure it is impossible unless you do one of the following: 1. allow numbers in the algorithm (as per solution) 2. assume at least one of the values is not 0. (as per below) 3. allow ^ 4. use a system that defines 0/0 as 1 (i.e. is wrong).

the last two would allow you to implicitly get a 1 by doing (a^a)/(a^a) or a/a respectively (and thus a 2 through addition).

Grant GoodmanHere's an intuitive derivation of the solution:

(a+b)/2 gives you the average, which is directly in between a and b, and it is 1/2 the difference from either a or b. So to get to the larger one, just add the difference of the two divided by two, giving you (a+b)/2 + abs(a-b)/2, or (a+b+abs(a-b))/2

RaviThat's an awesome explanation. MoralMachine, I suggest adding it to the puzzle solution.

Eduard NicodeiWohooo touring-completeness achieved once again! Time to play with this in Haskell.

Anurag Poddarmax(a,b) = ((a+b)+|a-b|)/2

Martyn ShuttThis is the solution I came up with in C++;

int largerNum(int x, int y){

}

I decided it would be easier for me to get to the solution if I broke it up into individual parts. But working from that, as a single expression;

return (abs((x - y - abs(pt1)) / (x/x + y/y))) + x;

Martyn ShuttBut as highlighted in other posts, it doesn't work if either number is zero. It actually crashes my compiled application. Probably due to dividing by zero.

Martyn ShuttCorrection; as a single return statement it should be

return (abs((x - y - abs(x - y)) / (x/x + y/y))) + x;

I accidentally left variable pt1 in there.