Trial division Algorithm for Prime Factorization
In this article, the trial division method to check whether a number is a prime or not is discussed. Given a number N, the task is to check whether the number is prime or not.
Examples:
Input: N = 433
Output: Prime
Explanation:
The only factors of 433 are 1 and 433. Therefore, it is a prime.
Input: N = 1263
Output: Composite
Explanation:
The factors of 1263 are 1, 3, 421, 1263. Therefore, it is a composite number.
Naive Approach: By definition, a prime number is a whole number greater than 1, which is only divisible by 1 and itself. Therefore, we initialize a loop from 2 to N – 1 and check the divisibility. The following is the pseudo-code for the approach:
N <- input
initialise: i <- 2
while(i ? N - 1):
if(N % i == 0):
return "Composite"
return "Prime"
Time Complexity Analysis:
- For any given number N, the while loop runs for N – 2 times. Therefore, the time complexity for the while loop is O(N).
- The divisibility check is done in constant time. Therefore, the time complexity for the if condition in the while loop is O(1).
- Therefore, the overall time complexity of the above approach is O(N).
Trial Division Method: The primality check can be performed more efficiently by the concept of the trial division method. The Trial Division method is one of the crucial but one of the easiest factorization techniques when dealing with integer factorization.
Observation: The above method works with the observation that the maximum factor for any number N is always less than or equal to the square root(N). This conclusion can be derived in the following way:
- From the school arithmetics, it is a known fact that any composite number is built out of two or more prime numbers.
- Let the factors of N be n1, n2, and so on. The factors are largest only when there exist two factors n1 and n2 for the number N.
- Therefore, let’s assume n1 and n2 are the two largest factors for the number N. These numbers n1 and n2 can be largest only when both n1 and n2 are equal.
- Let n1 = n2 = n. Therefore, N = n * n. Hence, the largest possible factor for N is square root(N).
Approach: From the above observation, the approach for this algorithm is straightforward. The idea is instead of checking till N – 1 for a factor, we only check until square root(N).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int TrialDivision( int N){
int i = 2;
int k = ceil ( sqrt (N));
while (i<= k){
if (N % i == 0)
return 0;
i += 1;
}
return 1;
}
int main()
{
int N = 49;
int p = TrialDivision(N);
if (p)
cout << ( "Prime" );
else
cout << ( "Composite" );
return 0;
}
|
Java
import java.util.*;
class GFG{
static int TrialDivision( int N){
int i = 2 ;
int k =( int ) Math.ceil(Math.sqrt(N));
while (i<= k){
if (N % i == 0 )
return 0 ;
i += 1 ;
}
return 1 ;
}
public static void main(String[] args)
{
int N = 49 ;
int p = TrialDivision(N);
if (p != 0 )
System.out.print( "Prime" );
else
System.out.print( "Composite" );
}
}
|
Python3
def TrialDivision(N):
i = 2
k = int (N * * 0.5 )
while (i< = k):
if (N % i = = 0 ):
return 0
i + = 1
return 1
if __name__ = = "__main__" :
N = 49
p = TrialDivision(N)
if (p):
print ( "Prime" )
else :
print ( "Composite" )
|
C#
using System;
class GFG{
static int TrialDivision( int N){
int i = 2;
int k =( int ) Math.Ceiling(Math.Sqrt(N));
while (i<= k){
if (N % i == 0)
return 0;
i += 1;
}
return 1;
}
public static void Main()
{
int N = 49;
int p = TrialDivision(N);
if (p != 0)
Console.Write( "Prime" );
else
Console.Write( "Composite" );
}
}
|
Javascript
<script>
function TrialDivision(N){
let i = 2;
let k = Math.ceil(Math.sqrt(N));
while (i<= k){
if (N % i == 0)
return 0;
i += 1;
}
return 1;
}
let N = 49;
let p = TrialDivision(N);
if (p)
document.write( "Prime" );
else
document.write( "Composite" );
</script>
|
Time Complexity Analysis:
- The while loop is executed for a maximum of square root(N) times. Therefore, the time complexity of the while loop is O(sqrt(N)).
- The running time of all the if conditions are constant. Therefore, the time complexity of the if statements are O(1).
- Therefore, the overall time complexity is O(sqrt(N)).
Optimized Trial Division Method: The above trial division method can be further optimized by eliminating all even numbers in the range [2, K] where K = square root(N) as 2 is the only even prime number. The overall complexity still remains the same but the number of executions gets reduced by half.
Note: The optimization made in the Trial Division method might seem very small as this method is almost similar to Naive Approach except the number of iterations. However, this drastically reduces the number of computations for higher values of N. This is explained by the following graph plotted against the corresponding running times of the algorithms:
Last Updated :
27 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...