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

Forgive me for playing devil's advocate, but I spent many long hours on problem 1 in search of finding a way to define prime numbers and my prime tests did not work right.. Please help me spot my error, and then I'll demonstrate a simpler solution I developed. http://dpaste.com/577985/ By doing this, I only generated a list of odd numbers, not a list of prime numbers. For this reason, I don't believe that dividend%i==0 is a good test for finding a prime number. I believe my error was that once the computer ran a test for dividend%i==0 and found it false for any i, it immediately resorted t

OpenStudy (anonymous):

you need a better definition of what isn't prime, and where you put your branches if one looks your definition of what isn't prime is: potencial_prime%divisor == 0 meaning that anything that: potencial_prime%divisor != 0 is prime (!= is the symbol for does not equal in python) notice how it makes this check for each number in range(2,dividend) so lets take 9%2, 9%2 != 0 , and gets added to your list then it moves on 10%3 this is because dividend += 1 and the for loop continues, next in line this is where your problem is if you changed your else statment to an elif and tested something better, like if i*i > dividend: print dividend, "is prime" right now anytime in your loop that you fail to find out a number is not a prime (sorry for the double negative) you add that number (before your done your test) to the prime list and move on, giving your list a wierd result. Hope that helps

OpenStudy (anonymous):

number mod dividend != 0 (or == 0) is an excellent test for prime-ness sometimes it helps to write down what you want to do on paper then translate it to code. something like: start with a prime candidate ....test for primeness using Every number between 2 and candidate ....if candidate failed (ANY) of the tests go on to the next candidate ....if the candidate passed ALL the tests then it must be prime ....add the candidate to the list and ....move on to the next candidate do all of the above til you have 1000 primes (or the 1000th prime)

OpenStudy (anonymous):

You don't even have to check the modulus between 2 and the candidate number, only up to the square root of the candidate. Any integer past that will need to be multiplied by a smaller number in order to equal the candidate number Ex 16 1*16 2*8 4*4 8*2 <-Already tested 2 so therefore this is redundant

OpenStudy (anonymous):

trust me when i say a fair number of people know, its really not the point we're trying to help with, we're trying to help him understand how to get prime numbers, not get prime numbers faster, just google "python prime numbers" to do that

OpenStudy (anonymous):

I was having a heck of a time with this until I realized I only had to divide up to the sqrt(candidate)- Every bit o info helps, I thinks.

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!