Having trouble with PS1A can anyone offer up some assistance? I dont know how to generate all odd numbers first, and then I am not sure how to iterate through the problem
You can generate odds by starting with an odd an adding 2. The simplest way to test for a prime, n, is to loop through all odd numbers, x, less than n and see whether n % x == 0. If any of those mods do equal 0, it means the prime candidate is actually composite. If you get to the end of the list without finding a factor, you have a prime number. So, we have this pseudocode: set first prime to 2 set prime count to 1 set prime candidate to 3 while prime count is less than 1000, loop set prime to True set divisor to 3 while prime is true and divisor is less than the prime candidate, loop if prime candidate % divisor equals 0 set prime to False because the prime candidate is composite increase the divisor by 2 if prime is True increase the prime count by 1 increase the prime candidate by 2 print out your 1000th prime You can even build on this simple example using the fact that it is only necessary to use primes less than or equal to the square root of the candidate as potential divisors. For example, if I was checking the number 121 to see if it was prime, I only need to see if it is divisible by primes less than or equal to 11 (i.e. 2, 3, 5, 7, 11). So, you could make a collection of primes as you go and use those instead of all odd numbers to save a great deal of time on larger numbers.
did you read the 'readings' for lectures 1-3? there is some good stuff there. and here are some links to the python docs: http://docs.python.org/tutorial/controlflow.html#for-statements http://docs.python.org/tutorial/controlflow.html#the-range-function http://docs.python.org/library/functions.html#range http://docs.python.org/reference/compound_stmts.html#compound-statements
I found that it was easier for me to write the scripts for the first 2 assignments after I read all the material and watched the first 4 or 5 lectures. The readings have little exercises in them which are excellent tools to help you learn the basics. I guess it's possible to finish ps1 after only hearing the first 2 lectures, but not for me.
are those python docs from the class or just good to know in general?
those are links to the online python documentation. they should also be installed on your computer. I use windows and i find the help-file version of the docs that are on my computer easier to use. if you intend on becoming proficient in Python then the docs are a must - and they will definitely help with the assignments given in this class.
Hi bwCA, Where can I find the "readings for lectures" you referenced? I am checking out the links you posted but would also like to review the readings, thanks!
http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00-introduction-to-computer-science-and-programming-fall-2008/readings/ It's on the left side of the course main page.
Join our real-time social learning platform and learn together with your friends!