Hey guys,
This is my first time on the forum. I am trying to code a program that factors a polynomial (up to quartic). I am using synthetic division to find factors for the polynomial. Its working pretty well, but is limited. I only have a basic understanding of programming on the nspire, so the code is very lengthy. Could anybody help me compact my code/suggest another way to factor polynomials? Also, the program has a couple bugs right now and won't tell me the correct factorization sometimes, so if anybody finds the error, please let me know.
Define syntheticdiv()=
Prgm
:Request "Power of polynomial (2-4)?",e
:If e=2 Then
: Text "Enter Quadratic as Ax^2+Bx+C"
: Request "A(x^2)=",d
: Request "B(x)=",h
: Request "C=",i
: Lbl checkquad
: −20→a
: While a≤20
: a+((1)/(12))→a
: If i+a*(d*a+h)=0 Then
: quadfactor:=−a
: Goto e2finish
:ElseIf a=20 Then
:quadfactor:=10000
:Goto e2finish
:EndIf
:EndWhile
:ElseIf e=3 Then
:Text "Input the Cubic Polynomial as Ax^3+Bx^2+Cx+D"
:Request "A(x^3)=",b
:Request "B(x^2)=",c
:Request "C(x)=",l
:Request "D=",m
:Lbl checkcube
: −20→o
: While o≤20
: o+((1)/(12))→o
: p:=((b*o+c)*o+l)*o+m
: If p=0 Then
: cubicfactor:=−o
: d:=b
: h:=b*o+c
: i:=(b*o+c)*o+l
: Goto checkquad
: ElseIf o=20 Then
: cubicfactor:=10000
: Goto e3finish
:EndIf
:EndWhile
:ElseIf e=4 Then
:Text "Input Quartic Polynomial as Ax^4+Bx^3+Cx^2+Dx+E"
:Request "Ax^4",r
:Request "Bx^3",s
:Request "Cx^2",t
:Request "Dx",u
:Request "E",v
: −20→w
: While w≤20
: w+((1)/(12))→w
:q:=(((r*w+s)*w+t)*w+u)*w+v
: If q=0 Then
: quarticfactor:=−w
: b:=r
: c:=r*w+s
: l:=t+w*(r*w+s)
: m:=u+w*(t+w*(r*w+s))
: Goto checkcube
: ElseIf w=20 Then
: quarticfactor:=10000
: Goto e4finish
:EndIf
:EndWhile
:EndIf
:Lbl e4finish
:If quarticfactor=10000 Then
:Disp "There are no factors"
:Goto finish
:EndIf
:Lbl e3finish
:If cubicfactor=10000 Then
:If e=4 Then
:quarticnum:=getNum(quarticfactor)
:quarticdenom:=getDenom(quarticfactor)
:Disp quarticdenom,"*(",quarticdenom,"x+",quarticnum,")(",b,"x^3+",c,"x^2+",l,"x+",m,")"
:Goto finish
:Else
:Disp "There are no factors"
:Goto finish
:EndIf
:EndIf
:Lbl e2finish
:If quadfactor=10000 Then
:If e=4 Then
:quarticnum:=getNum(quarticfactor)
:quarticdenom:=getDenom(quarticfactor)
:cubicdenom:=getDenom(cubicfactor)
:cubicnum:=getNum(cubicfactor)
:quaddenom:=getDenom(quadfactor)
:quadnum:=getNum(quadfactor)
:coefficient:=((1)/(quarticdenom*cubicdenom*quaddenom))
:Disp "(",quarticdenom,"x+",quarticnum,")(",cubicdenom,"x+",cubicnum,")(",d,"x^2",h,"x",i,")"
:Goto finish
:ElseIf e=3 Then
:cubicdenom:=getDenom(cubicfactor)
:cubicnum:=getNum(cubicfactor)
:quaddenom:=getDenom(quadfactor)
:quadnum:=getNum(quadfactor)
:coefficient:=((1)/(cubicdenom*quaddenom))
:Disp coefficient,"*(",cubicdenom,"x+",cubicnum,")(",d,"x^2+",h,"x+",i,")"
:Goto finish
:ElseIf e=2 Then
:Disp "There are no factors"
:Goto finish
:EndIf
:EndIf
:If e=4 Then
:quarticnum:=getNum(quarticfactor)
:quarticdenom:=getDenom(quarticfactor)
:cubicdenom:=getDenom(cubicfactor)
:cubicnum:=getNum(cubicfactor)
:quaddenom:=getDenom(quadfactor)
:quadnum:=getNum(quadfactor)
:coefficient:=((1)/(d))
:Disp coefficient,"*(",quarticdenom,"x+",quarticnum,")(",cubicdenom,"x+",cubicnum,")(",quaddenom,"x+",quadnum,")(",d,"x+",a*d+h,")"
:ElseIf e=3 Then
:cubicdenom:=getDenom(cubicfactor)
:cubicnum:=getNum(cubicfactor)
:quaddenom:=getDenom(quadfactor)
:quadnum:=getNum(quadfactor)
:coefficient:=((1)/(d))
:Disp coefficient,"*(",cubicdenom,"x+",cubicnum,")(",quaddenom,"x+",quadnum,")(",d,"x+",a*d+h,")"
:ElseIf e=2 Then
:quaddenom:=getDenom(quadfactor)
:quadnum:=getNum(quadfactor)
:coefficient:=((1)/(d))
:Disp coefficient,"*(",quaddenom,"x+",quadnum,")(",d,"x+",a*d+h,")"
:EndIf
:Lbl finish
:DelVar cubicdenom,cubicnum,quaddenom,quadnum,quarticnum,quarticdenom,a,b,c,d,e,f,g,h,i,l,m,o,p,q,r,s,t,u,v,w,quarticfactor,cubicfactor,quadfactor
:EndPrgm