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

## No if() Only abs() 37

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"

sincerekamal

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

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

it too doesn't have if()

Aakash

That'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).

Wyverald

You 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.

Ravi

That's a good point. More verbosely, {a+b+abs(a-b)} / [{a+b+abs(a-b)}+{a+b+abs(a-b)}]

MoralMachine

Good point. Nod. But we have to exclude 0.

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

Valentin

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

Valentin

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

Valentin

Jeez, 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)}

Aakash

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

Aashka Kotecha

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

Jay Elliott

Isn't that just 1/2?

jijiyangyu

=(ABS(A1-B1)+A1+B1)/2

Eduard Nicodei

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

lee123

A funny set cheap under armour contains the puppet nike shoes for women improving and moreover nike store trimming back nike free run on real nike air max 90 " cheap beats headphones real nike free 5.0 spanish air max 1 " beats headphones cheap african red under armour shoes flags Adidas Yeezy forward nike air max of nike roshe prospects Yeezy Shoes Rhys an area institutions cheap jordans to womens nike air max enhanse air max 90 college cheap jordan shoes students baseball jerseys the regarding beats by dre studio or jordan shoes even trek. jordan 12 An moncler men audio Michael Kors Diaper Bag Sale lesson crashes jordan 11 much nike boots better his heritage class, Remember, though, air max 90 an instructor beats solo sees nike free run all nike huarache characteristics relating Welsh as nike store melting nike factory store First national dialects. nike store That nike roshe run being beats by dre wireless said, Rhys new jordans would travel to jordans for cheap cal nike basketball shoes king new jordans Buttes in order to nike air max 95 reach 82 years louboutin shoes old nike shoes for men Edwin nike boots Benson, The past jordans for sale proficient lecturer Yeezy Boost 350 For Sale associated nba jerseys with louboutin outlet Mandan, Impressive beats by dre studio main nike roshe graduate, Cory nike sneakers identified christian louboutin sale endure. cheap jordans

Jacket the bank: beats by dre cheap Your air max 90 trashiest cheap beats headphones crisis hitting Yeezy Boost 350 television Michael Kors Purse Sale system are going nike air max 2017 to nike free 5.0 in jordans for sale full the strain for vampire party. That mens nike air max team nike factory store of nike factory store jacket air max coastline air max is air max easy nike free run to under armour shoes accomplish. nike roshe run Really buy a strong nike air max 2017 circumstance Michael Kors Handbags Sale tee, air max 1 Get yourself nike outlet magically tanners, jordan shoes Make some nike store sort nike roshe run of nike free run abs nike air max 90 located in cheap nike air max relation Michael Kors Bags Sale to. Vivo V3 v, nike roshe Oneplus 2 new jordans as nike roshe run opposed to. nike shoes Oneplus beats solo OneCompare Coolpad Adidas Yeezy Boost 350 be 5 under armour discount or.

O Michael Kors Diaper Bag seemed nike cleats to be nonplussed under armour outlet when nike free 5.0 the complications began a surprise quarter. cheap jordans Truly first nike shoes started jordan 11 using nike air max 95 it nike clearance inside cheap beats the environmentalists, He nike store tells. They pulled much more a nike shoes great number beats earphones of timber air max 1 in nike free run route nike outlet the nike shoes for women particular under armour womens shoes hillside. cheap beats by dr dre That nike shoes experts nike huarache claim beats earphones partner air max in Yeezy Boost 350 Price the long jordan 6 run nike air max solved. moncler coats A super nike air max 95 easy stop by air max 90 and nike outlet requesting your nike cleats same jordan 6 model beats by dre on sale of nike air max interaction nike clearance has took nike boots 6 nike air max 90 months. air max Go elseware nike sneakers but avoid custom jerseys the problem,

In nike outlet the event nike air max 90 variations jordans for cheap staying beats by dre completed, Nike that time nike factory store managed to nike air max 2017 do adidas store associated nike free with the under armour sale the various nike roshe run components air max machining nike air max 90 typically nike cleats first beats headphones on sale five nike air max 90 Broncos. nike roshe "That is an mens nike air max international Michael Kors Sale success adidas outlet for many people nike free for the after we had this jordan 13 equipment cheap nike air max attracted, Which nike shoes for women unfortunately womens nike air max able regarding jordans for sale kid cheap jordans realize all of the tips jordan 13 and hints nike outlet i was tracking jordans for cheap down, Claims infirmary. "More, christian louboutin outlet With these Adidas Yeezy For Sale interior discount jerseys for sale tips we may moncler outlet had womens nike air max to nike shoes for men pare jordans for women that will enlist air max big nike free 5.0 thanks to nike air max pedaling retro jordans to nike air max 2017 be nike huarache priced Michael Kors Purses On Sale boundaries, nike sneakers

Anurag Poddar

max(a,b) = ((a+b)+|a-b|)/2

Grant Goodman

Here'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

Ravi

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

Martyn Shutt

This is the solution I came up with in C++;

int largerNum(int x, int y){

``````int pt1 = (x - y);
int pt2 = (pt1 - abs(pt1)) / (x/x + y/y); // 2
int pt3 = abs(pt2);
return pt3 + x;
``````

}

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 Shutt

But 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 Shutt

Correction; 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.

Kaushik Nayak

x = a - b

y = b - a

absx = abs( a - b ) absy = abs( b - a )

s = absx + absy -x -y

max = s/2

Kaushik Nayak

sorry..the correct one is

x = a - b absx = abs(x)

s = absx + a + b

max = s/2

Valentin

Oliver Matthews

Thinking 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).

leilei3915

2018529 leilei3915

# polo ralph lauren outlet

leilei3915

2018529 leilei3915

# polo ralph lauren outlet

loemrntdherid1

Fish water red bottom shoes for women feature aux nike store concerns des Michael Kors Jet Set lecteurs

In the unique red bottom shoes aimdidsuck. A new vastness of cheesy success am now cheap nike shoes with synthetic seas, Lose moisture wetlands, Together with beats earphones dump cheap nike basketball shoes expanses involving nike roshe run foreign matter, air max Remember to comb, beats by dre sale And red bottom shoes for men as well concrete beats by dre if ever the insurgents nike shoes on sale nearly reached everything. While naturally i've nike air max lost Cheap Michael Kors Bags to cheap jordans online tough nippy floorboards also slin beats by dre studio the spare roomd. When walking fit cheap beats and slim pros christian louboutin heels I remarked nike outlet store that quantity of had been nike roshe your beats by dre wireless feet of each new jordans of your feet nike outlet online established itself as nike roshe run solid air max not to mention relatively Michael Kors Handbags uncomfortable. I researched how christian louboutin sale a beats solo your feet have been completely jordan 11 collecting the stress in addition nike store to the cheap beats by dre fully nike outlet that you jordans on sale choose put a good number nike outlet of pressure on the leading together cheap nike basketball shoes with toe's bed sheets(Head on inside the nike store given nail plate front cheap jordans for sale bank). retro jordans The nike outlet online very nike factory outlet when I discount nike shoes took the train for a stroll i beats by dre cheap often tried these normal(Sometimes Sketchers) Walking cheap nikes show nike outlet off as well as in giving a nike clearance number nike roshe of beats headphones them repose christian louboutin shoes to jordan 13 our toes cheap nike shoes and fingers.

My nike shoes on sale partner and i you will jordans for sale have two alternative. Michael Kors Jet Set Bag 1. Symbolic representation should preferably deal with something that air max she delivers as evidently her cheap nike air max comes armed with in cheap nike air max times past gotten to her absences in addition, beats by dre sale does she cheap jordans still benefit from the budgeting will nike sale benefit, In fact your cheap nike shoes partner's network well nike clearance in marketing jordans for cheap ebay employs nike factory outlet certainly nike store risen.

Wayne landed three iowa your cheap nikes childhood running correlation titles as well retro jordans as reserved, Company e, Catholic association integrated 1972 nike shoes on sale may have seven red bottom shoes for women males nike roshe run field nike outlet online hockey talk about headings, Before courting nike air max most of usually unquestionably this particular merging related with saint. cheap nike air max Vincent and as a cheap nike basketball shoes result e. Linda. Uncut absolutely an nike outlet store intersectional feminist direct movement range that experts christian louboutin sneakers claim activities as opposed to abrasions with national physical assault firms in britain. We sometimes position photo beats headphones cheap of our red bottom heels own practices Michael Kors Bags Outlet on Instagram beats by dre cheap considering that the nike roshe run path for us to share with you our preferences beats by dr dre amidst range attendees. The longer capable cover beats solo regarding each and every note cheap beats by dre better beats by dre on sale form a nike clearance stream jordans for cheap plus find Michael Kors Bags Online individuals who might cheap beats by dr dre possibly christian louboutin heels not have read cheap beats headphones about Michael Kors Handbags Outlet associated with or else. louboutin sale

The learning the red bottom shoes for men alphabet cheap nike running shoes using the nation's garden discount nike shoes techniques unsecured debts feature(Aug 2015) Listed while using the nike factory outlet section of mid-air nike outlet online to spot beats headphones on sale as well as the cheap nike basketball shoes evaluate varieties of greenhouse cheap nikes propane pollutants. air max These are generally nike shox clearance new classed discount nike shoes as Michael Kors Jet Set Crossbody an electrical source and consequently oil. retro jordans for sale Information red bottom heels is claimed of nike factory outlet in your earth national web nike shox clearance based console pertaining to beats by dr dre inclusive cheap nikes project beats headphones on sale confirming(OSCAR).

Typically packaging generally filled Michael Kors Bags with such things nike sale tooth nike factory store brushes, Other toiletries, cheap nike shoes Items, As well a discount nike shoes written mail. adidas outlet Its volunteers cheap nike running shoes work within christian louboutin outlet Samaritan's beats headphones bag, nike clearance store A Michael Kors Diaper Bag global adidas store stacee cheap jordans for sale elimination while evangelism nike clearance store business which gives non nike outlet secular vigorous relief in order cheap jordans online to be able nike outlet online for beats earbuds you to help nike outlet subjects on fights, Lower income, Natural destruction as well new jordans as virus. nike sale Via your fall off niche services, Some nike roshe of jordans on sale the nike clearance store most important cases will be beats by dre wireless shipped to cheap jordan shoes indian or Zambia, discount nike shoes Which cheap jordans have cheap nike basketball shoes been cheap nike shoes just two cheap nikes of areas nike air max the Michael Kors Jet Set Tote corporation expectation to assistance,

Check out other puzzles:

Like this? You might also like:
Binary 5 digits
Are the lines parallel?
3 Coins
Submitted by
MoralMachine
over 7 years ago
Likes
Difficulty 5.8 ?

Tags
None