0

This program generates prime numbers . It works well but I want to speed it up as it takes quite a while for generating the all the prime numbers

#!/usr/bin/python

#intgr = int(raw_input ("Please enter your number: "))
intgr = 50000

for i in range (2, intgr+1):
    j = 2
    while j<i:
        if (i%j) == 0:
            break
        j += 1
    if j == i:
        #print "prime", i
        pass #print "prime", i
print "done"

it takes about 15 seconds to run right now i would like to decrease that time.

bio483
  • 21
  • 1

1 Answers1

-2

Generating prime numbers is inherently slow. The algorithm you implemented is known as Trial Division and is one of the slowest ways to generate primes. There are other algorithms which are much faster, for example the Sieve of Eratosthenes. I suggest you do some more research about better algorithms.

Code-Apprentice
  • 69,701
  • 17
  • 115
  • 226