-1

I'm trying to make a factoring program, but it doesn't seem to work with negative number a-, b- and c-inputs.

from fractions import gcd
factor = -1
opp = 0
number = 1
success = 0
a = int(input("a-value: "))
b = int(input("b-value: "))
c = int(input("c-value: "))
factors = []
d = 0
e = 0
while number <= abs(a*c):
#Checking for multiples
    if abs(a*c) % number == 0:
        factor += 1
        factors.append(number)
    number += 1
while (factor-opp) >= 0:
#Checking for actual factors
    d = int(factors[factor])
    e = int(factors[opp])
    if (abs(d+e) or abs(d-e)) == abs(b):
        success += 1
        break
    else:
        factor -= 1
        opp += 1

if success > 0:

    if (d+e) == b:
        e = e
    elif (d-e) == b:
        e -= 2*e
    elif (e-d) == b:
        d -= 2*d
    elif (-d-e) == b:
        d -= 2*d
        e -= 2*e
    #Figuring out the equation
    if d % a == 0:
        d /= a
        f = 1
    else:
        f = a/gcd(d,a)
        d /= gcd(d,a)

    if e % a == 0:
        e /= a
        g = 1
    else:
        g = a/gcd(e,a)
        e /= gcd(e,a)
    #Displaying the answer
    if d >= 0:
        d = str("+" + str(int(d)))
        if e >= 0:
            e = str("+" + str(int(e)))
        elif e < 0:
            e = str(int(e))
    else:
        d = str(int(d))
        if e >= 0:
            e = str("+" + str(int(e)))
        elif e < 0:
            e = str(int(e))
    if f == 1:
        if g == 1:
            print ("(x" + d + ")(x" + e + ")")
        else:
            g = str(int(g))
            print ("(x" + d + ")(" + g + "x" + e + ")")
    elif g == 1:
        f = str(int(f))
        print ("(" + f + "x" + d + ")(x" + e + ")")
    else:
        f = str(int(f))
        g = str(int(g))
        print ("(" + f + "x" + d + ")(" + g + "x" + e + ")")
else:
    print("This equation cannot be factored into integers.")

More specifically, the problem is somewhere within this block, I think. I've tested it out with print statements:

while (factor-opp) >= 0:
#Checking for actual factors
    d = int(factors[factor])
    e = int(factors[opp])
    if (abs(d+e) or abs(d-e)) == abs(b):
        success += 1
        break
    else:
        factor -= 1
        opp += 1

I've searched everywhere: my programming textbook, online searches about inputting negatives, everything. What am I doing wrong here?

richzli
  • 131
  • 8

1 Answers1

1

Ok I am able to reproduce your issue for a simple testcase like - a=1 , b=0, c=-4 .

The issue is in the line -

if (abs(d+e) or abs(d-e)) == abs(b):

This does not check whether abs(b) is equal to abs(d+e) or abs(d-e) , instead it first evaluates the result of (abs(d+e) or abs(d-e)) , which would return the first non-zero result , and then compare that against abs(b) , so for negative numbers this does not evaluate the result correctly. Change that condition to -

if abs(d+e) == abs(b) or abs(d-e) == abs(b):

or you can also use a set -

if abs(b) in {abs(d+e), abs(d-e)}:           #Though I doubt if using set would give any performance improvement because of the overhead of creating a set.

Demo after changes -

a-value: 1
b-value: 0
c-value: -4
(x+2)(x-2)

a-value: 1
b-value: -1
c-value: -6
(x-3)(x+2)

One more thing, there is something you have not considered , when a=-1 , b=-4 , c=-4 , the result should come to -(x+2)(x+2) , but the current program results in (x+2)(x+2) .

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • Thanks! I just expected that they would factor out the GCF of the coefficients. a=-1, b=-4, c=-4 have a common multiple of -1 so the program outputs the same as a=1, b=4, c=4. – richzli Sep 23 '15 at 12:34