Number of divisors of a given number N which are divisible by K


Finding the number of divisors of a given number N which is also divisible by K (any constant number) is a typical mathematical problem which requires lot of calculations. Here, K is usually a number which is less than or equal to square root of N. However, we can construct a C++ program by which counting such numbers will be an easier task.

In this article, we will discuss different methods in C++ using which we can find solution to the above given problem.

Input Output Scenarios

If we consider the below scenario here we have N value as 50 and the K value as 5. The divisors of 50 are 1, 2, 5, 10, 25, 50 and, among these 5, 10, 25, 50 are divisible by 5. Therefore, the output should be 4.

Input: N = 50, K = 5
Output: 4

Similarly, if we consider the N value as 120.

  • The divisors of 120 are 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24, 30, 40, 60, 120.

  • Among these the numbers that are divisible by K i.e. 10 are 10, 20, 30, 40, 60 and, 120.

  • Therefore, the number of divisors of 120 divisible by 10 are 6.

Input: N = 120, K = 10
Output: 6

Using a For Loop and the && Operator

This is a simple method where we use the for loop to iterate over values from 1 to N. Within the for loop, we use the if statement for finding the count of numbers which fulfills the required conditions. These conditions are −

  • The number should be divisor of N.

  • And should be divisible by K.

To check the divisibility of a number, we use the modulo operator.

Modulo operator (%) − This operator is used to get the remainder of a division operation. If remainder of a division is zero, then it means that the dividend is divisible by the divisor. For example, (25 % 5) is 0, hence 25 is divisible by 5.

Example

In the following example we are trying to calculate the Number of divisors of 100 which are divisible by 5.

#include <iostream>
using namespace std;
int findNumberOfDivisors(int N, int K){

   // Variable to store the result
   int find = 0, x;
   
   // Iterate from 1 to N  
   for(x = 1; x <= N; x++){
      if (N % x == 0 && x % K == 0){
         find++;
      }
   }
   return find;
}
int main(){

   // Declaring values of N and K
   int N = 100, K = 5;
   cout << "Number of divisors of " << N << " which are divisible by " << K << " are " << findNumberOfDivisors(N, K);
   return 0;
}

Output

Number of divisors of 100 which are divisible by 5 are 6

By Iterating up to the Square Root of N

We have another alternate solution here we will optimize our loop by iterating from 1 to square root of N.

In this approach, we will use one of the properties of divisors which states that divisors always occur in pairs and, one of them is smaller or equal to square root of the number.

For example, if N = 100, then the pairs of divisors are: (1, 100), (2, 50), (4, 25), (5, 20), (10, 10).

  • Therefore, if the loop we will; search for the numbers up to the square root of the given numbers.

  • Then, we check whether the number is a divisor of N and is also divisible by K. If both conditions are satisfied, we will increment the count variable.

  • In order to avoid counting of the same divisor twice when iterating the form 1 to √(N), we write another if statement. Also, we check whether N/x is a factor of K (Since x is a divisor of N and x is divisible by K). If both the conditions are satisfied, we need to increment the count variable again.

This method reduces the number of required iterations. Hence, it increases the performance by decreasing the time complexity.

Note  The cmath is a C++ standard library header which enables the developers to perform mathematical functions and operations like sin(), cos(), pow(), sqrt() etc.,

Example

Following is an example −

#include <iostream>
#include <cmath>
using namespace std;

// Function for finding the number
int findNumberOfDivisors(int N, int K){

   // variable to store the result
   int find = 0, x;
   
   // Iterate from 1 to √(N) 
   for (x = 1; x <= sqrt(N); x++) {
      
      // Check if x is divisor of N 
      if (N % x == 0) {
         
         // Check if x is divisible by K
         if (x % K == 0) {
            find++;
         }
         if (x != N / x && (N / x) % K == 0) {
            find++;
         }
      }
   }
   return find;
}

int main(){

   // Declaring values of N and K
   int N = 100, K = 5;
   cout << "Number of divisors of " << N << " which are divisible by " << K << " are " << findNumberOfDivisors(N, K);
   return 0;
}

Output

Number of divisors of 100 which are divisible by 5 are 6

Conclusion

We have explored various techniques which can be used to count the number of divisors of a given number N and which is divisible by K. We have seen different methods which includes use of modulo operator in a for loop and optimized loop technique.

The modulo operator approach is an easy-to-use method whereas optimized loop technique is an effective method which improves performance. Now, the question arises: which method is better? The answer is that it depends on the requirement of the problem and the size of N

Updated on: 12-Jul-2023

94 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements