Count common prime factors of two numbers
Last Updated :
25 Jun, 2022
Given two integer and , the task is to find the count of common factors of two numbers where factors are prime.
Examples:
Input: A = 6, B = 12
Output: 2
2 and 3 are the only common prime divisors of 6 and 12
Input: A = 4, B = 8
Output: 1
Naive Approach: Iterate from 1 to min(A, B) and check whether i is prime and a factor of both A and B, if yes then increment the counter.
Efficient Approach is to do following:
- Find Greatest Common Divisor (gcd) of the given numbers.
- Find prime factors of the GCD.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countPrimeFactors( int x)
{
int res = 0;
if (x % 2 == 0) {
res++;
while (x % 2 == 0)
x = x / 2;
}
for ( int i = 3; i <= sqrt (x); i = i + 2) {
if (x % i == 0) {
res++;
while (x % i == 0)
x = x / i;
}
}
if (x > 2)
res++;
return res;
}
int countCommonPrimeFactors( int a, int b)
{
int gcd = __gcd(a, b);
return countPrimeFactors(gcd);
}
int main()
{
int a = 6, b = 12;
cout << countCommonPrimeFactors(a, b);
return 0;
}
|
C
#include <stdio.h>
#include <math.h>
int countPrimeFactors( int x)
{
int res = 0;
if (x % 2 == 0) {
res++;
while (x % 2 == 0)
x = x / 2;
}
for ( int i = 3; i <= sqrt (x); i = i + 2) {
if (x % i == 0) {
res++;
while (x % i == 0)
x = x / i;
}
}
if (x > 2)
res++;
return res;
}
int countCommonPrimeFactors( int a, int b)
{
int gcd, i;
for (i = 1; i <= a && i <= b; ++i)
{
if (a % i == 0 && b % i == 0)
gcd = i;
}
return countPrimeFactors(gcd);
}
int main()
{
int a = 6, b = 12;
printf ( "%d" ,countCommonPrimeFactors(a, b));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int __gcd( int a, int b)
{
if (a == 0 )
return b;
if (b == 0 )
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
static int countPrimeFactors( int x)
{
int res = 0 ;
if (x % 2 == 0 ) {
res++;
while (x % 2 == 0 )
x = x / 2 ;
}
for ( int i = 3 ; i <= Math.sqrt(x); i = i + 2 ) {
if (x % i == 0 ) {
res++;
while (x % i == 0 )
x = x / i;
}
}
if (x > 2 )
res++;
return res;
}
static int countCommonPrimeFactors( int a, int b)
{
int gcd = __gcd(a, b);
return countPrimeFactors(gcd);
}
public static void main (String[] args) {
int a = 6 , b = 12 ;
System.out.println(countCommonPrimeFactors(a, b));
}
}
|
Python3
from math import sqrt,gcd
def countPrimeFactors(x):
res = 0
if (x % 2 = = 0 ):
res + = 1
while (x % 2 = = 0 ):
x = x / 2
k = int (sqrt(x)) + 1
for i in range ( 3 , k, 2 ):
if (x % i = = 0 ):
res + = 1
while (x % i = = 0 ):
x = x / i
if (x > 2 ):
res + = 1
return res
def countCommonPrimeFactors(a, b):
gcd__ = gcd(a, b)
return countPrimeFactors(gcd__)
if __name__ = = '__main__' :
a = 6
b = 12
print (countCommonPrimeFactors(a, b))
|
C#
using System ;
class GFG {
static int __gcd( int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
static int countPrimeFactors( int x)
{
int res = 0;
if (x % 2 == 0) {
res++;
while (x % 2 == 0)
x = x / 2;
}
for ( int i = 3; i <= Math.Sqrt(x); i = i + 2) {
if (x % i == 0) {
res++;
while (x % i == 0)
x = x / i;
}
}
if (x > 2)
res++;
return res;
}
static int countCommonPrimeFactors( int a, int b)
{
int gcd = __gcd(a, b);
return countPrimeFactors(gcd);
}
public static void Main() {
int a = 6, b = 12;
Console.WriteLine(countCommonPrimeFactors(a, b));
}
}
|
PHP
<?php
function __gcd( $a , $b )
{
if ( $a == 0)
return $b ;
if ( $b == 0)
return $a ;
if ( $a == $b )
return $a ;
if ( $a > $b )
return __gcd(( $a - $b ), $b );
return __gcd( $a , ( $b - $a ));
}
function countPrimeFactors( $x )
{
$res = 0;
if ( $x % 2 == 0)
{
$res ++;
while ( $x % 2 == 0)
$x = $x / 2;
}
for ( $i = 3; $i <= sqrt( $x ); $i = $i + 2)
{
if ( $x % $i == 0)
{
$res ++;
while ( $x % $i == 0)
$x = $x / $i ;
}
}
if ( $x > 2)
$res ++;
return $res ;
}
function countCommonPrimeFactors( $a , $b )
{
$gcd = __gcd( $a , $b );
return countPrimeFactors( $gcd );
}
$a = 6;
$b = 12;
echo (countCommonPrimeFactors( $a , $b ));
?>
|
Javascript
<script>
function __gcd(a, b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
function countPrimeFactors(x)
{
let res = 0;
if (x % 2 == 0) {
res++;
while (x % 2 == 0)
x = parseInt(x / 2, 10);
}
for (let i = 3; i <= Math.sqrt(x); i = i + 2)
{
if (x % i == 0) {
res++;
while (x % i == 0)
x = parseInt(x / i, 10);
}
}
if (x > 2)
res++;
return res;
}
function countCommonPrimeFactors(a, b)
{
let gcd = __gcd(a, b);
return countPrimeFactors(gcd);
}
let a = 6, b = 12;
document.write(countCommonPrimeFactors(a, b));
</script>
|
Time Complexity: O(sqrtn*logn)
Auxiliary Space: O(1)
If there are multiple queries for counting common divisors, we can further optimize above code using Prime Factorization using Sieve O(log n) for multiple queries
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...