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

Below is my code for finding the 1000th prime number. PS1 part 1. primeCounter=0 #keeps track of the number of primes oddNum=1 #Odd Number beginning 1 var=0 arrayOddNum=[] while(primeCounter!=999): oddNum=oddNum+2 arrayOddNum=[oddNum] length=len(arrayOddNum) for index in arrayOddNum: var=var+1 temp=0 while(temp!=var): for i in range(len(arrayOddNum)): if(oddNum%arrayOddNum(1)!=0): print oddNum temp=temp+1 primeCounter=primeCounter+1

OpenStudy (anonymous):

I think that 'for i in arrayOddNum' will suffice - in my (limited) experience working with python the 'for thing in otherthing' will take each element and work through it - if it's a multi-line document 'thing' will be an individual line, in a list 'thing' will be an item from the array, and in a dictionary it will be an individual tuple. Hope that helps a bit - I haven't actually ran the code to test it out for sure.

OpenStudy (anonymous):

Why did you assign this: length=len(arrayOddNum)? You never called it. Also, comments would help us see what you are thinking (which I learned from an uncommented post of my own).

OpenStudy (anonymous):

First, I must apologise if I misread your code, I'm new to this. while(primeCounter!=999): ... arrayOddNum=[oddNum] ... Wouldn't this actually reassign your list for every loop? If that is the case why use a list? If you're trying to add an element, use the append method. In terms of your algorithm, that is as far as I can think, it could work, except that you skipped 2 which is a prime by itself. Finally, a nitpick. Wouldn't it be inefficient if you divide the current number by all the odd number that came before it? Why not just store the prime numbers you have gathered so far and use that instead; all other odd numbers could be expressed in terms of prime(I think).

OpenStudy (anonymous):

use dpaste.com or a similar code pasting site to post your code pls

OpenStudy (anonymous):

Array notation uses brackets [] rather than parenthesis in line 15: if(oddNum%arrayOddNum(1)!=0): ...also there, you want to use an index variable (i) instead of the hardcoded 1 In your while(temp!=var) loop, you only increment temp if you get an even divide. Otherwise, temp doesn't get incremented and you loop forever. I always try to increment my loop sentinel as the last statement in the loop just to remind myself and to keep it out of any conditional branches. As GoonDu mentioned, you will probably want to hardcode the number 2 as your first prime and use the append method for extending an existing array like so: arrayOddNum.append(oddNum) The length and index variables aren't referenced, so you can clean out those assignments. You'll want to revisit your logic and carefully walk through your program. The steps outlined in the assignment are an excellent basis for your own program logic. Note that a prime is only a prime if it is not evenly divisible by any of the primes less than it, so you will have to check all of them and not just one. I hope this helps.

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!
Latest Questions
Lilmunchin: Trump or Biden
7 minutes ago 15 Replies 1 Medal
ARTSMART: Art!
44 minutes ago 5 Replies 4 Medals
Jasonisyours: What were the key causes of the French Revolution in 1789?
38 minutes ago 3 Replies 5 Medals
PureSoulless: Why is the word "Pedophile" always censored in yt vids?
1 day ago 3 Replies 0 Medals
Jalli: What's 58x3634u00b07
22 hours ago 6 Replies 3 Medals
arriya: who wanna play roblox
1 day ago 5 Replies 1 Medal
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!