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

I can't even figure out PS1 part one I know that you need some sort of loop that tests if the number is prime and exits as soon as it finds a divisor, but other than that im lost.

OpenStudy (e.mccormick):

Well, have you been able to run anything? Do a basic output?

OpenStudy (anonymous):

yes I have been able to do simple math and use variables, my problem is I'm not exactly sure what to do. im messing with is now and I think i need some variable that will keep dividing different numbers using %. Then once it finds that the potential prime % some number = 0, it adds 2 to the value of the Potential prime and 1 to a counter, then tests the prime again. well something like that. Am I right at all?

OpenStudy (anonymous):

This Is what I came up with. ##Tprime is the number being tested Tprime = 3 ##div is the divisor div = 2 ##count finds the keeps track if the nth prime count = 1 nthprime = 100 while count < nthprime: while div * 2 <= Tprime and Tprime % div != 0: div = div + 1 if Tprime % div != 0: count = count + 1 if count == nthprime: print Tprime div = 2 Tprime = Tprime + 1 How did I do?

OpenStudy (e.mccormick):

If it works, you succeeded. Over time you can make versions that work better. Here are a few tips for this sort of problem. First, is there anything you don't need to test? Well, once you divide by 2, you don't need to test any other even numbers. So you could actually advance by 2 and only test odd numbers 3, 5, 7, etc. Also, once you test the first half, there is no need to test the second half. See, anything that can be divided by half of itself has a quotient of 2. Once you exceed that, you are trying to find quotients that are smaller than 2. So you could set the range of numbers you test to start at 3 and end at (number tested+1)/2 (so that odd numbers round up, not off.) Advance through the range by steps of 2. This tests a lot less numbers and makes the function run faster. Thinking about these sorts of things can be called program efficiency. For a small program, it is not a major concern. However, lets look at a cell phone. Small processor, little ram, etc. It is not a powerful machine. Doing a little more work on program efficiency can make a cell phone program a much better product.

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!