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

Hi guys, I am just starting and am working on ps.1 question 1. could someone help me find the problem in my code? http://codepad.org/LbpWWmf5

OpenStudy (anonymous):

I have a feeling the reason it isn't working is becuase when I have the prime % divisors ==0 python is looking to see if all the answers are 0 when all i need is one 0 for it to be not prime. is there anyway to specify what i want?

OpenStudy (anonymous):

Two main things pop up from your code. 1) Don't over think it at the first attempt. Look at this line: for divisors in range (2, (prime +3) / 2):. You are trying to improve your code, which is good, but you don't have a working solution to return if you mess your code up. Just try to implement a very naive working solution first, then improve it. I am not saying that your math is wrong, just saying as a general advice. 2) Check your if-else statement block. It's incrementing prime_count for every divisor tested in the for-loop. That's not what you wanted, right? Look at this slightly modified version and see the output for your program. It may give you some intuition on what is wrong. Kudos for your work so far, you are on the right track. Work on it, and you will have it finished in no time :-)

OpenStudy (anonymous):

Sorry, forgot the link: http://codepad.org/SmnxAuk4

OpenStudy (anonymous):

Thanks for the advice, I could see that the divisors weren't resetting. I didn't know how to correct that, so i tried to take a different approach but I'm getting confused with all my conditionals. I got an answer but its not correct because I looked up the 1000th prime. heres my new code. http://codepad.org/fgPuxZm9 please some advice anyone?

OpenStudy (anonymous):

i think i like d the first one better, i like using a for loop for 'testing with all the divisors it looks to me that both of your tries have a common problem. you have to test a number (n) with ALL the divisors (between 2 and n-1) before you can say that is is prime. in both your attempts, you say that a number is prime the very first time that it isn't divisible by a divisor, (line seven in the first one and line ten in the second) you need to move that 'outside' the 'divisor/test' loop.

OpenStudy (anonymous):

i'm only looking at my second because i deleted my first. In line ten i am trying to increase the divisor by one so that it will test the next higher divisor until it reaches prime. Where it will leave that loop and add to primecount. doesn't that cycle through all possible divisors before it is added to my primecount?

OpenStudy (anonymous):

I just tried printing out my counts as it was calculating so i could see where the calculation was going. http://codepad.org/CYk0wVNA Im getting mostly prime numbers, but numbers like 95, 121, 209 are getting through... btw I only went up to 100 so it would be more manageable. maybe that will give some insight into whats wrong...

OpenStudy (anonymous):

http://codepad.org/KBfWZ69A hmm.. i have seen this before - at lines 7 and 9 you are changing the number you are testing inside the loop - this can be problematic because the loop keeps running and the new number doesn't get tested with all the divisors - there are a few things you could do - you will have to be creative - you need to somehow start your divisors over again, you mentioned that in a previous post but didn't address it. if you want to attempt to solve it with your first try, it is still available at the link you posted abaove

OpenStudy (anonymous):

you can see that happening in lines 708-713 of the output

OpenStudy (anonymous):

oohh I see what you're saying. Its switching primes without resetting the divisor. hmm... Ok. Ill try again with my first code.

OpenStudy (anonymous):

omg! i just got it with my second code from ur advice. I simply reset the divisor to 2 each time prime is changed. wow thanks so much for your help! http://codepad.org/39XHfKx8 thats my final code in case you were curious haha. thanks again!

OpenStudy (anonymous):

cool - http://codepad.org/PfZ1TgJ8

OpenStudy (anonymous):

hmm.. what is isPrime? is that a function that tests for primality? perhaps i missed it in the reading....

OpenStudy (anonymous):

http://codepad.org/TubE4415

OpenStudy (anonymous):

cool u were a huge help.

OpenStudy (anonymous):

Hi guys, this is the code I came up with. Seems to work: prime_count = 0 divisor = 0 x = 1 while prime_count < 1000: for i in range(1, x+1): if x%i == 0: divisor = divisor + 1 if divisor == 2: prime_count = prime_count + 1 divisor = 0 x= x+1 print i

OpenStudy (anonymous):

that looks similar to my code and it looks like it functions the same but mine i used much different key words. can u guys check it out? i posted it as a question

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!