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
- Common prime factors of two numbers
- 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
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- K-Primes (Numbers with k prime factors) in a range
- Number of distinct prime factors of first n natural numbers
- Count of common multiples of two numbers in a range
- Find count of Almost Prime numbers from 1 to N
- Count of numbers below N whose sum of prime divisors is K
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Sum of numbers in a range [L, R] whose count of divisors is prime
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Queries for the difference between the count of composite and prime numbers in a given range
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.