Given two integer and , the task is to find the count of common factors of two numbers where factors are prime.
Input: A = 6, B = 12
2 and 3 are the only common prime divisors of 6 and 12
Input: A = 4, B = 8
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:
Below is the implementation of the above approach:
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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Common prime factors of two numbers
- Count numbers in a given range whose count of prime factors is a Prime Number
- Check if a number exists having exactly N factors and K prime factors
- Maximum number of prime factors a number can have with exactly x factors
- Find number of factors of N when location of its two factors whose product is N is given
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Maximum count of pairwise co-prime and common divisors of two given numbers
- Count ways to split N! into two distinct co-prime factors
- Count numbers from range whose prime factors are only 2 and 3
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
- K-Primes (Numbers with k prime factors) in a range
- Number of distinct prime factors of first n natural numbers
- Find sum of exponents of prime factors of numbers 1 to N
- Find and Count total factors of co-prime A or B in a given range 1 to N
- Count of nodes in a Binary Tree whose child is its prime factors
- Count of Nodes whose both immediate children are its prime factors
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.