Join 307,001 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,966 people online right now. Registration is fast and FREE... Join Now!
Looking for a simple algorithm/expression to find min of two numbers using only four basic arithmetic operators (add, subtract, multiply, divide).
Alternatively, find how to make '1' if a<b and '0' otherwise (again, using only the 4 arithmetic operators). Knowing how to do this I can answer the original question.
Another route is to be able to get an absolute value of a - b using the 4 operators above. Knowing this will also enable me to answer the original question.
Clarifications:
No conditional statements are allowed, no floating point, no min( a,b ). You can only use the two given numbers ( a and b ), and the four basic arithmetic operators.
<To make '1' or '0'> means that the result of the operation should be '1' or '0' depending on whether a>b or b>a. If you just want to make '1' (unconditionally) a/a will do, and to make '0', you can do a-a.
Thanks, Mike
This post has been edited by mike0613: 17 Sep, 2009 - 02:04 AM
I'm going to go out on a limb and assume the minimum of two numbers is not defined if those numbers are the same. Not sure I agree there but let's go with it.
If the numbers can't be the same then they can't both be zero.
Also, no floating point implies integer division.
(a+b )/(a+b ) = 1
So I can use 1.
(a+1)/(b+1) = j a > b => j >= 1 a < b => j = 0
(b+1)/(a+1) = k a > b => k = 0 a < b => k >= 1
Note: either j or k is zero, never both. Now:
j/(j+k)
if j = 0 we get 0/k = 0
If k=0 we get j/j = 1
This post has been edited by cfoley: 18 Sep, 2009 - 05:11 PM
Also, I already gave an answer that meets your parameters. I just didn't solve it outright.
Well, the "no floating point" is new. So now you've eliminated division as well?
Sorry baavgai, but your solution has no relation to the stated requirements. No one in the other thread was able to solve the problem - that's why I posted it here, hoping that the more theoretical computer science thread will be more successful.
If we had known before that neither a nor b can be zero, then NickDMax could have solved this a long time ago. //notice I did not say I could have solved it
QUOTE(NickDMax @ 18 Sep, 2009 - 01:51 PM)
You can do it if you know that either a or b is non-zero, but if both a and b are zero then... well its hard to make something out of nothing.
If we had known before that neither a nor b can be zero, then NickDMax could have solved this a long time ago. //notice I did not say I could have solved it
QUOTE(NickDMax @ 18 Sep, 2009 - 01:51 PM)
You can do it if you know that either a or b is non-zero, but if both a and b are zero then... well its hard to make something out of nothing.
I don't agree that I kept changing the rules, it just hasn't been stated explicitly in the original requirements. But boy, the people in the C/C++ thread made so many preposterous assumptions trying to solve this problem (none of which are in the requirements), - they could as well make this one, which is not unreasonable. E.g., cfoley above made an assumption that a<>b which quickly lead to the correct solution. (The other thread is here: http://www.dreamincode.net/forums/index.ph...5&t=126122)
@AdamSpeight2008: as stated in the solution post, a and b are non-zero, so no problem with a/b.
Actually, looking at my solution again, you don't need the assumption a != b. It works even if they are the same. So mine works for all integers, including zero.