Wheel Factorization Algorithm
Last Updated :
26 Nov, 2021
Given a number N. The task is to check if the given number is Prime Number or not.
Examples:
Input: N = 987
Output: Not a Prime Number
Explanation:
As, 987 = 3*7*47. Therefore 987 is not a prime number.
Input: N = 67
Output: Prime Number
Wheel Factorization Method:
Wheel Factorization is the improvement of the method Sieve of Eratosthenes. For wheel factorization, one starts from a small list of numbers, called the basis — generally the first few prime numbers, then one generates the list, called the wheel, of the integers that are coprime with all numbers of the basis. Then to find the smallest divisor of the number to be factorized, one divides it successively by the numbers on the basis, and in the wheel.
Let say we select basis as {2, 3, 5} and the numbers which are coprime to the basis are {7, 11, 13, 17, 19, 23, 29, 31} are set as the wheel.
To understand it more, see the pattern in the above image that the numbers exhibit. The LCM of the first three Prime Numbers is 30. The numbers(less than 30) which are ending with 7, 1, and 3 and are not a multiple of 2, 3, and 5 and are always prime i.e {7, 11, 13, 17, 19, 23, 29}. Adding the no. 31 to this list and then if we add multiples of 30 to any of the numbers in the list, it gives us a Prime Number.
Algorithm for Wheel Factorization Method:
For the number N to be Prime or not:
bool isPrime(x) {
if (x < 2) {
return False;
}
for N in {2, 3, 5} {
return False;
}
for p= [0, sqrt(N)] such that p = p + 30: {
for c in [p+7, p+11, p+13, p+17, p+19, p+23, p+29, p+31] {
if c > sqrt(N)
break;
else if N % (c+p) = 0:
return False;
}
}
}
return True;
}
Approach:
Following is the approach for the above algorithm:
- For Primality Test of a given Number N, check if the given number is divisible by any of the number 2, 3, 5 or not.
- If the number is not divisible by any of 2, 3, 5, then check if the number formed by adding multiples of 30 in the list [7, 11, 13, 17, 19, 23, 29, 31] divides the given number N or not. If Yes then the given number is not Prime Number, else it is a Prime Number.
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
using namespace std;
void isPrime( int N)
{
bool isPrime = true ;
int arr[8] = { 7, 11, 13, 17,
19, 23, 29, 31 };
if (N < 2) {
isPrime = false ;
}
if (N % 2 == 0 || N % 3 == 0
|| N % 5 == 0) {
isPrime = false ;
}
for ( int i = 0; i < sqrt (N); i += 30) {
for ( int c : arr) {
if (c > sqrt (N)) {
break ;
}
else {
if (N % (c + i) == 0) {
isPrime = false ;
break ;
}
}
if (!isPrime)
break ;
}
}
if (isPrime)
cout << "Prime Number" ;
else
cout << "Not a Prime Number" ;
}
int main()
{
int N = 121;
isPrime(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void isPrime( int N)
{
boolean isPrime = true ;
int []arr = { 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 };
if (N < 2 ) {
isPrime = false ;
}
if (N % 2 == 0 || N % 3 == 0
|| N % 5 == 0 ) {
isPrime = false ;
}
for ( int i = 0 ; i < Math.sqrt(N); i += 30 ) {
for ( int c : arr) {
if (c > Math.sqrt(N)) {
break ;
}
else {
if (N % (c + i) == 0 ) {
isPrime = false ;
break ;
}
}
if (!isPrime)
break ;
}
}
if (isPrime)
System.out.println( "Prime Number" );
else
System.out.println( "Not a Prime Number" );
}
public static void main(String args[])
{
int N = 121 ;
isPrime(N);
}
}
|
C#
using System;
class GFG{
static void isPrime( int N)
{
bool isPrime = true ;
int []arr = { 7, 11, 13, 17,19, 23, 29, 31 };
if (N < 2) {
isPrime = false ;
}
if (N % 2 == 0 || N % 3 == 0
|| N % 5 == 0) {
isPrime = false ;
}
for ( int i = 0; i < ( int )Math.Sqrt(N); i += 30) {
foreach ( int c in arr) {
if (c > ( int )Math.Sqrt(N)) {
break ;
}
else {
if (N % (c + i) == 0) {
isPrime = false ;
break ;
}
}
if (!isPrime)
break ;
}
}
if (isPrime)
Console.WriteLine( "Prime Number" );
else
Console.WriteLine( "Not a Prime Number" );
}
public static void Main(String []args)
{
int N = 121;
isPrime(N);
}
}
|
Python3
import math
def isPrime( N):
isPrime = True ;
arr = [ 7 , 11 , 13 , 17 ,
19 , 23 , 29 , 31 ]
if (N < 2 ) :
isPrime = False
if (N % 2 = = 0 or N % 3 = = 0
or N % 5 = = 0 ):
isPrime = False
for i in range ( 0 , int (math.sqrt(N)), 30 ) :
for c in arr:
if (c > int (math.sqrt(N))):
break
else :
if (N % (c + i) = = 0 ) :
isPrime = False
break
if ( not isPrime):
break
if (isPrime):
print ( "Prime Number" )
else :
print ( "Not a Prime Number" )
if __name__ = = "__main__" :
N = 121
isPrime(N)
|
Javascript
<script>
function isPrime(N)
{
let isPrime = true ;
let arr = [ 7, 11, 13, 17,
19, 23, 29, 31 ];
if (N < 2) {
isPrime = false ;
}
if (N % 2 == 0 || N % 3 == 0
|| N % 5 == 0) {
isPrime = false ;
}
for (let i = 0; i < Math.sqrt(N); i += 30) {
for (let c of arr) {
if (c > Math.sqrt(N)) {
break ;
}
else {
if (N % (c + i) == 0) {
isPrime = false ;
break ;
}
}
if (!isPrime)
break ;
}
}
if (isPrime)
document.write( "Prime Number" );
else
document.write( "Not a Prime Number" );
}
let N = 121;
isPrime(N);
</script>
|
Output:
Not a Prime Number
Time Complexity: O(N3/2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...