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

I keep entering the following code into the IDLE: http://pastebin.com/nzG9Nva7 by copy and pasting, and it returns nothing. Is there a special way to compile your code? BTW: this is problem set 1 question 1, computing the 1000th prime

OpenStudy (anonymous):

Oh, and if you care to comment on my code, that would be cool too.

OpenStudy (anonymous):

save your code in a new window as a .py file then click run tab and run module. after that call your code from idle.

OpenStudy (anonymous):

I was wondering, does your code find primes or even and odd? you have to use more divisors than just 2 to determine if it is prime.

OpenStudy (anonymous):

I think the biggest issue with this code is some confusion between the variables prime_counter and prime_candidate. Can you explain what you intend for these two pieces of data to represent?

OpenStudy (anonymous):

@jpkita yes, my code is designed to find primes. however, now that I'm using your advice and running it, it only returns 3, 5, and then doesn't return anything. If you look at this peice of code: if prime_counter%prime_divisor>0: ###checking to see if there is a remainder prime_divisor=prime_divisor+1 you see that I add 1 to the prime divisor each time their is a remainder found

OpenStudy (anonymous):

@polpak prime_counter is just keeping track of which prime number I'm on. Prime _candidate is the number being checked for prime-ness. There is currently an issue with my code, and I'm addressing it.

OpenStudy (anonymous):

I have now adjusted my code, but I still get stuck in some sort of infinite loop...

OpenStudy (anonymous):

well - version two seems to have some confusion with prime_counter and prime_candidate as mentioned earlier - what are you using those variables for? Maybe pick a place in the code and print their values to see what they are doing.

OpenStudy (carlsmith):

Here are two example scripts, they are full of explanatory comments and you don't need to download them, they open in a new tab. The first finds whether a number is prime or not.

OpenStudy (carlsmith):

The second uses the logic from the above script to create a little function, then uses that to find the 1000th prime.

OpenStudy (carlsmith):

Let me know if you're still stuck, I'd be happy to help.

OpenStudy (anonymous):

Hi carl, thanks for the help. I kind of wanted to get these answers without blatantly looking at someone else's code, but thanks for the help. I was able to create a script to determine whether a question is prime or not, but could not accurately determine the 1000th prime (I was off by two). Here's my code: (V3.0) http://pastebin.com/nzG9Nva7 Oh, and I created a new post because this one is a bit bogged down.

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!