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

------------------------------------------------------ I think this works: ps1.py ------------------------------------------------------ def isPrime(n): # this is a function created by me ri = 2 rf = n / 2 isPrim = 1 # yes for b in range(ri, rf+1): mod = n % b if mod == 0: isPrim = 0 # no return isPrim x = 2 lastPrime = 0 numbPrime = 1000 while lastPrime < numbPrime: # range from 0 to 999, i.e. 1000 if isPrime(x): # call function isPrime() print x lastPrime = lastPrime + 1 x = x + 1 -------------------------------

OpenStudy (anonymous):

Cool! I like the way it's boken into two separate problems. One thing you might do is change the variables inside isPrime to obvious booleans, that is, isPrime = True instead of isPrime = 1. The comments are good.

OpenStudy (anonymous):

It is a very nice approach. I have a few comments though. First, since you have a nice function here, why not simply return False rather than setting isPrim=0. That would prevent you from continuing to search for additional divisors after you've already found that the number wasn't prime. Then you can simply return True if you get to the end of the loop without finding a divisor. Second, the project is to find the 1000th prime (and all previous ones) what you've written here will only find primes between 0 and 1000. The 1000th prime is much larger than 1000, so you'll need to modify the condition of your while loop a bit to find the correct results.

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!