0

I know that python is "slow as dirt", but i would like to make a fast and efficient program that finds primes. This is what i have:

    num = 5 #Start at five, 2 and 3 are printed manually and 4 is a        multiple of 2
    print("2")
    print("3")

def isPrime(n):
#It uses the fact that a prime (except 2 and 3) is of form 6k - 1 or 6k + 1 and looks only at divisors of this form.

    i = 5
    w = 2
    while (i * i <= n): #You only need to check up too the square root of n
        if (n % i == 0): #If n is divisable by i, it is not a prime
            return False
        i += w
        w = 6 - w
    return True #If it isn´t ruled out by now, it is a prime


while True:
    if ((num % 2 != 0) and (num % 3 != 0)): #save time, only run the     function of numbers that are not multiples of 2 or 3
        if (isPrime(num) == True):
            print(num) #print the now proved prime out to the screen
    num += 2 #You only need to check odd numbers

Now comes my questions:
-Does this print out ALL prime numbers?
-Does this print out any numbers that aren't primes?
-Are there more efficient ways(there probably are)? -How far will this go(limitations of python), and are there any ways to increase upper limit?

Using python 2.7.12

4 Answers4

2

Does this print out ALL prime numbers?

There are infinitely many primes, as demonstrated by Euclid around 300 BC. So the answer to that question is most likely no.

Does this print out any numbers that aren't primes?

By the looks of it, it doesn't. However, to be sure; why not write a unit test?

Are there more efficient ways(there probably are)? -How far will this go(limitations of python), and are there any ways to increase upper limit?

See Fastest way to list all primes below N or Finding the 10001st prime - how to optimize?

Community
  • 1
  • 1
Karim Tabet
  • 1,629
  • 1
  • 12
  • 28
  • With "all primes", I meant that the program doesn't skip any. I am wondering if there is any bugs that would do e.g. 2, 3, 7, 11(skipping 5), are there numbers that will not be printed using my script? – DriverUpdate Feb 27 '17 at 12:37
  • Again; I would recommend writing a unit test. It is of course impossible to test every case due to the infinite nature of prime numbers. Or you could just use one of the already tried-and-tested solutions in the links suggested above. – Karim Tabet Feb 27 '17 at 13:28
0

Checking for num % 2 != 0 even though you increment by 2 each time seems pointless.

I have found that this algorithm is faster:

primes=[]

n=3

print("2")
while True:
    is_prime=True
    for prime in primes:
        if n % prime ==0:
            is_prime=False
            break
        if prime*prime>n:
            break

    if is_prime:
        primes.append(n)
        print (n)

    n+=2
heroworkshop
  • 335
  • 2
  • 6
0

This is very simple. The function below returns True if num is a prime, otherwise False. Here, if we find a factor, other than 1 and itself, then we early stop the iterations because the number is not a prime.

def is_this_a_prime(num):
    if num < 2 : return False # primes must be greater than 1
    for i in range(2,num): # for all integers between 2 and num
        if(num % i == 0): # search if num has a factor other than 1 and itself
            return False # if it does break, no need to search further, return False
    return True # if it doesn't we reached that point, so num is a prime, return True
  • This isn't more efficient than mine, i think? This checks all potential factors up to "num" itself, but you know you never have to check anything above the square root of n – DriverUpdate Feb 16 '19 at 21:03
  • Hello @DriverUpdate. It actually doesn't ! You can check it yourself. When a factor is detected other than 1 or the number itself, I return false. – Konstantinos Monachopoulos Feb 17 '19 at 21:28
0

I tried to optimize the code a bit, and this is what I've done.Instead of running the loop for n or n/2 times, I've done it using a conditional statements.(I think it's a bit faster)

def prime(num1, num2):
import math
def_ = [2,3,5,7,11]
result = []
for i in range(num1, num2):
    if i%2!=0 and i%3!=0 and i%5!=0 and i%7!=0 and i%11!=0:
        x = str(math.sqrt(i)).split('.')
        if int(x[1][0]) > 0:
            result.append(i)
    else:
        continue

return def_+result if num1 < 12 else result