Blog 2: Factoring the Lazy Way

 This week I have decided to try and create solvers for simple polynomial factoring in algebra. This includes solving for roots of trinomials using the Quadratic Equation, and expanding factored form into standard form. I will try to incorporate the formulas that will be taught, as well as allow for the possibility for students to expand on my simple code to help them solve more complex problems.

Quadratic Equation

I couldn't find any resources to show me their background code, mostly because I think this process is fairly simple, so instead I'm trying it completely blind. Hopefully it turned out well! In reference to other resources, I found lots of websites that would do the solution for me, but I couldn't access their mechanism. 

So after some tinkering, I couldn't get the proper form to work because the code wasn't recognizing my variables, but I was able to make the "lazy way" work. Here it is:

# For a trinomial of the form ax^2 + bx + c = 0

> Quad.Form = function(a,b,c){

+     Root1 = ((-b + sqrt((b*b)-4*a*c))/2*a)

+     Root2 = ((-b - sqrt((b*b)-4*a*c))/2*a)

+     print(Root1,Root2)}

With the result after typing in the function with its three required variables,

> Quad.Form(1,5,6)

[1] -2

[1] -3

Meaning x = -2 and x = -3, giving the factored form of the function as (x+2)(x+3).

This is definitely something I'm going to improve on my next GH time. 

Expanding Factored Form

Once I understood the format of the Quadratic Equation function, adjusting it for Expansion was relatively easy. I again chose the "lazy" way to do it, with inputs simply printing out the correct variables.

# For a trinomial of the form (ax +b)(cx + d) = 0

> Expand.Tri = function(a,b,c,d){

+     print(a*c)

+     print(a*d + b*c)

+     print(b*d)# For a trinomial factored in the form (ax + b)(cx + d)

With the result after trying in the function with its four required variables,

> Expand.Tri(1,2,1,3)

[1] 1

[1] 5

[1] 6

Something I've learned is that these functions require explanations in # form in order to give the function context. 

I want to get better at writing functions, which would make these problems more robust. 

Comments