Welcome to MindCipher, a social repository of the world's greatest brain teasers, logic puzzles and mental challenges.

## No if() no abs()! 4

This is inspired by the puzzle "No if() only abs()" :

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

You can only use +- · / (basic arithmetic operators) and integers. NO if() or abs() is allowed in your construction.

Pure arithmetic ; )

(a+b + (a-b)* (-1 + 2*(1+(a-b)*(a-b)+(a-b))/(1+(a-b)*(a-b))) )/2

The absolute value can be expressed with arithmetic : abs(x) = x· (-1 + 2·(1+x·x+x)/(1+x·x))

so we can just use it : f(a,b) =( a+b+ abs(a-b) )/2 = (a+b + (a-b) · (-1 + 2·(1+(a-b)·(a-b)+(a-b))/(1+(a-b)·(a-b))) )/2

• Explanation for abs() :
• for x<0 , (1+x·x+x)<(1+x·x) so (1+x·x+x)/(1+x·x) = 0 and we end up with -x or |x|
• for x>=0 (1+x·x+x)>=(1+x·x) so (1+x·x+x)/(1+x·x) >= 1 , but (1+x·x+x)<=(1+x·x+x·x)<1+x·x+1+x·x = 2(1+x·x) so (1+x·x+x)/(1+x·x) < 2 hence (1+x·x+x)/(1+x·x) = 1 and the whole expression is x·(-1 +2) = x

A more elegant solution exists assuming a≠b :

f(a,b) = a*( ( 2·(a/b) ) / ( (a/b)+1) ) + b·( (2·(b/a) )/( (b/a)+1) ) if a>b than b/a = 0 and vice-versa, therefore the first addend apply only when a>b, and the second only when a<b . for the case b<a WLOG :

(2·(a/b) / (a/b) +1) will return 1 :

a/b >1 so 2·(a/b) > (a/b) +1 and therefore (2·(a/b) / (a/b) +1)= >1
But (2·(a/b) / (a/b) +1) < (2·(a/b) / (a/b) ) = 2

because the result is an integer, we are left with (2·(a/b) / (a/b) +1) = 1 we multiply the result by (a) to get (a) , which is the maximum.

Aurielle Payne

Couldn't follow the explanation, and the answer formula also doesn't work (it yields very close to a + 1). Basically, you're saying P < Q so P / Q = 0 ??? Failing all that, could use the gimmick that is square rooting the square for the absolute value.

Jean-Victor Côté

This is a fun one! spoilerEverything hinges on the fact that, whatever a and b are, abs(a-b) is equal to max-min.

leilei3915

2018529 leilei3915

# polo ralph lauren outlet

Check out other puzzles:

Like this? You might also like:
Mutilated Chessboard Problem
The Stealing Mailman
Electrical Deaths
Submitted by
Doodz