Saturday, January 24, 2009

Simplified First-degree equation solver

In it's simplest form, a first degree equation looks like this: a*x+b=zero If you want to know what x equals, you would simply solve it. However, that would be impossible for more difficult equations, like 5th degree. That is what I'm working on now. Until then, however, this what I've gotten so far, only first degree. Notice, to get an answer with a possiblity as high as ten million, and accurate to the ten-thousandths place, it would take the computer practically forever, assuming it subtracted one ten-thousandth at a time. However, by making X "swing" back and forth around the number, it manages to zone in on it much faster. X goes down by a large amount until it passes the number, and then it goes up by a slightly smaller amount, and then back down again, until it finally hit the number. If, after getting to negative ten million, it couldn't find a number, it spits out "no solution". of course, unless X is too big, or too small, there will be a solution for every first degree equation. I only added that because I was thinking of the later equations, in which certain equations will have no solution.

to firstdegree :a :b
make "x 100000000000 
Until [:x=-10000000000] [while [:a*:x+:b>0] [make "x :x-100000000] while [:a*:x+:b<0]>0] [make "x :x-1000000] while [:a*:x+:b<0]>0] [make "x :x-10000] while [:a*:x+:b<0]>0] [make "x :x-100] while [:a*:x+:b<0]>0] [make "x :x-1] while [:a*:x+:b<0]>

end

No comments: