So I've been trying to make some sort of a calculator tool. One of the function is to check if a factor of a number multiplies to something but at the same time adds to something else. This is helpful for factoring a trinomial. I found the factors but I don't know how to proceed. This is what I have so far
public static void MA() {
int sum = 0;
int product = 0;
ArrayList<Integer> Factors = new ArrayList<Integer>();
System.out.println("Enter the sum of the number");
sum = sc.nextInt();
System.out.println("Enter the number it should multiply to");
product = sc.nextInt();
if(product < 0){
for(int i = product; i <= Math.abs(product); ++i) {
// skips the iteration for i = 0
if(i == 0) {
continue;
}
else {
if (product % i == 0) {
Factors.add(i);
}
}
}
}
else{
for (int i = 1; i <= product; ++i) {
// if number is divided by i
// i is the factor
if (product % i == 0) {
Factors.add(i);
}
}
}
System.out.println(Factors);
}
Keep in mind the architecture of my program is a main class that calls on all other functions.
EXAMPLE: X^2 + 9x + 20 = (x+5)(x+4)
4*5 = 20 4+5 = 9
Your problem can be succinctly expressed as follows:
A simple solution to which is:
If a and b are not null, a pair that summed to
sum
was found.