This is a minor issue that I'm having in a larger program. The program is for factoring a second degree polynomial. The function below is supposed to find the factors, which is does, but let's say a=6, b=1, c=-12 so a*c=-72 and I want the two numbers that will add to 1. Instead of 9 and -8, this code gives me -9 and 8. I can't figure out why. If anyone could help me with this I would really appreciate it.

void findFactors(int a, int b, int c, int&f1, int&f2)
{
    int f;
    int i=0;
    f=-int(sqrt(abs(a*c)));
    while(f<=int(sqrt(abs(a*c))))
    {
        if(f==0)
            f++;
        if((a*c)%f==0)
        {
            f1=(a*c)/f, f2=f;
            i++;
        }

        f++;
    }

    if(f!=-int(sqrt(abs(a*c))))
    {
        f1==0, f2==0;
    }
}

Dani AI

Generated

Quick diagnosis and a safe fix.

The symptom you showed (first finding 9 and -8, then being overwritten by -9 and 8) is caused by two things: the loop keeps scanning every divisor pair and keeps updating f1/f2 instead of stopping when the matching pair is found, and the fallback at the end uses the comparison operator (==) instead of assignment so it never resets the outputs. correctly suggested an early exit and pointed out the == mistake. Below is a clearer, more robust approach that (1) only iterates positive divisors (faster), (2) checks the signed complement pairings, (3) uses 64-bit arithmetic to avoid overflow, and (4) returns success/failure explicitly.

// returns true and sets f1,f2 when two integers exist with f1*f2 == a*c and f1+f2 == b
bool findFactorsInt(int a, int b, int c, int &f1, int &f2) {
    using ll = long long;
    ll prod = static_cast<ll>(a) * static_cast<ll>(c);
    if (prod == 0) { f1 = 0; f2 = b; return true; } // handle ac == 0 quickly
    ll absprod = std::llabs(prod);
    ll limit = static_cast<ll>(std::sqrt((long double)absprod));
    for (ll d = 1; d <= limit; ++d) {
        if (absprod % d != 0) continue;
        ll e = prod / d;                // signed complement, d > 0
        if (d + e == b) { f1 = static_cast<int>(e); f2 = static_cast<int>(d); return true; }
        if (-d - e == b) { f1 = static_cast<int>(-e); f2 = static_cast<int>(-d); return true; }
    }
    f1 = f2 = 0;
    return false;
}

Troubleshooting tips

  • Use = for assignment. f1==0 is a comparison and does nothing.
  • Return immediately when you find the matching pair instead of letting later iterations overwrite it.
  • Use long long for a*c if a and c can be large.
  • After you get m and n (the two numbers), split the middle term (ax^2 + mx + nx + c) and factor by grouping to get the final binomials.

This keeps the search small and predictable and avoids the “wrap around” effect you observed.

Its wraping around.

$ ./a.out
i(0) f1(9) f2(-8)    <------ Should have stopped here
i(1) f1(12) f2(-6)
i(2) f1(18) f2(-4)
i(3) f1(24) f2(-3)
i(4) f1(36) f2(-2)
i(5) f1(72) f2(-1)
i(6) f1(-72) f2(1)
i(7) f1(-36) f2(2)
i(8) f1(-24) f2(3)
i(9) f1(-18) f2(4)
i(10) f1(-12) f2(6)
i(11) f1(-9) f2(8)  <------ When all of the way to here
-9 8
$
Try adding a break if f1+f2 equal b. I tested it a little and looks like it works
$ ./a.out
i(0) f1(9) f2(-8)
9 -8
$
Code
void findFactors(int a, int b, int c, int&f1, int&f2)
{
    int f;
    int i=0;
    f=-int(sqrt(abs(a*c)));
    while(f<=int(sqrt(abs(a*c))))
    {
        if(f==0)
            f++;
        if((a*c)%f==0)
        {
            f1=(a*c)/f, f2=f;
            //-----------------------------------
            // Debug
            //cout << "i(" << i << ") f1(" << f1 << ") f2(" <<f2 << ")" << endl;
            //-----------------------------------
            // Break if all is well
            if ( f1+f2 == b) {
               break;
            }
            i++;
        }

        f++;
    }
    //--------------------------------------
    // Was't sure what this is for?
    if(f!=-int(sqrt(abs(a*c))))
    {
        //----------------------------------
        // Why are you using '==' here? 
        // The values of f1 and f2 will be
        // unchanged.
        f1==0, f2==0;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.