Finding two biggest numbers out of three numbers efficiently
It is a simple problem, and many of us must have had such problem either in exam or exercises or math-quiz or may be in real life. There are many ways to solve this, here is one efficient way to solve it. I do not claim it is to be most efficient or the only method. I found it out so I thought its good to share.
#EDIT Started:
2 more ways which our professor taught in the class,
1) (a+b+c) - min(a,b,c) => takes total sum and subtracts minimum no out of a,b,c.
2) (max (a+b), (a+c), (b+c)) => takes sum of each unique pair i.e. a&b, b&c and a&c, and find which sum is maximum.
But I think these 2 methods are less efficient than mine upper one, because they have more iterations and will take more time (though negligible, but it does matter in case of very large computations or in super-computers) to calculate the result.
(From programmer's perspective) One more thing, they make use of min and max functions, either inbuilt or user-defined, which will take more time to calculate the result.
#EDIT Complete.
I will soon post one more trick - easiest way to square any number, and I mean its really easy, you can easily square any number with any digits.
It is not programming, its just logic.
Consider three numbers a, b, c
First I will put the (what I think is) dumbest way - step by step, considering each scenario,
//Finding 2 biggest numbers from list of three numbers i.e. a, b, c
//Considering all values are different like 12, 45, 48
if(a>c && b>c){
"Two big numbers are a & b"
}else if(b>a && c>a){
"Two big numbers are b & c"
}else if(a>b && c>b){
"Two big numbers are a & c"
}
//in case any 2 values are equal like 22, 22, 1212
//so there are three cases either a=b or b=c or a=c
//a=b case
else if(a==b && a>c){
"Two big numbers are a & b"
}else if(a==b && a<c){
"Two big numbers are a & c or b & c"
}
//b=c case
else if(b==c && b>a){
"Two big numbers are c & b"
}else if(c==b && b<a){
"Two big numbers are b & a or c & a"
}
//c=a case
else if(a==c && a>b){
"Two big numbers are a & c"
}else if(a==c && a<b){
"Two big numbers are b & c or b & a"
}
//if all of them are equal, i. e. a=b=c
//in case all three are same 22,22,22
else if(a==b && b==c){
"Two big numbers are any two"
}
And if you look carefully, there are only three unique results i.e.
two big numbers are either a & b or b & c or a & c.
So if you merge all above conditions carefully we will get, I think its easiest and one of the efficient ways,
//Finding 2 biggest numbers from list of three
//in case, all are different values, like 233,121,2323
if(a>=c && b>=c){
"Two big numbers are a & b"
}else if(b>=a && c>=a){
"Two big numbers are b & c"
}else if(a>=b && c>=b){
"Two big numbers are a & c"
}
Now find out how I found out these conditions.
#EDIT Started:
2 more ways which our professor taught in the class,
1) (a+b+c) - min(a,b,c) => takes total sum and subtracts minimum no out of a,b,c.
2) (max (a+b), (a+c), (b+c)) => takes sum of each unique pair i.e. a&b, b&c and a&c, and find which sum is maximum.
But I think these 2 methods are less efficient than mine upper one, because they have more iterations and will take more time (though negligible, but it does matter in case of very large computations or in super-computers) to calculate the result.
(From programmer's perspective) One more thing, they make use of min and max functions, either inbuilt or user-defined, which will take more time to calculate the result.
#EDIT Complete.
I will soon post one more trick - easiest way to square any number, and I mean its really easy, you can easily square any number with any digits.
Comments
Post a Comment