Interview Question
Country: United States
Hello,
let the 2 nos be a and b so the given is
a*b = (say)8 -- equation1
a+b=6. -- equation 2
You can keep 8 and 6 and x and y too.
so a = 6-b => substitute this in equation 1
=> (6-b)*b = 8
=>6b-b^2 = 8
=> b^2 - 6b + 8 = 0
Now this becomes a quadratic equation and will always be in such problems. solve get the value of b and by substituting that into either of equations above of ur choice will get the value of a.
So in this case
(b-4)(b-2)=0 so the o/p is 4 and 2
Edit:::::
In short, in such problems try to reduce the number of variables to one and try to represent others in term of only one variable. Then that variable will help you to find out rest of the values.
Hi
This is the code:
package CareerCup;
/**
* Created by Deven on 17/08/15.
*/
public class SumProduct {
public static void main(String[] args) {
int sum=6,product=8;
int a,b;
a=(sum +(int)Math.sqrt((sum*sum)-(4*product)))/2;
b=(sum -(int)Math.sqrt((sum*sum)-(4*product)))/2;
System.out.println("Answers are:" + a + " "+ b);
}
}
Thanks.
package CareerCup;
/**
* Created by Deven on 17/08/15.
*/
public class SumProduct {
public static void main(String[] args) {
int sum=6,product=8;
int a,b;
a=(sum +(int)Math.sqrt((sum*sum)-(4*product)))/2;
b=(sum -(int)Math.sqrt((sum*sum)-(4*product)))/2;
System.out.println("Answers are:" + a + " "+ b);
}
}
package CareerCup;
/**
* Created by Deven on 17/08/15.
*/
public class SumProduct {
public static void main(String[] args) {
int sum=6,product=8;
int a,b;
a=(sum +(int)Math.sqrt((sum*sum)-(4*product)))/2;
b=(sum -(int)Math.sqrt((sum*sum)-(4*product)))/2;
System.out.println("Answers are:" + a + " "+ b);
}
}
Simplify the equations:
a+b=x --> a=x-b
a*b=y --> a=y/b
Substituting (x-b) for 'a' in the equation and solving the quadratic equation yields:
(x-b)*b=y --> b^2 -x*b + y =0 --> b=(x+-(x^2-4*y)^(1/2))/2
a, b == (x-(x^2-4*y)^(1/2))/2, (x+(x^2-4*y)^(1/2))/2
Validating with the numbers from the example
a=(6-(36 - 32)^(1/2))/2 --> a=(6-2)2 -->a=2
b=(6+(36 - 32)^(1/2))/2 --> b=(6+2)2 -->b=4
Solve for 'a' in the equations:
a+b=x --> a=x-b
a*b=y --> a=y/b
Substituting (x-b) for 'a' in the equation and solving the quadrtic equation yeilds:
(x-b)*b=y --> b^2 -xb + y =0 --> b=(x+-(x^2-4*y)^(1/2))/2
a, b == (x-(x^2-4*y)^(1/2))/2, (x+(x^2-4y)^(1/2))/2
Validating with the numbers from the example
a=(6-(36 - 32)^(1/2))/2 --> a=(6-2)2 --> a=2
b=(6+(36 - 32)^(1/2))/2 --> b=(6+-2)2 --> b=4
Hi
Here's the code
package CareerCup;
/**
* Created by Deven on 17/08/15.
*/
public class SumProduct {
public static void main(String[] args) {
int sum=1037,product=29232;
int a,b;
a=(sum +(int)Math.sqrt((sum*sum)-(4*product)))/2;
b=(sum -(int)Math.sqrt((sum*sum)-(4*product)))/2;
System.out.println("Answers are:" + a + " "+ b);
}
}
Compute the quadratic result for values:
public static void printComponents(int sum, int product){
int sqrRt = (int)Math.sqrt((sum * sum) - (4 * product));
int posA = (-sum + sqrRt) / 2;
int posB = product / posA;
if(posA + posB != sum ){
posA = (-sum - sqrRt) / 2;
posB = product / posA;
}
java.lang.System.out.println(""+posA+", "+posB);
}
- Lucas crawford August 17, 2015