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

Help With Problem 1a initial=1 Denom=3 OddPrime=True PrimeNumber=0 if (Denom/2)*2==Denom: OddPrime=False print 'Number is even.' else: OddPrime=True print 'Number is odd, checking if prime..' if OddPrime==True: while PrimeNumber<1001: for i in range(initial,Denom): if Denom%i==0: OddPrime=False if OddPrime==False: Denom+=2 else: OddPrime=True if OddPrime==True: Denom+=2 PrimeNumber+=1 if PrimeNumber==1000

OpenStudy (anonymous):

I dont understand why this isnt working correctly. Please help

OpenStudy (anonymous):

Opps. Bottom should look like if PrimeNumber==1000: print Denom

OpenStudy (anonymous):

please paste you code on dpaste.com or pastebin.com. it'll help with debugging

OpenStudy (anonymous):

http://dpaste.com/543250/. There you go, any advice is appreciated

OpenStudy (anonymous):

added an extra period at the end of that link. http://dpaste.com/543250/ sorry.

OpenStudy (anonymous):

fyi, you can choose the style of the code, so you can select python, and it'll add all the pretty syntax colors

OpenStudy (anonymous):

http://dpaste.com/543251/ Even better, thank you

OpenStudy (anonymous):

Your 'initial' variable is 1, so any number divided by 1 will have 0 as a remainder. So line 14 will always be true

OpenStudy (anonymous):

could i fix that simply by setting 'initial' to 2, or would it be more complicated?

OpenStudy (anonymous):

You can, but I think there's other issues too, but that's a start

OpenStudy (anonymous):

ok. let's take a look. lines 3 to 11, will only be executed once. There's no loop to the beginning for it to be reused. I think you wanted the check to be done for every new Denom.

OpenStudy (anonymous):

well the reasoning behind that was i figured that once it was established the number was prime, and if i only added to Denom in intervals of 2, Denom would remain prime and i would not need to check it again. Am i assuming something incorrectly?

OpenStudy (anonymous):

I think you just want to check all odd number starting from 3. Don't get odd and prime mixed up here. You can certainly only check the odd number by incrementing by 2

OpenStudy (anonymous):

for line 5 to 11 since Denom=3, and doesn't every change in those lines. You can effectively remove them, since OddPrime is guarantee to be true.

OpenStudy (anonymous):

is your indentation on line 20 correct?

OpenStudy (anonymous):

i believe so, i think it just got messed up when i pasted it. Heres the revised code: http://dpaste.com/543256/

OpenStudy (anonymous):

ok a couple of coding style tips line 5, is not needed, since OddPrime is already True. line 10, and 14 are also not needed, because you just sent the condition in the line above to be the same as the condition.

OpenStudy (anonymous):

You're almost there. You have all the pieces, and a couple of extra. Just need to remove the extra stuff. consider what happens when you're in your for loop. Once you find the prime, do you stop or do you continue?

OpenStudy (anonymous):

by the way, don't remove line 14 just yet. You'll see why later.

OpenStudy (anonymous):

you'll need to play with the indentations (i.e. where the code is actually called) to get to the solution

OpenStudy (anonymous):

alright i have to get off the computer now, but il play with that tomorrow and see if i can figure it out. I think i have an idea of what you mean, thanks a ton for your help

OpenStudy (anonymous):

you're welcome. good luck. post back if you need more help

OpenStudy (carlsmith):

Hi ntoooto. I'll attach a couple of scripts for you to have a look through. The first one only checks whether a number's prime or not, the second one defines a function for checking primes, using the code from the first, which it then uses to find the 1000th prime. I've documented each line, so they're easy to follow.

OpenStudy (carlsmith):

I put you up a medal as well stuyboyz, keep up the good work mate.

OpenStudy (anonymous):

@carlsmith, I noticed that your prime_suspect.html script returns an overflow-error...when you enter a very large odd number; I was wondering, shouldn't the scripting work for any odd number regardless of its size?

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!