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

Can anyone help me with my code for problem set 1? : test=2 #number to test primes=0 #number of prime numbers counted while (primes < 1000): for i in range(1,test): if test%i == 0: primes=primes else: primes = primes+1 test = test+1 else: print test i keep getting '2049' which isn't even a prime number! gah :(

OpenStudy (e.mccormick):

You can't test a number against just one value. You need to do nested loops. The outer loop finds 1000 primes. The inner loop finds if one number is prime.

OpenStudy (e.mccormick):

Here is another clue. It is prime iff test%i==0 AND test==i

OpenStudy (anonymous):

Thank you for your response :) My understanding was that the "for" command acts as the inner loop? I guess that's wrong hmm is there a better way than the "for" command then?

OpenStudy (e.mccormick):

Oh, didn't even notice that. LOL. I had done it with two while loops when I was woring this in C++, or the same basic thing.

OpenStudy (e.mccormick):

I think you may be cycling your test at the wrong time. You are doing it inside the for loop every time the for loop cycles. Try a smaller number of primes, like 5, and add a bunch of print statements. That should make it a lot clearer where the issues crop up. Also, a paste service like gist on github or pastebin would let me copy your code. Here, it collapses all the lines if I copy it, so I have to reedit it to even try it.

OpenStudy (anonymous):

Okay, I'll try that thank you :) and ooh okay! here: https://gist.github.com/anonymous/5855938

OpenStudy (e.mccormick):

And I see an abandoned else at the bottom! hmmm.... what does print range(1,3) print? Might show you an issue with your range.

OpenStudy (e.mccormick):

One more warning, the "It is prime if test%i==0 AND test==i " does not necessarily mean in the same if statement. Why? Because you are checking all sorts of numbers against all numbers until you reach that number. 10%2=0 but \(10\ne 2\). However, if you keep testing you will eventually hit 10%2=0 and 10=10 and think it is prime! So you need to stop testing when you hit 10%2=0. SPOILER ALERT: http://pastebin.com/bsSCfhX0 That is a solution that will be live for a week. I strongly suggest you tinker with your code more, but if you get really frustrated, there is a commented ou section in there that has a solution.

OpenStudy (anonymous):

Thanks so much for all your help :D

OpenStudy (anonymous):

i hope this helps: test=3 #number to test, 2 is an exception primes=1 #number of prime numbers counted, # since, we r testing from 3, prime=1 while (primes < 1000): b=0 #if test%i!=0, b will remain 0 for i in range(2,test): #loop will run from 2 to test-1 if test%i == 0: b=1 #for non primes b will become 1 if b==0: # if b==0 i.e. test is prime, inc primes primes=primes+1 test = test+1 print test-1 #because, the test statement is increased 1 even when #prime becomes 1000

OpenStudy (anonymous):

I tried the following way: print('hehehe the task is to print the 1000th prime number!!! hope we are successfull') k=1 i=1 while(i<1001): k=k+1 for n in range(2,k-1): if k%n==0: break else: i=i+1 print(k)

OpenStudy (e.mccormick):

@ramesh.kumar.roy That printed out the #97 Few things to watch out for. 1) You want to find the first 1000 primes, not the primes in the first 1000 numbers. So only iterate i when you have found a prime. In the center, you are using a for loop that breaks out if it has a remainder of 0. I like this method. It is efficient because it disposes with a few tests. However, you gave a few organizational issues. First, range([start,] stop[, step]) goes from 0 or start to stop-1, so there is no need to -1 in your range statement. Second, the else is where it would be a prime, so that is where you want to print out primes. It is a good start and only needs a few fixes. Try using dpaste, pastebin, or github's gist. These let you paste code that can be copied out properly. I had to reformat your code to try it so I could see what the end result was.

OpenStudy (e.mccormick):

@ramesh.kumar.roy I found one other issue, it said 15 was prime! So I did a change that involved elif instead of else so I could determine if it really had worked through all the numbers and found a prime. My total number of lines is only one longer than yous and it "finds" from 2 on up. 2 is not really found, I just added it to the print. Try things some more, move them around, and see what you get. If you get frustrated, here is a link to what I did to fix your code: http://dpaste.com/hold/1276460/ However, it is best for your learning if you fix your code and then check to see how I did it.

OpenStudy (anonymous):

I am probably a little late the this discussion, but if you are still working on this (or anyone else!) the sieve of eratosthene ( http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) is the most efficient way to to compute prime numbers. If you write a code that creates one list which will be your candidates (ie all numbers between 2 and N), then a second list that will be your prime numbers. Using the sieve of eratosthenes you start with the smallest number in the list of candidates, and iterate through the list removing all multiples of that number (but not the number itself), then append that number to your list of primes. After appending it to your list of primes, you remove it from the list of candidates, and start over. The smallest number in your list of candidates should now be 3, and you repeat the process. Once you have enough prime numbers, you simply print or return a specific number from your list of primes.

OpenStudy (e.mccormick):

@dknight_75 The goal of the assignment is not to use the most efficient algorithm. This is the first problems in a computer science class and at this point the students need to learn about loops.

OpenStudy (anonymous):

I am a rank amateur myself and struggled with this problem for a while until I came across this tip. It helped me look at the problem from a different angle and eventually write a code that worked and thought it might be useful to others as well.

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!