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

ps1a here's my code. I am attempting to use generator objects for the prime candidate and the divisors. I am missing something and could use some help. I have attached the file because I couldn't get it to show completely here. (my first post) http://www.scribd.com/fullscreen/63456579?access_key=key-d5azbp0ezs7q5lbzvvp

OpenStudy (anonymous):

http://ideone.com/QCamb Uploaded your code to a codepasting site so it will be easier for folks to copy/paste your code and help you etc.

OpenStudy (anonymous):

thank you :)

OpenStudy (anonymous):

Your code is not checking for primes properly.

OpenStudy (anonymous):

Also, you have prime = primeCan.next() both at the end and at the beginning of your while loop. might want to delete the one at the end

OpenStudy (anonymous):

http://codepad.org/ul55Qpe4 Try running this code; I guess it does not properly check for primes since you test the generator object d against the prime candidate which always evaluates to false, and therefore causes you to skip the while block primality test. You should get rid of that divisor generator, and use something else that you can properly test against prime candidates. You may try testing the prime candidates by each element in the primes list by using a for loop, or by simply declaring an integer d = 3 and incrementing it by 2 until d * d > prime or prime % d == 0

OpenStudy (anonymous):

import android droid = android.Android() ## program finds the 1000th prime number ## tests to see if the number is prime def isprime(num): test = 3 if num%2 == 0: return False else: while test <= num: if num % test == 0 and test < num: return False elif num % test != 0 and test < num: test = test + 1 elif num % test != 0 and test == num: test = test + 1 elif num % test == 0 and test == num: return True test = test + 1 count = 2 tester = 3 while count <= 1000: if isprime(tester) == True and count <= 1000: tester = tester + 1 count = count + 1 if isprime(tester) == False and count <= 1000: tester = tester + 1 if isprime(tester) == True and count == 1000: print(tester,'is the 1000th prime') count = count + 1

OpenStudy (anonymous):

You should only test against odd numbers.

OpenStudy (anonymous):

http://ideone.com/VsdD0 Here's my solution. It's not perfect; I'm just using trial division to calculate primes.

OpenStudy (anonymous):

thank you for pointing me in the right direction agdgdgdgwngo, http://codepad.org/HKYYfxiv is my code now, and it works!

OpenStudy (anonymous):

what is the time countdown?

OpenStudy (anonymous):

btw that code you just posted looks like a Eratosthenes' sieve program :)

OpenStudy (anonymous):

Jesse.bo- you mean what's the clock function in agdgdgdgwngo's script? It will report how long it took to calculate the 1000th prime. Or do you mean the program "countdown" explained in Chapter 4.7 of How to Think Like a Computer Scientist?

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!