A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 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

anonymous
 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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Cool! 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
 5 years ago
Best ResponseYou've already chosen the best response.0It 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.
Ask your own question
Sign UpFind more explanations on OpenStudy
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...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 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.