Number of divisors of a given number N which are divisible by K
Last Updated :
15 Dec, 2023
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 √(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++
#include <iostream>
using namespace std;
int countDivisors( int n, int k)
{
int count = 0, i;
for (i = 1; i <= n; i++) {
if (n % i == 0 && i % k == 0) {
count++;
}
}
return count;
}
int main()
{
int n = 12, k = 3;
cout << countDivisors(n, k);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countDivisors( int n, int k)
{
int count = 0 , i;
for (i = 1 ; i <= n; i++) {
if (n % i == 0 && i % k == 0 ) {
count++;
}
}
return count;
}
public static void main (String[] args) {
int n = 12 , k = 3 ;
System.out.println(countDivisors(n, k));
}
}
|
Python3
def countDivisors(n, k) :
count = 0
for i in range ( 1 , n + 1 ) :
if (n % i = = 0 and i % k = = 0 ) :
count + = 1
return count
if __name__ = = "__main__" :
n, k = 12 , 3
print (countDivisors(n, k))
|
C#
using System;
class GFG
{
static int countDivisors( int n, int k)
{
int count = 0, i;
for (i = 1; i <= n; i++)
{
if (n % i == 0 && i % k == 0)
{
count++;
}
}
return count;
}
public static void Main ()
{
int n = 12, k = 3;
Console.WriteLine(countDivisors(n, k));
}
}
|
Javascript
<script>
function countDivisors(n, k)
{
var count = 0, i;
for (i = 1; i <= n; i++) {
if (n % i == 0 && i % k == 0) {
count++;
}
}
return count;
}
var n = 12, k = 3;
document.write(countDivisors(n, k));
</script>
|
PHP
<?php
function countDivisors( $n , $k )
{
$count = 0;
for ( $i = 1; $i <= $n ; $i ++)
{
if ( $n % $i == 0 && $i % $k == 0)
{
$count ++;
}
}
return $count ;
}
$n = 12; $k = 3;
echo countDivisors( $n , $k );
|
Time Complexity : O(N)
Auxiliary Space: O(1)
Efficient Approach: The idea is to run a loop from 1 to < √(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++
#include <bits/stdc++.h>
using namespace std;
int countDivisors( int n, int k)
{
int count = 0, i;
for (i = 1; i <= sqrt (n); i++)
{
if (n % i == 0)
{
if (i % k == 0)
{
count++;
}
if ((n / i) % k == 0)
{
count++;
}
}
}
i--;
if ((i * i == n) && (i % k == 0))
{
count--;
}
return count;
}
int main()
{
int n = 16, k = 4;
cout << countDivisors(n, k);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countDivisors( int n, int k)
{
int count = 0 , i;
for (i = 1 ; i <= Math.sqrt(n); i++)
{
if (n % i == 0 )
{
if (i % k == 0 )
{
count++;
}
if ((n / i) % k == 0 )
{
count++;
}
}
}
i--;
if ((i * i == n) && (i % k == 0 ))
{
count--;
}
return count;
}
public static void main (String[] args)
{
int n = 16 , k = 4 ;
System.out.println( countDivisors(n, k));
}
}
|
Python 3
import math
def countDivisors(n, k):
count = 0
for i in range ( 1 , int (math.sqrt(n)) + 1 ):
if (n % i = = 0 ) :
if (i % k = = 0 ) :
count + = 1
if ((n / / i) % k = = 0 ) :
count + = 1
if ((i * i = = n) and (i % k = = 0 )) :
count - = 1
return count
if __name__ = = "__main__" :
n = 16
k = 4
print (countDivisors(n, k))
|
C#
using System;
class GFG
{
static int countDivisors( int n, int k)
{
int count = 0, i;
for (i = 1; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
if (i % k == 0)
{
count++;
}
if ((n / i) % k == 0)
{
count++;
}
}
}
i--;
if ((i * i == n) && (i % k == 0))
{
count--;
}
return count;
}
static public void Main ()
{
int n = 16, k = 4;
Console.WriteLine( countDivisors(n, k));
}
}
|
Javascript
<script>
function countDivisors(n, k)
{
let count = 0, i;
for (i = 1; i <= Math.sqrt(n); i++)
{
if (n % i == 0)
{
if (i % k == 0)
{
count++;
}
if ((n / i) % k == 0)
{
count++;
}
}
}
i--;
if ((i * i == n) && (i % k == 0))
{
count--;
}
return count;
}
let n = 16, k = 4;
document.write( countDivisors(n, k));
</script>
|
PHP
<?php
function countDivisors( $n , $k )
{
$count = 0;
for ( $i = 1; $i <= sqrt( $n ); $i ++)
{
if ( $n % $i == 0)
{
if ( $i % $k == 0)
{
$count ++;
}
if (( $n / $i ) % $k == 0)
{
$count ++;
}
}
}
i--;
if (( $i * $i == $n ) && ( $i % $k == 0))
{
$count --;
}
return $count ;
}
$n = 16;
$k = 4;
echo (countDivisors( $n , $k ));
?>
|
Time Complexity: O(√(n))
Auxiliary Space: O(1) as using constant space
Share your thoughts in the comments
Please Login to comment...