A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing


  • 5 years ago

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

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...


  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.