1

I recently made this bit of code, but wonder if there is a faster way to find primes (not the Sieve; I'm still trying to make that). Any advice? I'm using Python and I'm rather new to it.

def isPrime(input):
    current = 0
    while current < repetitions:
        current = current + 2
        if int(input) % current == 0:
            if not current == input:
                return "Not prime."
            else:
                return "Prime"
        else:
            print current

    return "Prime"

i = 1
primes = []
while len(primes) < 10001:
    repetitions = int(i)-1
    val = isPrime(i)
    if val == "Prime":
        primes.append(i)
    i = i + 2
print primes[10000]
Emil
  • 41
  • 2
  • 10

3 Answers3

2

here is a function that detect if x is prime or not

def is_prime(x):
    if x == 1 or x==0:
        return False
    elif x == 2:
        return True
    if x%2 == 0:
        return False
    for i in range(3, int((x**0.5)+1), 2):
        if x%i == 0:
            return False
    return True

and another implementation that print prime numbers < n

def prime_range(n):
    print(2)
    for x in range(3, n, 2):
        for i in range(3, int((x**0.5)+1), 2):
            if x%i == 0:
                break
        else:
            print(x)

hope it helps !

taoufik A
  • 1,439
  • 11
  • 20
1

If you are not using a sieve, then the next best are probably wheel methods. A 2-wheel checks for 2 and odd numbers thereafter. A 6-wheel checks for 2, 3 and numbers of the form (6n +/- 1), that is numbers with no factors of 2 or 3. The answer from taoufik A above is a 2-wheel.

I cannot write Python, so here is the pseudocode for a 6-wheel implementation:

function isPrime(x) returns boolean

  if (x <= 1) then return false

  // A 6-wheel needs separate checks for 2 and 3.
  if (x MOD 2 == 0) then return x == 2
  if (x MOD 3 == 0) then return x == 3

  // Run the wheel for 5, 7, 11, 13, ...
  step <- 4
  limit <- sqrt(x)
  for (i <- 5; i <= limit; i <- i + step) do
    if (x MOD i == 0) then return false
    step <- (6 - step)  // Alternate steps of 2 and 4.
  end for

  return true

end function

I leave it to you to convert that into Python.

rossum
  • 15,344
  • 1
  • 24
  • 38
0

As in

n = 10000
for p in range(2, n+1):
    for i in range(2, p):
        if p % i == 0:
            break
    else:
        print p
print 'Done'

?

RnRoger
  • 682
  • 1
  • 9
  • 33
  • That seems to be similar to the code I already had- more simple though – Emil Jan 14 '17 at 22:09
  • 1
    it can be further improved by making inner loop to `range(2,p/2)`, since `for i > p/2` `p%i==0` will never happen. – jack jay Jan 14 '17 at 22:09
  • That is what you are asking for isn't it? It's similar yet endlessly faster. It took me half a minute to reach the prime number 201 with your code. (no offense ofc) – RnRoger Jan 14 '17 at 22:11
  • @jackjay It needs to be `range(2, p//2 + 1)`. Otherwise, for instance, it says 4 is a prime number. – Tagc Jan 14 '17 at 22:14
  • @jackjay The fact that it excludes stop is the exact reason why you need to use `p//2 + 1`. Consider if `p` is 4. What will `range(2, 4//2)` yield? – Tagc Jan 14 '17 at 22:22
  • you can also reduce to `sqrt(p+1)`. – jack jay Jan 14 '17 at 22:31
  • You can get some ideas from this post: http://www.dreamincode.net/forums/blog/1748/entry-4665-how-to-think-about-finding-primes/ – Jon Kiparsky Jan 14 '17 at 23:52