Computer >> Computer tutorials >  >> Programming >> Python

Analysis of Different Methods to find Prime Number in Python


A prime number is a a positive integer which is divisible only by 1 or itself. Finding whether a number is prime or not is an interesting programming challenge for long time. Moreover there are different menthods and they have different efficiency. In this article we will look at three such methods and judge which one is more efficient in terms of the timing of their execution.

Check all Divisors

This is a straight forward program where we take every integer from 1 to one less than the given number and keep checking if the number gets divided by any of these. If no number is found which can divide this number then the number is prime.

Example

import time
#Function to check Prime Number
def check_prime(final_val):
   if final_val <= 1:
      return False
   for divisor in range(2,final_val):
      if final_val % divisor == 0:
         return False
      return True
# Track the Start Time
StartTime = time.time()
#Count the number of prime numbers
cnt = 0
for final_val in range(1,10001):
   x = check_prime(final_val)
   cnt += x
print 'Count of prime numbers till',final_val,'is ', cnt
# Track the End Time
EndTime = time.time()
print 'Time Elapsed is: ', EndTime - StartTime

Output

Running the above code gives us the following result −

Count of prime numbers till 10000 is 1229
Time Elapsed is: 2.3100001812

Factors till Square Root(N)

Mathematically it is also found that it is enough to find the factors till square root of the number for which we are checking. This approach reduces the number of iterations and hence should be faster which we can check as below. The logic to implement this idea is below.

  • We find out the square root of the number which is being checked for prime value.

  • We divide the number with each of the value till the square root value starting from value 2, to check if any remainder is left.

  • If at any step in the above the remainder left is zero, then the number is not prime.

Example

import math
import time
def is_prime(final_val):
   # 1 is not a prime number
   if final_val <= 1:
      return False
   i = 2
   while i <= math.floor(math.sqrt(final_val)):
   # Check if any remainders are cerated
      if final_val % i == 0:
         return False
      i += 1
   return True
# Track the Start Time
StartTime = time.time()
cnt = 0
for n in xrange(1, 10001):
   x = is_prime(n)
   cnt += x
print 'Count of prime numbers till',n,'is ', cnt
# Track the End Time
EndTime = time.time()
print 'Time Elapsed is: ', EndTime - StartTime

Output

Running the above code gives us the following result −

Count of prime numbers till 10000 is 1229
Time Elapsed is: 0.0529999732971

Sieve of Eratosthenes

In this method we eliminate the non-prime or composite numbers to get the prime numbers till aspecific number. So below are the steps we follow to achieve that.

  • Make a list of consecutive integers from 2 to that number till which we want find all the prime numbers.

  • Take the first number i.e., 2 and create a list of all its multiples. Eliminate this list of multiples from the original list but not 2. Repeat this for the next number i.e., 3 and so on for the next numbers. Please note that we are eliminating only the multiples and not the number itself. So 5 or 11 never gets eliminated but 10 and 22 get eliminated.

  • After all the eliminations the left over list is the list of prime numbers till the asked number.

Example

import time
def sieve_method(n):
#Create a list of prime numbers
   prime_number_list = []
   for i in range(2, n+1):
# Capture the number if it si not in prime list
   if i not in prime_number_list:
      print (i)
# Add the number to the prime list number if it is a multiple
   for j in range(i*i, n+1, i):
      prime_number_list.append(j)
# Track the Start Time
StartTime = time.time()
cnt = 0
print(sieve_method(25))
# Track the End Time
EndTime = time.time()
print 'Time Elapsed is: ', EndTime - StartTime

Output

Running the above code gives us the following result −

2
3
5
7
11
13
17
19
23
Time Elapsed is: 0.0