Need help troubleshooting your PC? We have a couple of geeks in the forum who can help you

Forum rules
Please read the forum rules carefully before you post.

If you like AnimorphsFanForum.com, please consider making a donation. Any donation will go towards the cost of the hosting, the domain and any other running costs.

I'm not sure where this would thread would be most appropriately placed... Someone please shift it if necessary...

The other day i was kinda bored so i asked my friend for some challenging program to write. He gave me this one. Apparently it's been asked in some Microsoft interview.

The question is: Write a program to find the larger of two positive integers, without using the >,<,>= or <= operators.

Now i've asked two other people, each gave a different solution, with varying efficiency, so i thought it'd be nice to post it on the internet and get as many different solutions as possible.

Here are the three solutions i know:

Mine:

Spoiler:

place both the numbers in a loop. Decrement each no. by one in every iteration. Stop the loop as soon as one of them hits zero. That one is the smaller no. Of course, you'll need temporary variables to save the original values...

Another one:

Spoiler:

Divide one no. by the other. Convert the quotient to an integer (useful for floating points). If a/b is a zero, b is greater. Else, vice versa.

And the best one yet:

Spoiler:

Find out the sum and difference between the two numbers. Add the sum to the difference. Divide that number by two. The result will always be the greater no. (Works with floating points and negatives too)

So that's that. Post your solution. In a spoiler, so that others can still be creative and original.

There are 10 types of people in this world. Those who understand Binary, and those who don't. Now 1/4th a Computer Scientist (or Computer Science Engineer, depending on my mood), i can safely say i fall into the first category.

Hmmm. My knowledge of programming is extremely limited, so I'm not sure how workable anything I suggest is. Probably overcomplicated, but hey.

Spoiler:

If the square root of a-b is a real number, a is the greater number, otherwise b. Taking the logarithm works in the same way.

Spoiler:

If lim x->∞ (a/b)^x is 0, a is the smaller number, otherwise b. Yes, I thought it was a reasonable idea to use limits to find the smaller of two integers.

Spoiler:

Take the average, add 1/2. If that's one of the numbers, stop, that is the greater number. Otherwise, increase by one until you reach one of the numbers. That is the greater number. Come to think of it, you could simply add the absolute value of a-b divided by two, but then that's exactly the same as the third suggestion, only in a different order.

And the first one I think of that's relatively simple:

Spoiler:

If the absolute value of a-b equals a-b, a is the greater number, otherwise b.

The third one is probably the only one that'll work in pretty much any language, as some languages don't have any library functions to evaluate limits, or to check for real nos or absolute values.

Still, it's quite interesting to see other people's thought processes.

There are 10 types of people in this world. Those who understand Binary, and those who don't. Now 1/4th a Computer Scientist (or Computer Science Engineer, depending on my mood), i can safely say i fall into the first category.

[code=php]<?php
$a=99;
$b=7;
if($a == $b)
echo 'Numbers are equal';
else if(floor($a / $b) == 0)
echo 'a is less than b';
else echo 'a is greater than b';
?>[/code]

Edit: I see that this is the second solution.
Edit 2: Current's third solution is the most elegant of them all. Awesome thinking, Current.

Ellimist wrote:
Edit 2: Current's third solution is the most elegant of them all. Awesome thinking, Current.

... It doesn't work for negative numbers. Sure, that's not part of the question....

There are 10 types of people in this world. Those who understand Binary, and those who don't. Now 1/4th a Computer Scientist (or Computer Science Engineer, depending on my mood), i can safely say i fall into the first category.

No absolute values pretty limits the simple ways to check...
Depending, you could just replace every time I used absolute values with

Spoiler:

square then take the square root, but I have no idea whether the usual practice with square roots is returning both values, or only the positive one. Similarly, square root then squaring depends on how handling imaginary numbers is usually done.

I could really use some programming knowledge right now, it's pretty obvious I'm thinking this in purely mathematical terms.

You're thinking in mathematical terms stumped my programming knowledge...even though it is quite limited, the solutions I came up with before looking at the spoilers pretty much consisted of the ez-mode solutions inside the spoilers I would use for sample programs in a class, not actual applications

Current wrote:No absolute values pretty limits the simple ways to check...
Depending, you could just replace every time I used absolute values with

Spoiler:

square then take the square root, but I have no idea whether the usual practice with square roots is returning both values, or only the positive one. Similarly, square root then squaring depends on how handling imaginary numbers is usually done.

Actually, same problem, square roots require library functions. Not that that's a problem, it's still fantastic that you could think of so many different ways.

Current wrote:
I could really use some programming knowledge right now, it's pretty obvious I'm thinking this in purely mathematical terms.

That's perfectly fine. All programming knowledge does is limit the number of feasible solutions your otherwise free mind would think of. In fact some of us programmers (or atleast just me) find it far more difficult to think up algorithms in real world logic, preferring to first write the program directly, and then reverse engineer the algorithm when it is required (i'm talking about at college exams).

There are 10 types of people in this world. Those who understand Binary, and those who don't. Now 1/4th a Computer Scientist (or Computer Science Engineer, depending on my mood), i can safely say i fall into the first category.