Friday, February 6, 2009

solve 2nd degree equations

This is will output second degree equations using the quadratic formula. It is perfect except for two things: first, it outputs without parentheses around the parts before the fraction, so you must remember that you are dividing everything on the top by the bottom, not just the last part. Second, it does not reduce the square root. For example, the square root of 40 can be reduced to 2 times the square root of ten. However, I couldn't think of an effective way to do that. Still, it gives the right answer, and that's what counts in the end:

to solve.second :a :b :c
while [(:b*:b-4*:a*:c)>0] [if [sqrt(:b*:b-4*:a*:c)= int (:b*:b-4*:a*:c)] [make "q sqrt(:b*:b-4*:a*:c) show (word "x "= "(-:b+:q)/(2*:a) " and " (-:b-:q)/(2*:a)) stop]]
show (word " ) show (list "the "answers "are) show (word  (-:b) "+ "sqrt "(:b*:b-4*:a*:c) "/ "(2*:a)) show (list "and) show (word ""(-:b) "- "sqrt "(:b*:b-4*:a*:c) "/ "(2*:a))
end

Solve 1st degree equations

Although this does the same thing as the solver before, it does it the direct way. Put your equation in the form ax+b=0 and insert a and b into the variables, and it spits out X:

to solve.first :a :b
show (word "x "= "(-:b/:a))  setx (-:b/:a)
end