0

Regarding quadratic equations (learn more here), I've taken the a, b, and c of the equation as input.

A sample equation will be this: 21x^2 - 8x - 4 Here, a = 21, b = -8, c = -4. So, on solving (without formula), => 21x^2 - 14x + 6x - 4 = 0.

I need the two middle numbers, that is, in this case 14 and 6 (read factors). I think I have done all correct, but the input seems to be infinite and does not stop at all. Can you please rectify the mistake? I am also curious to learn why that happened.

import java.util.Scanner;
public class QuadFact {
    static Scanner sc = new Scanner(System.in); 
    static int a,b,c; 
    static int P, diff, p; 
    static int i;
    static boolean found = false;

    void accept(){
        System.out.println("Enter the a, b, c");
        a = sc.nextInt(); b = sc.nextInt(); c = sc.nextInt();
    }

    void compute(){
        P = a * c;
        diff = 0;
        while(!found){
           for (i = b + 1;;i++){
                diff = i - b;
                p = i * diff;
                if (p==P) {
                    found = true;
                    break; 
                }
            }
        }
    }

    void display(){
        System.out.print("These are the raw numbers, should be   correct.  
        Still,\n it is advisable you verify it.");
        System.out.println("One factor: " + i);
        System.out.println("Other factor: " + diff);
    }

    public static void main(String[] args){
        QuadFact a = new QuadFact();
        a.accept();
        a.compute();
        a.display();
    }
}
ayePete
  • 411
  • 1
  • 7
  • 23
Mission Coding
  • 301
  • 4
  • 12
  • Try posting your code here again. We can help you format it. Links can go dead, and some of us with firewalls (such as myself) can't even get to your link. – Hovercraft Full Of Eels Mar 31 '15 at 12:15
  • OK, what happens when you try to debug this with your debugger, or failing that, when you use println statements to see what your code is doing at critical spots? – Hovercraft Full Of Eels Mar 31 '15 at 12:27
  • You've got your signs confused. In your example the numbers you want are -14 and 6, not 14 and 6. You should be looking for two numbers with product P and sum b, whereas you seem to be searching for numbers with product P and difference b. – Paul Boddington Mar 31 '15 at 12:39
  • The input is infinite – Mission Coding Mar 31 '15 at 12:54

2 Answers2

2

I think you have to look "on both sides" of b for a factor pair that adds up to b and produces the product a*c.

void compute(){
    P = a * c;
    while(!found){
    for( i = 1; ; i++ ){
            diff = b - i;
            if (i * diff == P) {
                found = true;
                break; 
            }
            diff = b + i;
            if (-i * diff == P) {
                found = true;
                break; 
            }
        }
    }
}
laune
  • 31,114
  • 3
  • 29
  • 42
1

ok, I wrote a code for that.

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //Declare and get the variables 
        int a, b,c;

        Scanner s = new Scanner(System.in);

        System.out.println("Enter A");

        a = s.nextInt();

        System.out.println("Enter B");

        b = s.nextInt(); 

        System.out.println("Enter c");

        c = s.nextInt(); 

        //A should be 1 if not divide a, b and c by a
        if (a>1) {

            b= b/a;
            c=c/a;
            a= a/a;
        }
        //Just printing what the values of ABC IS AGAIN
        System.out.println("A = "+a+" B = "+b+" C = "+c);
        //Just printing what the values of ABC IS AGAIN but in reverse, that is if c was 5 it becomes -5 
        //another way to reverse positive to negative and vice versa
        System.out.println("A = "+(0-a)+" B = "+(0-b)+" C = "+(0-c));

        //Set i as c and start the loop from highest to lowest. 
        for (int i = Math.abs(c); i>0 ;i-- ) {

            //if a multiple is found it proceeds and checks for the 
            //multiple combination that when multiplied you get C and the addition or subtraction gives you B
            if (c%i==0) { 
                int fac1 = c/i;
                //Displays the multiples found

                System.out.println(i+" x "+fac1+" = "+c);

                //There are 4 possible outcomes or cases
                //case 1 multiple 1 - multiple 2 = b
                //case 2 multiple 2 - multiple 1 = b
                //case 3 multiple 1 + multiple 2 = b
                //case 4 -multiple 1 + -multiple 2 = b

                if (i-fac1 == b) {
                    //System.out.println("case 1: " + i+"-"+fac1+"="+b);
                    answer(i,fac1);
                    break;
                }
                else if(fac1 - i == b){

                    //System.out.println("case2: "  + fac1+"-"+i+"="+b);
                    answer(fac1,i);
                    break;
                }

                else if (i +fac1 == b ) {

                    //System.out.println("case3: "  + i+"+"+fac1+"="+b);
                    answer(i,fac1);
                    break;
                }
                else if((0-Math.abs(i)) + (0- Math.abs(fac1))==b) {

                    //System.out.println("case4: "+"-"+i+ " + "+ "-"+ fac1 +"="+b);

                    answer((0-Math.abs(i)),(0- Math.abs(fac1)));
                    break;
                }


                else {
                    System.out.println("Probably not a factorizable Equation");
                }

            }

        }

    }
    //Use this method to show the final answer
    private static void answer(int f1, int f2) {

        System.out.println("x = "+(0-f1) +" or x = "+(0-f2));

    }

}
Korashen
  • 2,144
  • 2
  • 18
  • 28
Jebjosh
  • 83
  • 8
  • note this mostly works when a = 1 and can be divided by c or b evenly. in cases like trinomials it probably wont work – Jebjosh Nov 20 '19 at 23:14