Sunday, January 25, 2009

solve second degree equations

In their simplest form, second degree equations look like this: a*x^2+b*x+c.

This code will solve a formula starting in that solution. Unfortunately, however, it will only solve equations which have whole numbers as solutions. If there are 2 solutions (possible with a second degree equation), it will display both of them. It solves the first solution by narrowing in ot it from above. As it gets closer to the solution, the amount subtracted from X decreases, so that, when it gets to the number, it stops completely. It starts on the second number from the first number, and slowly goes up until it hits it. If there is no solution, you will not get an answer for a very long time. When you do, it will say "no solution".


to seconddegree :a :b :c
make "x1 100000000000 make "done 1

while [:a*:x1*:x1+:b*:x1+:c>100000000] [make "x1 :x1-100000000 IF :x1>1000000000000 [show (list "no "solution)] while [:a*:x1*:x1+:b*:x1+:c>10000000]  [make "x1 :x1-10000000] while [:a*:x1*:x1+:b*:x1>1000000] [make "x1 :x1-1000000] while [:a*:x1*:x1+:b*:x1+:c>100000] [make "x1 :x1-100000] while [:a*:x1*:x1+:b*:x1+:c>10000] [make "x1 :x1-10000] while [:a*:x1*:x1+:b*:x1+:c>1000] [make "x1 :x1-1000] while [:a*:x1*:x1+:b*:x1>100] [make "x1 :x1-100] while [:a*:x1*:x1+:b*:x1+:c>10] [make "x1 :x1-10] while [:a*:x1*:x1+:b*:x1>1] [make "x1 :x1-1] while [:a*:x1*:x1+:b*:x1+:c>0] [make "x1 :x1-0.001]

make "x2 :x1-.01 until [:a*:x2*:x2+:b*:x2+:c>0] [make "x2 :x2-.01 IF :x2<-1000000000000 [show (list "the "answer "is :x1) STOP]]]

while [:x1>:x2] [show (list "the "answers "are :x1 "and :x2) Stop] show (list "no "solution)


end

No comments: