Prime number



“Methods for finding the given number is prime”



AbstractThis document mentions that some of the methods to check a given number is prime or not and calculating the efficiency of each type and comparing the efficiency among all proposed methods. Clearly indicates which method is more efficient in this type of problem.

I. Introduction

A prime number is a number that is divisible by one and itself.  There are various methods there to find a given number is prime or not. The only even prime number is 2. All other even numbers can be divided by 2. If the sum of a number's digits is a multiple of 3, that number can be divided by 3. No prime number greater than 5 ends in a 5. Any number greater than 5 that ends in a 5 can be divided by 5. Zero and 1 are not considered prime numbers. Except for 0 and 1, a number is either a prime number or a composite number. A composite number is defined as any number, greater than 1, that is not prime. To prove whether a number is a prime number, first try dividing it by 2 and see if you get a whole number. If you do, it can't be a prime number. If you don't get a whole number, next try dividing it by prime numbers: 3, 5, 7, 11 (9 is divisible by 3) and so on, always dividing by a prime number (see table below).

Here is a table of all prime numbers up to 1,000:   
2        3             5             7             11           13           17
19      23           29           31           37           41           43
47      53           59           61           67           71           73
79      83           89           97           101         103         107
09      113         127         131         137         139         149
151    157         163         167         173         179         18  
191    193         197         199         211         223         227
229    233         239         241         251         257         263
269    271         277         281         283         293         307
311    313         317         331         337         347         349
353    359         367         373         379         383         389
397    401         409         419         421         431         433
439    443         449         457         461         463         467
479    487         491         499         503         509         521
523    541         547         557         563         569         571
577    587         593         599         601         607         613
617    619         631         641         643         647         653
659    661         673         677         683         691         701
709    719         727         733         739         743         751
757    761         769         773         787         797         809
811    821         823         827         829         839         853
857    859         863         877         881         883         887
907    911         919         929         937         941         947
953    967         971         977         983         991         997         

In this paper we are discussing one such method such as “mod” method. “mod” is a mathematical method in which it will find a reminder under the division operation. In method 1 Checking each number from 2 to (n-1) numbers. In this method, we check each and every number that comes under 2 to (n-1) whether they are completely divisible or not. If any number is found in between these two then we can conclude that the given number is prime otherwise it is a non-prime number. In method 2 checking half of the given number. In this version, we need to check only half of the number instead of checking entire numbers. Since in the entire numbers checking takes the maximum amount of time that’s why it has been reduced to half. In method 3 Checking a square root of a given number. In this version, we need to check up to the square root of the number instead of checking entire numbers or up to half of that number. In method 4 a function that determines if a number is prime or not. The function must return 1 if prime, 0 if not. Besides, the function must "return" the value of one of the divisors. The best method for checking a given number is prime or non-prime is method 4 that is using even or odd number method because efficiency is high when compared to other methods like square root(method3), a number divided by 2 (method 2) and check the divisibility of given number starting from 2 to n-1. The efficiency and time complexity of all the above-mentioned methods with the help of the algorithm are discussed below.

II. Algorithm development

Check whether the given number is prime or not under different proposed methods. Comparing the efficiency of algorithms among different solution. The different possible ways to find the given number is prime or not is as shown below.
              i.            Checking each number from 2 to (n-1) numbers.
             ii.            Checking an  (n/2) half of the given number.
            iii.            Checking the square root of the given number.
            iv.             Checking a number based on even or an odd number.

III. Algorithm and analysis of the algorithm

The different approaches to find the given number are prime or not are shown in method 1, method 2, method 3 and method 4.

Method 1:            


Algorithm
Input: n {it must be positive integer}
Output: prime or not
{
   m ßn-1
   for kß 2 to m
{
If (n mod k==0) then {not prime, STOP}
                           else {continue k} 
}
{
n is prime number, STOP}
}

Method 2:            

Algorithm

Input: n {it must be positive integer}
Output: prime or not
{
   m ßn/2
   for kß 2 to m
{
If (n mod k==0) then {not prime, STOP}
                           else {continue k} 
}
{n is prime number, STOP}
}

Method 3:            

Algorithm

Input: n {it must be positive integer}
Output: prime or not
{
   m ßsqrt(n)
   for k<- 2 to m
{
If (n mod k==0) then {not prime, STOP}
                           else {continue k} 
}
{
n is prime number, STOP}
}
}

Method 4:         

The algorithm to check the even and odd number then find the given number is prime or not.
{
 Input: x, div.
 output: prime or non-prime.
   
   if (prime2 (x, div)) then   {is a prime number, x exit}
    else  {not prime number. Divisible by, x, div, exit}
}

 even (n)
{
return (!(n mod 2));
}

 prime2 (n, divisor)
{        
 Input : i, is_prime;
          
           divisor ß 0;
           if (even (n))
           {
                           if (n==2)
                                           divisorß0;
                           else
                                           divisorß2;
           }
           else
           {
                           if (n==1)
                                           divisorß0;
                           else
                                          
                                           For  iß3 to i<=sqrt(n) ißi+2
                                           {
                                                           if (!(n mod i))
                                                           divisorßi;
                                           }
           }
          
           is_prime ß divisor;
           return (!is_prime);
} 

IV. Experimentation and profiling

A priori Analysis:
                A Priori Analysis is analyzing the algorithm and calculating the computation time for the algorithm is termed as A Priori Analysis. After having made an analytical review of the algorithm, the calculated time complexity for it is of the O(n-2).

to check n is divisible by k. If remains indivisible
for k=2,3,4,5...(n-1)
then n is prime
else non-prime

computing time complexity:

tmax  α (n-2)
tmin  α 1
tO α (n-2)
tΩ   α 1

(n-2) is a polynomial of degree 1.

Method 1

Time complexity
frequency
Best case
Worst case
2
1
1
3
1
m-1
2
1
m-2
0
1
0
1
0
m-2

Table1  Time complexity for the best-case and the worst-case.

Frequency of Testing:

Best case efficiency:
A best-case is one in which a given number is a prime number. Frequency of testing best-case as follows.
tbest  α(2*1+3*1+2*1+0*1+1.0)
tbest α7
tbest =C*7*1  (where an α is replaced by some constant “C’)
tbest αβ*1
tbest α1

Worst-case efficiency:
The worst case is one in which an element, not a prime number. In this condition, algorithm efficiency is as shown.
                                        
tworst α 1*2+(m-1)*3+(m-2)*2+0*0+(m-2)*1
tworst  α 6m-7
tworst α  6(n-1)-7                       ( since m=n-1)
tworst  α  6n-13 ...........................< 1 >                                                                                 

This algorithm is a linear algorithm of degree 1.

Method 2:   

Frequency of Testing:
 
Worst-case efficiency:
tworst  α  6n-13                     (Taken from [1])
tworst  α  6(n/2)-13
tworst α  3n-13.....................................< 2 >
    
We have noticed from the above equation. The worst complexity has been reduced to almost half a value when compared to equation number [1].

Method 3:

Frequency of Testing:
 
Worst-case efficiency:

tworst  α  6n-13                     (Taken  from [1])
tworst α  6 sqrt(n)-13.....................................< 3 >

we have noticed from the above equation The worst complexity has been reduced to almost square root a value when compared to equation number [1] and [2].

Method 4:

Frequency of Testing:
 
Worst case efficiency:
tworst α 1*1+0*1+1*1+1*1+1*1+1*1+1*1+1*1+1*1+1*1+1*1
+5*(n-3)+2*n+1*1+1*1+1*1
tworst α 7n-2

A Posteriori Analysis:
A Posteriori Analysis is Analysis is done after the execution of the algorithm in the Computer. This Analysis is also called as Profiling.





















Figure 1: Comparing the time complexity of different methods.

Where,
continuous line indicates posterior analysis.
dotted line indicates apriori analysis.

V.    Summary / conclusion

The below table shows the number of cycles that needs to be executed to find a solution to the problem.

input
n
(n/2)
Squair root(n)
10
10
5
4
20
20
10
5
30
30
15
6
40
40
20
7
50
50
25
7
100
100
50
10
500
500
250
24
1000
1000
500
33
    
   Table2: comparing the efficiency





 
Figure 2: Comparing the time complexity of different methods.

               

                By comparing the value given in the table we can conclude that (square root(n)) method 3 gives the maximum efficiency when compared to other proposed methods as shown above table 1.


References



[1]        Dudley, Underwood (1978), Elementary number theory (2nd ed.), W. H. Freeman and Co., ISBN 978-0-7167-0076-0, p. 10, section 2.

[2]        Hardy, Michael; Woodgold, Catherine (2009). "Prime Simplicity". Mathematical Intelligencer 31 (4): 44–52.

[3]        C. S. Ogilvy & J. T. Anderson Excursions in Number Theory, pp. 29–35, Dover Publications Inc., 1988 ISBN 0-486-25778-9.

[4]        Apostol, Tom M. (1976), Introduction to Analytic Number Theory, Berlin, New York: Springer-Verlag, ISBN 978-0-387-90163-3, Section 1.6, Theorem 1.13.

[5]        Derbyshire, John (2003), Prime Obsession, Joseph Henry Press, Washington, DC, ISBN 978-0-309-08549-6, MR 1968857.