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

Given a number N and a number K. The task is to find the number of divisors of N which are divisible by K. Here K is a number always less than or equal to $\sqrt(N)$

Examples:

Input: N = 12, K = 3
Output: 3

Input: N = 8, K = 2
Output: 3

Simple Approach: A simple approach is to check all the numbers from 1 to N and check whether any number is a divisor of N and is divisible by K. Count such numbers less than N which satisfies both the conditions.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to count number of divisors
// of N which are divisible by K
  
#include <iostream>
using namespace std;
  
// Function to count number of divisors
// of N which are divisible by K
int countDivisors(int n, int k)
{
    // Variable to store
    // count of divisors
    int count = 0, i;
  
    // Traverse from 1 to n
    for (i = 1; i <= n; i++) {
  
        // increase the count if both
        // the conditions are satisfied
        if (n % i == 0 && i % k == 0) {
  
            count++;
        }
    }
  
    return count;
}
  
// Driver code
int main()
{
    int n = 12, k = 3;
  
    cout << countDivisors(n, k);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to count number of divisors
// of N which are divisible by K
  
import java.io.*;
  
class GFG {
     
// Function to count number of divisors
// of N which are divisible by K
 static int countDivisors(int n, int k)
{
    // Variable to store
    // count of divisors
    int count = 0, i;
  
    // Traverse from 1 to n
    for (i = 1; i <= n; i++) {
  
        // increase the count if both
        // the conditions are satisfied
        if (n % i == 0 && i % k == 0) {
  
            count++;
        }
    }
  
    return count;
}
  
// Driver code
    public static void main (String[] args) {
      int n = 12, k = 3;
  
    System.out.println(countDivisors(n, k));
    }
}
// This code is contributed by shashank..

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to count number 
# of divisors of N which are 
# divisible by K 
  
# Function to count number of divisors 
# of N which are divisible by K 
def countDivisors(n, k) :
  
    # Variable to store 
    # count of divisors 
    count = 0
  
    # Traverse from 1 to n 
    for i in range(1, n + 1) :
  
        # increase the count if both 
        # the conditions are satisfied 
        if (n % i == 0 and i % k == 0) :
  
            count += 1
              
    return count
  
# Driver code     
if __name__ == "__main__" :
  
    n, k = 12, 3
    print(countDivisors(n, k))
  
# This code is contributed by ANKITRAI1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to count number 
// of divisors of N which are
// divisible by K
using System;
  
class GFG
{
      
// Function to count number 
// of divisors of N which
// are divisible by K
static int countDivisors(int n, int k)
{
    // Variable to store
    // count of divisors
    int count = 0, i;
  
    // Traverse from 1 to n
    for (i = 1; i <= n; i++) 
    {
  
        // increase the count if both
        // the conditions are satisfied
        if (n % i == 0 && i % k == 0) 
        {
            count++;
        }
    }
  
    return count;
}
  
// Driver code
public static void Main () 
{
    int n = 12, k = 3;
      
    Console.WriteLine(countDivisors(n, k));
}
}
  
// This code is contributed by Shashank

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to count number 
// of divisors of N which are 
// divisible by K
  
// Function to count number of divisors
// of N which are divisible by K
function countDivisors($n, $k)
{
    // Variable to store
    // count of divisors
    $count = 0;
  
    // Traverse from 1 to n
    for ($i = 1; $i <= $n; $i++) 
    {
  
        // increase the count if both
        // the conditions are satisfied
        if ($n % $i == 0 && $i % $k == 0) 
        {
  
            $count++;
        }
    }
  
    return $count;
}
  
// Driver code
$n = 12; $k = 3;
  
echo countDivisors($n, $k);
  
// This code is contributed 
// by Akanksha Rai(Abby_akku)

chevron_right


Output:

3

Time Complexity : O(N)

Efficient Approach: The idea is to run a loop from 1 to <$\sqrt(N)$ and check whether the number is a divisor of N and is divisible by K and we will also check whether ( N/i ) is divisible by K or not. As (N/i) will also be a factor of N if i is a factor of N.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to count number of divisors
// of N which are divisible by K
#include <bits/stdc++.h>
using namespace std;
  
// Function to count number of divisors
// of N which are divisible by K
int countDivisors(int n, int k)
{
    // integer to count the divisors
    int count = 0, i;
  
    // Traverse from 1 to sqrt(N)
    for (i = 1; i < sqrt(n); i++) {
  
        // Check if i is a factor
        if (n % i == 0) {
            // increase the count if i
            // is divisible by k
            if (i % k == 0) {
                count++;
            }
  
            // (n/i) is also a factor
            // check whether it is divisible by k
            if ((n / i) % k == 0) {
                count++;
            }
        }
    }
  
    // If the number is a perfect square
    // and it is divisible by k
    if ((i * i == n) && (i % k == 0)) {
        count--;
    }
  
    return count;
}
  
// Driver code
int main()
{
    int n = 12, k = 3;
  
    cout << countDivisors(n, k);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java  program to count number of divisors 
// of N which are divisible by K
  
import java.io.*;
  
class GFG {
      
// Function to count number of divisors 
// of N which are divisible by K 
static int countDivisors(int n, int k) 
    // integer to count the divisors 
    int count = 0, i; 
  
    // Traverse from 1 to sqrt(N) 
    for (i = 1; i < Math.sqrt(n); i++) { 
  
        // Check if i is a factor 
        if (n % i == 0) { 
            // increase the count if i 
            // is divisible by k 
            if (i % k == 0) { 
                count++; 
            
  
            // (n/i) is also a factor 
            // check whether it is divisible by k 
            if ((n / i) % k == 0) { 
                count++; 
            
        
    
  
    // If the number is a perfect square 
    // and it is divisible by k 
    if ((i * i == n) && (i % k == 0)) { 
        count--; 
    
  
    return count; 
  
// Driver code 
      
    public static void main (String[] args) {
      
        int n = 12, k = 3
        System.out.println( countDivisors(n, k)); 
          
    }
}
//This Code is Contributed by akt_mit

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to count number of 
# divisors of N which are divisible by K
import math
  
# Function to count number of divisors
# of N which are divisible by K
def countDivisors(n, k):
      
    # integer to count the divisors
    count = 0
  
    # Traverse from 1 to sqrt(N)
    for i in range(1, int(math.sqrt(n)) + 1):
  
        # Check if i is a factor
        if (n % i == 0) :
              
            # increase the count if i
            # is divisible by k
            if (i % k == 0) :
                count += 1
  
            # (n/i) is also a factor check
            # whether it is divisible by k
            if ((n // i) % k == 0) :
                count += 1
  
    # If the number is a perfect square
    # and it is divisible by k
    # if i is sqrt reduce by 1
    if ((i * i == n) and (i % k == 0)) :
        count -= 1  
  
    return count
  
# Driver code
if __name__ == "__main__":
    n = 12
    k = 3
  
    print(countDivisors(n, k))
  
# This code is contributed 
# by ChitraNayal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to count number of divisors 
// of N which are divisible by K
using System;
  
class GFG
{
      
// Function to count number of divisors 
// of N which are divisible by K 
static int countDivisors(int n, int k) 
    // integer to count the divisors 
    int count = 0, i; 
  
    // Traverse from 1 to sqrt(N) 
    for (i = 1; i < Math.Sqrt(n); i++)
    
  
        // Check if i is a factor 
        if (n % i == 0) 
        
            // increase the count if i 
            // is divisible by k 
            if (i % k == 0) 
            
                count++; 
            
  
            // (n/i) is also a factor check
            // whether it is divisible by k 
            if ((n / i) % k == 0) 
            
                count++; 
            
        
    
  
    // If the number is a perfect square 
    // and it is divisible by k 
    if ((i * i == n) && (i % k == 0))
    
        count--; 
    
  
    return count; 
  
// Driver code 
static public void Main ()
{
    int n = 12, k = 3; 
    Console.WriteLine( countDivisors(n, k)); 
}
}
  
// This code is contributed by ajit

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to count number 
// of divisors of N which are 
// divisible by K
  
// Function to count number 
// of divisors of N which 
// are divisible by K
function countDivisors($n, $k)
{
    // integer to count the divisors
    $count = 0;
  
    // Traverse from 1 to sqrt(N)
    for ($i = 1; $i < sqrt($n); $i++)
    {
  
        // Check if i is a factor
        if ($n % $i == 0) 
        {
            // increase the count if i
            // is divisible by k
            if ($i % $k == 0)
            {
                $count++;
            }
  
            // (n/i) is also a factor
            // check whether it is 
            // divisible by k
            if (($n / $i) % $k == 0)
            {
                $count++;
            }
        }
    }
  
    // If the number is a perfect 
    // square and it is divisible by k
    if (($i * $i == $n) && ($i % $k == 0))
    {
        $count--;
    }
  
    return $count;
}
  
// Driver code
$n = 12;
$k = 3;
  
echo (countDivisors($n, $k));
  
// This code is contributed
// by Shivi_Aggarwal
?>

chevron_right


Output:

3

Time Complexity :$O(\sqrt(n))



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.