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

4 comments:

Buge said...

Hey this is Bugefun. I just happen to be taking this little class thing that uses LOGO but in a different program. I thought I would try out this and it has most of the programming the same. I found this one thing that I like: sound [100 300]
That makes a sound come out of your computer (not your speakers) with the frequency of 100 and the lenghth of 300 milliseconds. I wrote a little song using a site that says all the frequencies of all music notes.

Here is the code:
to song
sound [262 283]
wait 1
sound [262 83]
wait 1
sound [294 383]
wait 1
sound [262 383]
wait 1
sound [349 383]
wait 1
sound [330 783]
wait 1
sound [262 283]
wait 1
sound [262 83]
wait 1
sound [294 383]
wait 1
sound [262 383]
wait 1
sound [392 383]
wait 1
sound [349 783]
wait 1
sound [262 283]
wait 1
sound [262 83]
wait 1
sound [523 383]
wait 1
sound [440 383]
wait 1
sound [349 383]
wait 1
sound [330 383]
wait 1
sound [294 583]
wait 1
sound [466 283]
wait 1
sound [466 83]
wait 1
sound [440 383]
wait 1
sound [349 383]
wait 1
sound [392 383]
wait 1
sound [349 1183]
wait 1
end

You should be able to recognize it.

Vanderdecken12 said...

It's Happy Birthday! Very cool, Bugefun.

Buge said...

Thanks, I made a few others that aren't as cool.

to random2
repeat 200 [
sound list random 1000 random 1000
wait random 30
]
end

to timebomb
make "time 60
repeat 60 [
sound [300 50]
wait :time
make "time (:time - 1)
]
sound [1000 4000]
end

DanielAjoy said...

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.

This code seems to do the trick:


to simp.root :n
make "i 2
make "last.factor "X
make "outside 1
make "inside 1

while [not :i > :n] [
ifelse 0 = remainder :n :i [
make "factor :i
make "n :n / :factor

ifelse :factor = :last.factor [
make "outside :outside * :factor
make "factor "X
] [
if not :last.factor = "X [make "inside :inside * :last.factor]
]
make "last.factor :factor

] [
make "i :i + 1
]

]

if not :last.factor = "X [make "inside :inside * :last.factor]

make "inside :inside * :n
if :inside = 1 [output :outside]

output (sentence :outside [* sqrt] :inside)
end

It works like this:

show simp.root 40
[2 * sqrt 10]