Tuesday, 25 January 2011

My friend Max...

Here's a little puzzle - I'm indebted to my colleague Nick for sharing this with me:

In computer programming, it's common to have a function that returns a maximum. For example, in Java, there's a function Math.max(double a, double b), which returns the higher of the values a and b. (If you're not familiar with programming, don't worry about the doubles, a and b are just numbers.)

So here's the challenge. Write a function that returns the higher of two values, using standard algebraic operators - i.e. addition, subtraction, multiplication, division, powers. You cannot use conditionals (if x, then y etc) or comparators (e.g. >, < or = ).

You don't even need to answer in programming code; an algebraic function that returns the higher of two numbers is sufficient.

Answers on a postcard?

6 comments:

  1. I think it is worth commenting that I most def. totes have a brother-in-law who is a NERD!!! =D hahaha...

    Care to provide an example of a said function before I even attempt to display my general ineptitude at math to the world? Or is this a trick question? One never knows with this family...:)

    ReplyDelete
  2. i vote trick question. it makes me feel better about myself. does it count if i draw you a beautifully illustrated and aesthetically pleasing function, even if it's wrong???

    ReplyDelete
  3. no idea. I was at school for 13 hours today, so I'm a bit tired now. :) Maybe a piece-wise function?? :)

    Good job on getting a blog! Will be good to hear from you more!

    ReplyDelete
  4. It's not a trick question, and I will post my solution in due course. I'm not sure what you want by an example... or is this just a request for an answer?

    For Rachel, one could define a max function as being a piece-wise function: e.g. if (a > b) then a, else b. However, this would not satisfy the criteria (it uses conditionals and comparators!). Could you write a piece-wise function without conditionals?

    ReplyDelete
  5. This is Mark, and I think I can answer your problem. Let X and Y be the 2 numbers. The max of the 2 numbers is:
    (X+Y)/2 + .5*((X-Y)^2)^.5)
    where ^ is the exponential function. This solution assumes that the square root function only returns the positive root.

    ReplyDelete
  6. And the prize goes to... Mark

    That was the solution I had come up with.

    I consider that the assumption that the result of the square root function is the positive root is acceptable. Even if the square root function returned both the positive and the negative roots, you would know that you always want the positive root.

    Congratulations!

    ReplyDelete