Ask your own question, for FREE!
MIT 6.189 A Gentle Introduction to Programming Using Python (OCW) 11 Online
OpenStudy (anonymous):

MIT 6.189 Exercise 2.5

OpenStudy (anonymous):

I didn't have too much trouble getting the first part of 2.5 done (see how I did it: https://gist.github.com/TomDeBeauchamp/5864128). I'd like to go for the optional second half, though, but I realize, I don't really remember complex numbers. I've looked at a few web pages on using the quadratic formula with complex numbers, but nothing really great. Anyone have any suggestions for a good refresher tutorial on complex numbers, especially as regards the quadratic formula?

OpenStudy (e.mccormick):

http://www.purplemath.com/modules/complex.htm

OpenStudy (espex):

OH NOES, 404. We seem to have missed the gist of that gist you were looking for.

OpenStudy (e.mccormick):

Hmm? There is no 404. Just need to know how to read the link right: https://gist.github.com/TomDeBeauchamp/5864128 Always put a space after a link and before punctuation, or the punctuation will corrupt the auto link generator.

OpenStudy (espex):

http://pastebin.com/DVQVR7nW

OpenStudy (anonymous):

Awesome. Thank you both for your help!

OpenStudy (espex):

Since this question is closed, it is doubtful that anyone will find this code... Here is how I finally settled on handling the complex roots, there is an 'import unicodedata' at the top. Python 3.x if (x < 0): print("Complex root") x = -1*x if (math.sqrt(x)%1 == 0): r1 = -b/(2*a) + math.sqrt(x)/(2*a)*1j r2 = -b/(2*a) - math.sqrt(x)/(2*a)*1j print ("Root 1 =", r1) print ("Root 2 =", r2) else: # part1 + sqrt(part2)j/part3 part1 = -b/(2*a) part2 = x*1j part3 = 2*a print ("Root 1 = ", part1, " + ", unicodedata.lookup('square root')+"("+ str(part2) + ")/" + str(part3) ) print ("Root 2 = ", part1, " - ", unicodedata.lookup('square root')+"("+ str(part2) + ")/" + str(part3) )

OpenStudy (anonymous):

I'm a little new... Is there a way to re-open questions?

OpenStudy (espex):

Not that I am aware of. There is a "Closed Questions" tab that you can use to review those that have been finalized, but I think starting a new thread is your only option to revisit old topics.

OpenStudy (anonymous):

whoops... I'll keep that in mind going forward. btw, this bit was the life-saver: r1 = -b/(2*a) + math.sqrt(x)/(2*a)*1j r2 = -b/(2*a) - math.sqrt(x)/(2*a)*1j

OpenStudy (espex):

Glad to hear it.

OpenStudy (anonymous):

Not sure if there is a policy about adding to closed questions, especially ones 7 months old but here's what I did. I solved this by using import cmath at the top of the code which allows you to use cmath.sqrt for negative numbers: 'if discriminant < 0 complex1 = (-b + cmath.sqrt(discriminant)) / ( 2 * a) complex2 = (-b - cmath.sqrt(discriminant)) / ( 2 * a) return complex1, complex2 #testcase: print quadratic_root(1, 2, 3) results in ((-1+1.4142135623730951j), (-1-1.4142135623730951j))

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!