• anonymous
------------------------------------------------------ I think this works: ------------------------------------------------------ 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 -------------------------------
MIT 6.00 Intro Computer Science (OCW)
  • katieb
See more answers at
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get this expert

answer on brainly


Get your free account and access expert answers to this
and thousands of other questions

  • 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.
  • 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.

Looking for something else?

Not the answer you are looking for? Search for more explanations.