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

I've fought the urge to post about ps1, but I've read everything over and over, and I'm still not quite getting it. I think I can handle the part of getting the count to 1000 and printing the variable for 1000th prime positive integer. My head's in an endless loop over how to write the program that tests for primality. Here's my most recent and closest attempt at understanding it: def isprime(n): if n%(range(n-1, 2)) == 0: print "NOT Prime" else: print "Congrats! That sucker's PRIME" (PS: Did I miss something, or does this website not offer the ability t

OpenStudy (anonymous):

I should add that I do understand the final code will likely utilize a while statement.

OpenStudy (anonymous):

I think your question was cut-off. I'm guessing you were going to say "website does not offer the ability to search." That's a complete guess, but if it is what you were saying, then the answer is "no, you are not missing something." You have search in google. For example, search "openstudy.com problem set 1 answer."

OpenStudy (anonymous):

pls use a code pasting site to post your code n%(range(n-1, 2)) doesn't look right - looks like you are trying to mod an integer with a list

OpenStudy (anonymous):

@morgk16, that was precisely my question. Thank you for the tip. @bwCA, that is the code exactly as it was originally used. I recognize that I have at least enough errors in my method of attack to make this problem more difficult than it should be. I am currently looking for an answer by nesting while and if statements to arrive at a desire conclusion that will print (count, n) as (1000, (the thousandth prime number)). In assuming that my problem lies in the lack of understanding of the mechanics behind loop statements, I read on.

OpenStudy (anonymous):

A hint about prime numbers. 3 is your friend. For loops follow this syntax for item in range(n): code or you can use a list. L = [] for item in L: code

OpenStudy (anonymous):

This is essentially how you are expected to find primes for this PS. Just combine it with a function like you were essentially doing and add that while loop and you'll be done. http://pastebin.com/LBmxf9D0

OpenStudy (anonymous):

Ok, I feel like I'm getting closer, but no cigar just yet. Any tips on what I can read that will enlighten me on my current errors? n = 4 p = 2 count = 2 while count < 1001: for p in range(2, n-1): if n%p == 0: n+=1 elif n%p != 0: n+=1 count+=1 print count, n

OpenStudy (anonymous):

Excellent. Thank you for the links!

OpenStudy (anonymous):

http://pastebin.com/sAGK9eWr at line 7; you find that n is not prime and go to the next n but the for loop has not finished and p will continue at the next p - so the new n won't get tested with some numbers. it is easier to see it - put print statements in your code like this http://pastebin.com/JfRVyFbg

OpenStudy (anonymous):

those online docs should be on your computer - F1 from Idle if using windows

OpenStudy (anonymous):

Ok, I'm one prime number off. Why? I eliminated tests for 0-3 counting 2 & 3 as the first prime numbers by starting the test with x at 4 and the count at 2. Where does my one extra occur? What am I missing? count = 2 x = 4 while count < 1001: for p in range(2, x): if x % p == 0: x += 1 break else: print count, x x += 1 count += 1

OpenStudy (anonymous):

I believe you are one off because you increase count after printing. i.e. You are starting at count "2" because you are accounting for the first two prime numbers (2, 3). However, when you find the third prime number (5), you print the current count (2), then increase it. Just place the count += 1 statement before the print count, x statement. http://codepad.org/EPmEBafu

OpenStudy (anonymous):

Also, when it's written this way you can stop when count = 1000 instead of 1001.

OpenStudy (anonymous):

1 is also a prime number. You need to start at 3.

OpenStudy (anonymous):

Nvm im wrong. 1 isnt prime.

OpenStudy (anonymous):

Oh i see what you did. You set count to 2. In computer integers, 0 is actually a place holder. When used as array indexers and loops. You need to remember a computer starts counting from 0. for (i = 0, i < 1000, i++) code Translates into i=0: code i=1: code i=2: code Another way to understand this, is when you start counting and use your fingers you say 1 and then put up 1 finger. However you need to say 0 and put up 1 finger. Thats why the formula to find the bounds of n number of bits is 2**n -1.

OpenStudy (anonymous):

While i < 1000 will execute code only up to the loop that will compare i = 999 < 1000. So by hard coding i = 2 while i < 1001. You have effectively broke the logic of you wanting to loop 1000 times because you are supposed to start subtracting from 1001 for each prime number in count.

OpenStudy (anonymous):

i = 0 < 1001 will loop until i = 1000. If you start at 2, you will have only executed 999 loops because the logic of while i <n+1 assumes you start counting from 0.

OpenStudy (anonymous):

Basically you need to make it i < 999 because you only need 998 more prime numbers when counting from 2.

OpenStudy (anonymous):

Also when you are calculating to the 1000th prime you shouldnt iterate count when the number isnt prime.

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!