2

So I've created a program to generate a number and check if it's prime however i would like to get to a far larger prime. The current program works quickly for primes up to 2ish million.

def is_Prime(num):
if(num < 2):
    return False
elif (num == 2):
    return True

if(num%2 == 0):
    return False

for i in range(3, int(num**0.5)+1, 2):
    if(num%i == 0):
        return False
return True

bigPrime = 0;
for i in range(1000000):
    possiblePrime = (2*i) +1
    if(is_Prime(possiblePrime)):
        bigPrime = possiblePrime

print(bigPrime) 

The bigPrime part where i write

possiblePrime = (2*i) + 1

is an algorithm my Computer Science teacher was telling me about that is good at generating prime numbers.

Any help with either my is_Prime function or the generation algorithm is much appreciated.

smci
  • 26,085
  • 16
  • 96
  • 138
HristoM
  • 23
  • 1
  • 5
  • 4
    possible duplicate of [Quickly determine if a number is prime in Python for numbers < 1 billion](http://stackoverflow.com/questions/4545114/quickly-determine-if-a-number-is-prime-in-python-for-numbers-1-billion) – Patrick Collins Mar 18 '15 at 07:32
  • The problem is its very slow after a prime greater than 1 million – HristoM Mar 18 '15 at 08:08
  • Do you want to fix the sieve, or do you want a better algorithm? You can do much better than a sieve, I believe, if you use something like Fermat's primality test. – Patrick Collins Mar 18 '15 at 08:48

1 Answers1

1

Here is a code that gives the prime number 87178291199 in a very short time.

Firstly, I changed for a quickest primality test. First I build a list of prime numbers lower than some limit (at school you learn the Eratosthenes sieve to do this, here is more efficient algorithm). Then, to check if a number is a prime, I check if it is divisible by any of the prime numbers of the list.

Secondly, I changed your possiblePrime. Your candidates were all the odd integers: this is way too much if you just want a “big prime number”. Good candidates are Mersenne numbers (of the form 2**n - 1) and factorials (n! -1 or n! +1). See Wikipedia. You can add at your convenience other ways to generate big candidates.

def rwh_primes2(n):
    # http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188
    """ Input n>=6, Returns a list of primes, 2 <= p < n """
    correction = (n%6>1)
    n = {0:n,1:n-1,2:n+4,3:n+3,4:n+2,5:n+1}[n%6]
    sieve = [True] * (n//3)
    sieve[0] = False
    for i in range(int(n**0.5)//3+1):
      if sieve[i]:
        k=3*i+1|1
        sieve[      ((k*k)//3)      ::2*k]=[False]*((n//6-(k*k)//6-1)//k+1)
        sieve[(k*k+4*k-2*k*(i&1))//3::2*k]=[False]*((n//6-(k*k+4*k-2*k*(i&1))//6-1)//k+1)
    return [2,3] + [3*i+1|1 for i in range(1,n//3-correction) if sieve[i]]


LIMIT = 1000000
PRIME_LIST = rwh_primes2(LIMIT)

# A number is prime if and only if it is not divisible by any prime lower than its square root.
def is_Prime(num):
    for i in PRIME_LIST:
        if i*i> num:
            return True
        elif num%i == 0:
            return False
    return True

# Return a sorted list of possible primes lower than limit.
def possiblePrimes(limit):
    l = []
    # 2**i -1 (Mersenne primes)
    i = 2
    while(i < limit):
        l.append(i-1)
        i *= 2
    # n! + 1 or n! -1 (Factorial primes)
    i = 2
    n = 2
    while(i < limit):
        l.append(i-1)
        l.append(i+1)
        n += 1
        i *= n
    return sorted(list(l))

bigPrime = 0
candidates = possiblePrimes(LIMIT*LIMIT)
for c in candidates:
    if(is_Prime(c)):
        bigPrime = c

print(bigPrime)
Tom Cornebize
  • 1,173
  • 10
  • 30
  • You say “Good candidates are Mersenne numbers” but Wikipedia says “As of October 2014, 48 Mersenne primes are known.”, which does not make Mersenne numbers look like good candidates if you intend to find prime numbers (you might as well list the 48 known such numbers). Wikipedia also says that 2^c-1 is never prime when c is itself composite, meaning that a lot of Mersenne numbers are definitely not good candidates. – Pascal Cuoq Mar 18 '15 at 08:43