A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
My first attempt at prob set 1... When I call the Find1000Prime function I get the right answer but there is a slight delay as if my code is very inefficient. Will anyone provide me with suggestions to improve this code?
def isPrime(num):
i = 2
if num == 1:
return 0
if num == 2:
return 1
while i <= num1:
if num%i == 0:
return 0
else:
i = i + 1
return 1
def Find1000Prime():
i = 2
j = 0
while j < 1000:
if isPrime(i) == 1:
j = j + 1
if j == 1000:
return i
i = i + 1
anonymous
 5 years ago
My first attempt at prob set 1... When I call the Find1000Prime function I get the right answer but there is a slight delay as if my code is very inefficient. Will anyone provide me with suggestions to improve this code? def isPrime(num): i = 2 if num == 1: return 0 if num == 2: return 1 while i <= num1: if num%i == 0: return 0 else: i = i + 1 return 1 def Find1000Prime(): i = 2 j = 0 while j < 1000: if isPrime(i) == 1: j = j + 1 if j == 1000: return i i = i + 1

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Try using this algorithm. http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Awesome code! I was lost as to finding a function to i.d. prime numbers. But for efficiency the range for the divisor does not have to be from 2 till number1, rather 2 till sqrt(number) is sufficient http://en.wikipedia.org/wiki/Primality_test
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.