Given two positive integers N and M., The task is to find the smallest divisor D of N such that gcd(D, M) > 1. If there are no such divisors, then print -1.
Input: N = 8, M = 10
Input: N = 8, M = 1
A naive approach is to iterate for every factor and calculate the gcd of the factor and M. If it exceeds M, then we have the answer.
Time Complexity: O(N * log max(N, M))
An efficient approach is to iterate till sqrt(n) and check for gcd(i, m). If gcd(i, m) > 1, then we print and break it, else we check for gcd(n/i, m) and store the minimal of them.
Below is the implementation of the above approach.
Time Complexity: O(sqrt N * log max(N, M))
- Smallest prime divisor of a number
- Find the k-th smallest divisor of a natural number N
- Smallest N digit number with none of its digits as its divisor
- Number formed after K times repeated addition of smallest divisor of N
- Smallest subarray such that all elements are greater than K
- Smallest power of 4 greater than or equal to N
- Smallest number greater than or equal to N using only digits 1 to K
- Highest and Smallest power of K less than and greater than equal to N respectively
- Smallest number greater or equals to N such that it has no odd positioned bit set
- Smallest number greater than or equal to N divisible by K
- Length of Smallest subarray in range 1 to N with sum greater than a given value
- Smallest number greater than Y with sum of digits equal to X
- Smallest Special Prime which is greater than or equal to a given number
- Smallest integer greater than n such that it consists of digit m exactly k times
- Smallest number greater than n that can be represented as a sum of distinct power of k
- Smallest number greater than or equal to X whose sum of digits is divisible by Y
- Smallest Greater (than S) String of length K whose letters are subset of S
- Max occurring divisor in an interval
- Generating numbers that are divisor of their right-rotations
- Find elements in a given range having at least one odd divisor
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.