Hello currently I have started a project for my school. The project is a program that can

factor a trinomial. So far I have it set up to take the users inputs. then it

multiplies two of the inputs to get a new number. My problem is that i need it to factor

that new number and list it out. Once I have an idea of how to do that I know how I want

to go about the rest.

Example of how it should factor the trinomail:

Trinomial- 5x^2+16x+12

Multiplication- 5*12

New number- 60

Factors [1,2,3,4,5,6,10,12,15,20,30,60]

The factors that equal the middle number when added- [6,10]

then Divide the first number by those factors- 5/6, 5/10

then print out answer- (5x+6)(x-2)

Okay, I will help you out.
First off, have you heard of the rational root theorem? If you have just use that if not here is an explanation.

Given an arbitrary polynomial, the possible roots of that polynomial are +/- p/q where p is an integer factor of the constant term and q is a factor of the leading term.

Here is how the code would look:

``````from math import ceil
listOfFactors = lambda n: {i for i in range(1,ceil(abs(n)/2)+1) if n%i == 0}
multiply = lambda a,b: a*b
def polyRoots(polyListCoeff):
allFactors = set()
allFactorsList = list(allFactors.union(listOfFactors(polyListCoeff),{polyListCoeff},listOfFactors(polyListCoeff[-1]),{polyListCoeff[-1]}))
allFactorsList.extend([-1*i for i in allFactorsList])
polyListCoeff.reverse()
return [i for i in allFactorsList if sum([pow(i,j)*polyListCoeff[j] for j in range(0,len(polyListCoeff))]) == 0]``````

Here is an example for x^2 + 0x -4:

``````>>> polyRoots([1,0,-4])
[2, -2]``````

The parameters for polyRoots is a list of the coefficients starting from the leading one.

Now take the roots that you found from polyRoots and make a nice display for the factoring. The factored form the example is (x-2)(x+2)

Thanks

Here is the full code for the factoring based on the work from the answer above:

``````from math import ceil
listOfFactors = lambda n: {i for i in range(1,ceil(abs(n)/2)+1) if n%i == 0}
multiply = lambda a,b: a*b
def polyRoots(polyListCoeff):
allFactors = set()
allFactorsList = list(allFactors.union(listOfFactors(polyListCoeff),{polyListCoeff},listOfFactors(polyListCoeff[-1]),{polyListCoeff[-1]}))
allFactorsList.extend([-1*i for i in allFactorsList])
polyListCoeff.reverse()
roots = [i for i in allFactorsList if sum([pow(i,j)*polyListCoeff[j] for j in range(0,len(polyListCoeff))]) == 0]
factorList = list()
for i in roots:
if i<0:
factorList.append("(x+{})".format(-i))
else:
factorList.append("(x-{})".format(i))
return "".join(factorList)``````

Here is an example:

``````>>> polyRoots([1,0,-4])
'(x+2)(x-2)'``````

This code only solves for integer factorization. I have just noticed the problem and will fix it momentarily

Here is the edited version of the code that will give you decimal roots but only the rational decimal roots because the frame work of the algorithm resides in the Rational Root Theorem.

``````from math import ceil
listOfFactors = lambda n: {i for i in range(1,ceil(abs(n)/2)+1) if n%i == 0}
def removeDuplicates(mylist):
if mylist:
mylist.sort()
last = mylist[-1]
for i in range(len(mylist)-2, -1, -1):
if last == mylist[i]:
del mylist[i]
else:
last = mylist[i]
return mylist

def polyRoots(polyListCoeff):
allFactors = set()
allFactorsListOld = list(allFactors.union(listOfFactors(polyListCoeff),{polyListCoeff},listOfFactors(polyListCoeff[-1]),{polyListCoeff[-1]}))
allFactorsListOld.extend([-1*i for i in allFactorsListOld])
allFactorsList = list()
for k in allFactorsListOld:
for j in allFactorsListOld:
allFactorsList.append(k/j)
allFactorsList = removeDuplicates(allFactorsList)
polyListCoeff.reverse()
roots = [i for i in allFactorsList if sum([pow(i,j)*polyListCoeff[j] for j in range(0,len(polyListCoeff))]) == 0]
factorList = list()
for i in roots:
if i<0:
factorList.append("(x+{})".format(-i))
else:
factorList.append("(x-{})".format(i))
return "".join(factorList)``````

Thank you that worked perfectly

I am glad it worked. I am sorry about all of the posts, I just kept making mistakes and it wouldnt let me delete the old ones. Now the final function that I posted only works for equations that can indeed by factored. This means that they have rational roots.

I am glad that I could help!