School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

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!




Find min(a,b) using only the four basic arithmetic operators

 

Find min(a,b) using only the four basic arithmetic operators

mike0613

17 Sep, 2009 - 02:03 AM
Post #1

New D.I.C Head
*

Joined: 15 Sep, 2009
Posts: 8

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

User is offlineProfile CardPM
+Quote Post


baavgai

RE: Find Min(a,b) Using Only The Four Basic Arithmetic Operators

17 Sep, 2009 - 03:24 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 4,348



Thanked: 411 times
Dream Kudos: 550
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Please don't double post: http://www.dreamincode.net/forums/showtopic126122.htm

Also, I already gave an answer that meets your parameters. I just didn't solve it outright. tongue.gif

Well, the "no floating point" is new. So now you've eliminated division as well?

User is offlineProfile CardPM
+Quote Post

cfoley

RE: Find Min(a,b) Using Only The Four Basic Arithmetic Operators

18 Sep, 2009 - 05:11 PM
Post #3

D.I.C Addict
Group Icon

Joined: 11 Dec, 2007
Posts: 660



Thanked: 63 times
Dream Kudos: 25
My Contributions
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
User is offlineProfile CardPM
+Quote Post

mike0613

RE: Find Min(a,b) Using Only The Four Basic Arithmetic Operators

30 Sep, 2009 - 03:03 AM
Post #4

New D.I.C Head
*

Joined: 15 Sep, 2009
Posts: 8

QUOTE(baavgai @ 17 Sep, 2009 - 03:24 AM) *

Please don't double post: http://www.dreamincode.net/forums/showtopic126122.htm

Also, I already gave an answer that meets your parameters. I just didn't solve it outright. tongue.gif

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.
User is offlineProfile CardPM
+Quote Post

mike0613

RE: Find Min(a,b) Using Only The Four Basic Arithmetic Operators

30 Sep, 2009 - 03:40 AM
Post #5

New D.I.C Head
*

Joined: 15 Sep, 2009
Posts: 8

cfoley, looks like you nailed it! Just a couple of comments:

(1) to get '1', a/a will suffice
(2) you don't actually need '1' to arrive at the solution.

So, simplifying your solution a bit, we have:

let:
a and b non-zero integers
j = a / b
k = b / a
c = j / (j + k)

then:
a>b => c = 1
a<=b => c = 0

and thus min( a,b ) will be given by:
d = (b - a) * c + a

this is because:
if a>b then c = 1 and thus d = b
if a<=b then c = 0 and thus d = a

Thanks for the help!
-Mike

This post has been edited by mike0613: 30 Sep, 2009 - 03:42 AM
User is offlineProfile CardPM
+Quote Post

AdamSpeight2008

RE: Find Min(a,b) Using Only The Four Basic Arithmetic Operators

30 Sep, 2009 - 04:26 AM
Post #6

The Bandido Coder
Group Icon

Joined: 29 May, 2008
Posts: 2,734



Thanked: 160 times
Dream Kudos: 3925
Expert In: vb.net, LINQ

My Contributions
Your wrong a / a <> 1 if a=0


min(a,B )=(1/2)*abs(a+B ) - (1/2)*abs(a-B )
where abs is defined as
abs(x)=(x*x)^0.5

Just need a way of get the absolute using the four standard operators.
User is offlineProfile CardPM
+Quote Post

OliveOyl3471

RE: Find Min(a,b) Using Only The Four Basic Arithmetic Operators

30 Sep, 2009 - 04:54 AM
Post #7

Quick and Dirty
Group Icon

Joined: 11 Jul, 2007
Posts: 5,654



Thanked: 107 times
Dream Kudos: 775
Expert In: Basketball

My Contributions
You keep changing the rules!

a and b non-zero integers

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 tongue.gif


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.


User is online!Profile CardPM
+Quote Post

mike0613

RE: Find Min(a,b) Using Only The Four Basic Arithmetic Operators

30 Sep, 2009 - 02:18 PM
Post #8

New D.I.C Head
*

Joined: 15 Sep, 2009
Posts: 8

QUOTE(OliveOyl3471 @ 30 Sep, 2009 - 04:54 AM) *

You keep changing the rules!

a and b non-zero integers

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 tongue.gif


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.
User is offlineProfile CardPM
+Quote Post

cfoley

RE: Find Min(a,b) Using Only The Four Basic Arithmetic Operators

4 Nov, 2009 - 10:04 AM
Post #9

D.I.C Addict
Group Icon

Joined: 11 Dec, 2007
Posts: 660



Thanked: 63 times
Dream Kudos: 25
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

cfoley

RE: Find Min(a,b) Using Only The Four Basic Arithmetic Operators

4 Nov, 2009 - 11:01 AM
Post #10

D.I.C Addict
Group Icon

Joined: 11 Dec, 2007
Posts: 660



Thanked: 63 times
Dream Kudos: 25
My Contributions
Oh, sack that. If they're both zero it breaks.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 06:26AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month