A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 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 <= num-1: 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
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Try using this algorithm. http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

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

    Awesome 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 number-1, rather 2 till sqrt(number) is sufficient http://en.wikipedia.org/wiki/Primality_test

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

23

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