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

for ps1 i thought i did the code correctly but i guess not. what's wrong with it? #initializing variables x=1 count=1 countmax=5000 prime=(2,) #generating a large list of odd integers while count

OpenStudy (anonymous):

please use a code pasting site http://codepad.org/x2OD1IoK you have a loop that starts at line 12 and tests x for primeness x is only prime if it passes the test for all the numbers in the range as the tests are running if any single test in the loop fails, you need to preserve that so when the loop exits you will know if the number is prime

OpenStudy (coolguy123):

ah like defensive programming? and how do i preserve the variable when the loop exits to check?

OpenStudy (coolguy123):

http://codepad.org/wX9S9q2N

OpenStudy (coolguy123):

also adding the prime tuple to the tuple (x,) didn't work for me

OpenStudy (anonymous):

variables or data structures are useful for preserving information http://dpaste.com/715157/

OpenStudy (coolguy123):

ok so what should i do to fix my code?

OpenStudy (anonymous):

test each number (n) to see if it is evenly divisible by any divisor between 1 and n-1 if it is evenly divisible by any of those divisors it is NOT prime if it is NOT evenly divisible by any of those divisors it IS prime everytime you find a prime number print it or add it to a data structure (list, tuple, ...) when you have found the number of primes you need stop

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!