Given two integer
Input: A = 6, B = 12
Output: 2 3
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 display the number.
Efficient Approach is to do following:
Efficient Approach for multiple queries: The above solution can be further optimized if there are multiple queries for common factors. The idea is based on Prime Factorization using Sieve O(log n) for multiple queries.
Below is the implementation of the above approach:
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.
- Count common prime factors of two numbers
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Count numbers from range whose prime factors are only 2 and 3
- K-Primes (Numbers with k prime factors) in a range
- Find sum of exponents of prime factors of numbers 1 to N
- Number of distinct prime factors of first n natural numbers
- Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Maximum count of pairwise co-prime and common divisors of two given numbers
- 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
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Prime factors of a big number
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Sum of Factors of a Number using Prime Factorization
- Prime factors of LCM of array elements
- Print all prime factors and their powers
- Distinct Prime Factors of an Array
- Generate K co-prime pairs of factors of a given number
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.