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

Problem Set 1, Problem 1. Counting prime numbers question: My program is finding the prime numbers properly, but I can't figure out how to make it count just the prime numbers.

OpenStudy (anonymous):

Here's this: http://pastebin.com/duQFNZ6D

OpenStudy (anonymous):

It is much simpler then that. start out assuming the number is prime. Next all you have to do is check it the number is divisible by any number up to it's square root. if not it's prime so just break. You can check this with just running a loop to test number % index == 0 and if that test is true then the number is not prime then return false.

OpenStudy (anonymous):

Awesome, thanks!

OpenStudy (anonymous):

just make sure to start the loop with i = 2 or you will get stuck in an infinite loop.

OpenStudy (anonymous):

Right, got it.

OpenStudy (anonymous):

You get stuck here is my code. (please try it on your own first) http://pastebin.com/ZZpeULiY

OpenStudy (anonymous):

The frustrating problem with jimtuv and some other helpful responses to this question - problem 1 of set 2, is that they use concepts not taught yet in lectures 1 thru 3. Here is my code that was worked out with much time and frustration, but is simple and works with the bare bones concepts of a beginner. The trick is keeping the indentations correct. #generate n (odd numbers 3 to ?) n=3 count = 1 #go to 999 since we skipped 2 while(count<1000): #test n for prime i=2 while(i<n): if n%i==0: i=n if n%i!=0: i=i+1 if i==n: count=count+1 n=n+2 print n-2 ##

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!