A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

Fastest Algorithm to calculate 10000 primes in python ?is it

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

    import time st=time.time() mdu=0 check =3 noofprime=2 primeLs=[2,3] while noofprime!=10000: check=check+2 a=len(primeLs) b=primeLs[a-1] for div in primeLs: #print check mdu=check%div if mdu==0: break if div==b: noofprime= noofprime+1 primeLs=primeLs+[check] et=time.time() #print primeLs print st print et print et-st print primeLs[9999]

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

    time python test.py 1296951327.14 1296951364.78 37.6408441067 104729 real 0m37.667s user 0m37.242s sys 0m0.321s

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

    is it fastest in python as i could not find fastest time in google but found this on c++ http://www.troubleshooters.com/codecorn/primenumbers/primenumbers.htm below is the code avoid printing list it will take too much time but to verify i have put it there but commented and printed the last value ( 9999th value) verified from http://primes.utm.edu/lists/small/10000.txt

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

    Mine isn't bad http://www.youtube.com/watch?v=p6fHllFsy7k

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

    But it would have to be recoded a bit to stop after 1000 primes

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

    time ./primgen.py 7179 # I said explicitly to stop at 7919 since it doesn't count primes real 0m1.302s user 0m0.356s sys 0m0.008s Not bad I suppose

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

    Small point. You don't need to check for divisibility by 2 since you are generating only odd numbers to test. Big point. You can stop testing divisors when they exceed sqrt(x). At 7919, this is the difference between 999 test divisions (up to 7907) and 24 divisions (up to 83). You can also get a small speedup by generalizing the method of generating only odd numbers (not div by 2) to generating numbers not divisible by 2 or 3. Odd numbers not divisible by 3: 5,7, 11,13, 17,19, etc So instead of always counting by 2s, alternate counting by 2 and 4. This eliminates 1/3 of the numbers you are testing (and you could carry this on to 5,7, etc if it was worth doing, but diminishing returns hit fast, especially since the loops you skip would have been quick ones that failed on the first division tests).

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

    On my computer, I got bored waiting for your program to finish generating 10k primes, but 5k primes took about 11 sec. Changing the loop to recognize numbers are prime when div>sqrt(x) took just 0.5 sec for 5000 primes, and about 1.4 sec for 10k primes.

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

    @zifmia thanks for the tips i made changes as you suggested and it works, if you please verify the time as well posting the code below , will try for dict method as well and by the way do you think this code compromises space over time

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

    import time from math import sqrt st=time.time() mdu=0 check =3 noofprime=2 primeLs=[2,3]#,5,7,11,13] prime=True while noofprime!=10000: check=check+2 if check%3==0:check=check+2 sqrcheck=int (sqrt(check))+1 a=len(primeLs) b=primeLs[a-1] for div in range (3,sqrcheck): prime=True mdu=check%div if mdu==0 : prime=False break if prime==True: noofprime= noofprime+1 primeLs=primeLs+[check] et=time.time() print primeLs[noofprime-1] print st print et print (et-st)/60

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

    i get this time when checked without writing to list prime 104729 st:1298174950.11 et:1298174952.94 0.0471333344777

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