Alright, is this http://codepad.org/Z4DeI5Xq is my attempt at assignment #1. Would someone mind giving me a critique?
You should only check up to the square root of N, plus you are checking pair numbers other than 2 for primality. You don't need to do it. Other minor aspects: xrange() is slightly more efficient than range, since range is actually generating a list, while xrange() takes a lazy approach, giving a number at a time. Also you should comment your code, at least a header in the file, like # #Name: Foo #Authour: Bar #Date: some_day #Objectives of the program: # This way you will remember a couple of months from now what your code is supposed to do, rather than having to struggle reading your own code (This is more important for bigger programs, but still, good programming discipline is a good thing to exercise).
Thank you for your input. I remember them talking about the commenting in the second lecture. I use very descriptive filenames to help me with this, but I will fix my code and put comments in before I post my finished code in this thread. Once again, than you for your time.
When you say "pair numbers," do you mean even numbers?
Oh yeah, slightly typo there and at Authour, instead of Author, :-) Anyway, kudos for writing a working solution, because that's the most important aspect of the problem set.
Perhaps, but I have yet to achieve the solution. My program creates output that is *even*. Which is obviously wrong. Also, I don't deserve that praise, as I went through and found things that worked and adapted them to my needs. At any rate, what would the best way of avoiding testing even numbers be? I was thinking something along the lines of starting at three and adding 2 every time I iterate.
You can create a list that starts with a 2 in it, then some variable like numToTest = 3, and keep adding 2, like you thought. Then, you can print listOfPrimes[-1] for the 1000th prime, or just print every element in the list. This approach is far from optimal, (appending to a list is somewhat expensive, and some other minor details) but it was my first implementation of this problem, and I think it's very much straightfoward to code it, in the sense that your code won't have any sort of smart but obtuse code, nor some harder number theory concepts.
Thank you for your time. It has been vary valuable.
*very
As for your question in the chat, DeprecationWarning refers to the fact that you are likely using an older version of Python (prior to 2.7 or 3.x), and the function that you called now accepts an int, instead of a float in the newer versions of Python, so your code might not be compatible with the newer version. Here's how to handle it: http://stackoverflow.com/questions/879173/how-to-ignore-deprecation-warnings-in-python
I am getting closer, but now my output is really low... http://codepad.org/fpAsnhTf
http://codepad.org/ypGDSqhE My program doesn't like the sqrt in (2, (numberbeingtested**.5))
Yeah, I noticed that. You might want to change the name of the variable to n, or something more simple. If you prefer, maybe adding capital letters in the name. Thing is, big variable names are very confusing, and it's easy to put parenthesis in the wrong place. I changed the name to n, and fixed some other parts of your code, now it's working fine here. Other than that, if isprime = False, then you should break the loop altogether. It doesn't really matter anymore, since you already found that it is not prime.
Join our real-time social learning platform and learn together with your friends!