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

hey guys, been working on problem set one, the one with the 1000th prime number, and so far I get this. http://dpaste.com/568125/ So far I'm getting just the prime numbers, but I can't figure out how to stop at the 1000th prime

OpenStudy (anonymous):

use a tuple to store the prime numbers u get..and then print the 1000th member of the tuple at last

OpenStudy (anonymous):

I don't think a tuple is needed, though of course it's a neat way to do it. Just create some kind of 'counter' for the primes. It'll loop through every prime candidates but would only increase when a prime is found.

OpenStudy (anonymous):

well i suggest tuple,,coz it will cut down your task by 99% in the next problem :)

OpenStudy (anonymous):

i'm considering your tuple suggestion, but I'm unsure how to turn my list of prime numbers into a tuple :(

OpenStudy (anonymous):

i mean, the prime numbers aren't even in a list. it's just a readout of the prime numbers in as int

OpenStudy (anonymous):

well first initialise the tuple outside the for loop..like.. prime_numbers=() then replace print candidate by prime_numbers= prime_numbers + (candidate,) that will add candidate to the tuple.. then at last u can print the 1000th member of the tuple by prime_number[999]

OpenStudy (anonymous):

Hmmm true enough though. It did cause me some headaches when I got to problem b...

OpenStudy (anonymous):

Sunu, thanks for helping me use tuples. When I was doing my studying, I didn't even get to them to be honest with you. Well I did as you suggested, and for some reason when I index the tuple at 999, the computer is returning a value of 3055, which isn't prime. Perhaps it is the way I defined prime numbers, but before I stored the primes in the tuple, everything looked good.

OpenStudy (anonymous):

http://dpaste.com/568161/

OpenStudy (anonymous):

you could use a while loop to terminate the loop once you have found the solution

OpenStudy (anonymous):

Actually, it's in the logic you used to determine primes. In the beginning, it would look good, since the lower candidates (3, 5, 7, 9, 11, etc) are mostly primes anyway, and the ones that are not could be eliminated with your test. However, once it gets to the bigger numbers, more divisors appear and thus your test becomes obsolete. I would iterate the divisors with a loop, and see if the current candidate can be divided with any of them. If not, then it's a prime.

OpenStudy (anonymous):

But I don't know the answer, if I didn wouldn't that be cheating?

OpenStudy (anonymous):

I also looked into my prime numbers, and how I defined them is wrong. For example I have 35, and 55 and many others.

OpenStudy (anonymous):

Thaks lifeburner, let me get on that :)

OpenStudy (anonymous):

Wait, what answer? If you mean the real value of the 1000th prime, there's a link in you problem sheet containing the first 1000 primes.

OpenStudy (anonymous):

looked into it...and as u said there were 35s and 55s and so on in that list.. so u should do as lifeburner says.. make a loop and iterate the divisors..that will be better.. and for the cheating part..actually what 007 meant is that.. include.. while len(prime_numbers)<1000: at the top so that the code will stop when u have 1000 prime numbers in your tuple :)

OpenStudy (anonymous):

actually, I meant have a counter so that each check you do on a number, if it is prime, save it and increment the counter. else, move to the next number. just a note, if you are going to divide by all the numbers between 1 and the number before the number you are checking, then you can skip dividing by the evens, sine no prime number after 2 is even.

OpenStudy (anonymous):

and according to the optimization lesson learnt today..i suggest divide by numbers between 3 and sqrt(n) and yes if u want to take a look ...here is what i did http://dpaste.com/hold/563189/

OpenStudy (anonymous):

Oh that. :P I wouldn't call that cheating, really.

OpenStudy (anonymous):

Ok, so here's my explanation of his code: x and y are two integers, one is the current prime candidate and the other is the current divisor being tested against the candidate. The len(prime_num) < 1000 part is sunu's way of checking whether the current prime is the 1000th prime or not. It basically involves a tuple which includes all the primes counted. The entire block under the 2nd while keyword is for testing whether the current candidate is a prime or not. So basically: while x>y: if x%y ==0: #if x can be divided by y, then x=x+2 #increase the value of x to x+2 y=3 #and reset y back to 3 else: y=y+2 #if it's not a divisible with the current y, increase y to y+2 prime_num= prime_num + (x,) #if x is a prime, adds prime number to the tuple x=x+2 #then increase x's value to x+2 y=3 #and reset y back to 3 So, as you can see, the act of increasing x's value is actually us moving to the next prime candidate. And increasing y's value means checking against the next divisor for that candidate.

OpenStudy (anonymous):

so how far have u gone?..keep us updated about ur doubts.. coz i'm actually quite free right now,...and would love to do something..:p

OpenStudy (anonymous):

let me guess a thing...kjs862 sleeps with his computer on.. :\

OpenStudy (anonymous):

I do sleep with my computer on, but my bed is right next to my computer.

OpenStudy (anonymous):

IMO, I think this problem set should have been given after lecture three. I mean the professors didn't even touch for while loops till lecture 3.

OpenStudy (anonymous):

Oh good, me too. My laptop is always located right next to my bed.

OpenStudy (anonymous):

It is just that i am a supporter of save energy campaign :\.....and i can't really know when u are REALLY online...lol

OpenStudy (anonymous):

well, i agree with your 2nd reason. :P as for the save energy campaign, im a gamer.

OpenStudy (anonymous):

me too lol..i don't leave my Xbox or pc on after that ..LOL...still the electricity bill gets me some angry staring from mom ..lol

OpenStudy (oussama):

These is my solution http://ideone.com/Tg3BH

OpenStudy (anonymous):

Nice n neat piece of code there^^^ and the site is great too..it shows the output :)

OpenStudy (anonymous):

Here's my solution: http://ideone.com/w5RHp

OpenStudy (anonymous):

Well, I'm damned. It's an online interpreter.... say goodbye to IDLE, everyone. Since we're using Python anyway, I propose using these as the standard, instead of DPaste.

OpenStudy (anonymous):

I second you lifeBurner ..that one shows memory used..time used too...great site :)

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!