Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 7 Online
OpenStudy (anonymous):

I am at my whit's end. Can somebody tell me why this doesn't give me the right output? http://codepad.org/BS50p09k

OpenStudy (anonymous):

There are a couple of subtleties there. One, when you truncate the square root, you are actually off by one on average (it rounds the number to the floor, if I recall correctly). It should be sqrt(n) + 1. Other points: you should print n - 2, rather than n, since you will be off by 2, since it executes the while loop on the last, and it add two to n, so it will print 7921, rather than 7919. You don't really need to start at prime_counter = 3, the only special prime is 2, that is an even number. You should be able to do it with prime_counter = 1 and n = 3. Good luck and don't give up :-)

OpenStudy (anonymous):

http://codepad.org/QOB01NzF I made the changes that you suggested, and the output was still too high. You don't notice anything else that might be amis, do you?

OpenStudy (anonymous):

Yes, I already mentioned it. The first thing I mentioned is what is wrong still :-)

OpenStudy (anonymous):

Oh no!

OpenStudy (anonymous):

Brilliant! That works! http://codepad.org/JjRhF8kO Thank you so much for the help! I really feel as though I have accomplished something!

OpenStudy (anonymous):

Just so you know, the second assignment took me less than ten minutes. Here is the code: http://codepad.org/SwPS6F7C This stuff is way more interesting than people give it credit for.

OpenStudy (anonymous):

The notes on it are wrong, since I just used the last code and changed it a bit.

OpenStudy (anonymous):

I think that now a kudos is in place :-). CS is, together with Physics and Maths for me, the most interesting subject, haha. Anyway, here is what is left of my implementation: http://codepad.org/6pkmUt1o I do think there was another file that I used a different approach, but I can't find it. As a final note, avoid using from math import *, or for that matter, any kind of import *. The reasoning is that, what if you import two modules with sqrt defined in there, but the implementation is different? It creates a namespace pollution, something that you shouldn't care about right now, but just so you keep in mind to avoid doing.

OpenStudy (anonymous):

So, you are giving me what the class refers to as *style* pointers? I appreciate that. I feel as though style is the wrong word for what they are trying to enforce. I feel like discipline would be better, since, even though they are just suggestions, these minor mistakes can glom to nullify a code. I studied Econ and Finance, and while I find finance to be tedious, I like the mathematical expressions and logical progressions to be fairly organic. Feelsgoodman.

OpenStudy (anonymous):

More or less. If you prefer, as Prof. Grimson (if my memory does not fail me) calls 'programming hygiene', works fine. And indeed, as you can see from working on your pset (the +1 at the end of the for loop), a minor mistake can crash the entire solution. When you start working on bigger projects and start to use your own modules, you will soon find out that namespace pollution is quite hard to see, if you don't keep the discipline of importing everything in a single way. Here is something more in-depth: http://stackoverflow.com/questions/4313124/python-is-there-a-disadvantage-to-from-package-import-besides-namespace-colli

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!