I'm trying to create a program that would be able to factor trinomials in the form
x² + bx + c
where b and c are integers, and outputs the factored form of the trinomial if it can be factored, or the statement, “This trinomial cannot be factored” if it can’t The final product is passed to be two separate programs
one A Python file that contains your package of functions. This file should not produce any output. That is, it should only contain functions, with no function calls, input statements or print statements.
And the other imports your first package and uses it to factor simple trinomials with input statements.
Right now I have just put them together in one program to edit it easier and plan on separating it later. The program works and gives me a seemingly random number of outputs and although they are right, I don't want it repeated 300 times.
Also I'm not sure how to make it give a factor as an output if there are not factors.
FactoringToolbox
def findFac(c): #set the count to start at -1 or 1
if c > 0:
count=int(1)
elif 0 > c:
count=int(-1)
repeat (c,count)
def repeat(c,count):
if c > 0:
#finds factors if c is positive
for i in range(count,c+1,1):
if c%i == 0:
num=c/i
testB(i,num,count)
elif 0 > c:
#finds factors is c is negitive
for i in range(count,c-1,-1):
if abs(c)%abs(i) == 0:
num=c/i
testB(i,num,count)
def testB(i,num,count): #checks if factors add to b
if i+num==b:
if i*num==c:
print ("(x+",i,")(x+",num,")")
a=1
elif i-num==b:
if i*(-1*num)==c:
print ("(x+",i,")(x-",num,")")
a=1
elif num-i==b:
if (-1*i)*num==c:
print("(x-",i,")(x+",num,")")
a=1
else:
if c > 0 :
count=count+1
else:
count=count-1
repeat(c,count)
2nd program
print ("Please enter varibles b and c for a quadratic expression in the form:")
print("x^2+bx+c")
b=int(input("Please enter a interger for 'b': "))
c=int(input("Please enter a interger for 'c': "))
findFac(c)
That is not how I would do it because of two reasons:
There is a better way to do it. Since we are dealing with only one variable second degree polynomials (ax2 + bx + c), we can find the factors directly by using the quadratic formula
(-b ± (b2 - 4ac)1/2)/2a
All we need to now check is whether b2 - 4ac > 0 for the polynomial to be factorizable.
The factorized polynomial will be of the form:
a[x - (-b+(b2 - 4ac)1/2)/2a][x - (-b-(b2 - 4ac)1/2)/2a]