Count the numbers < N which have equal number of divisors as K
Last Updated :
14 Mar, 2023
Given two integers N and K, the task is to count all the numbers < N which have equal number of positive divisors as K.
Examples:
Input: n = 10, k = 5
Output: 3
2, 3 and 7 are the only numbers < 10 which have 2 divisors (equal to the number of divisors of 5)
Input: n = 500, k = 6
Output: 148
Approach:
- Compute the number of divisor of each number < N and store the result in an array where arr[i] contains the number of divisors of i.
- Traverse arr[], if arr[i] = arr[K] then update count = count + 1.
- Print the value of count in the end.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countDivisors( int n)
{
int x = 0, ans = 1;
while (n % 2 == 0) {
x++;
n = n / 2;
}
ans = ans * (x + 1);
for ( int i = 3; i <= sqrt (n); i = i + 2) {
x = 0;
while (n % i == 0) {
x++;
n = n / i;
}
ans = ans * (x + 1);
}
if (n > 2)
ans = ans * 2;
return ans;
}
int getTotalCount( int n, int k)
{
int k_count = countDivisors(k);
int count = 0;
for ( int i = 1; i < n; i++)
if (k_count == countDivisors(i))
count++;
if (k < n)
count = count - 1;
return count;
}
int main()
{
int n = 500, k = 6;
cout << getTotalCount(n, k);
return 0;
}
|
Java
public class GFG{
static int countDivisors( int n)
{
int x = 0 , ans = 1 ;
while (n % 2 == 0 ) {
x++;
n = n / 2 ;
}
ans = ans * (x + 1 );
for ( int i = 3 ; i <= Math.sqrt(n); i = i + 2 ) {
x = 0 ;
while (n % i == 0 ) {
x++;
n = n / i;
}
ans = ans * (x + 1 );
}
if (n > 2 )
ans = ans * 2 ;
return ans;
}
static int getTotalCount( int n, int k)
{
int k_count = countDivisors(k);
int count = 0 ;
for ( int i = 1 ; i < n; i++)
if (k_count == countDivisors(i))
count++;
if (k < n)
count = count - 1 ;
return count;
}
public static void main(String []args)
{
int n = 500 , k = 6 ;
System.out.println(getTotalCount(n, k));
}
}
|
Python3
def countDivisors(n):
x, ans = 0 , 1
while (n % 2 = = 0 ):
x + = 1
n = n / 2
ans = ans * (x + 1 )
for i in range ( 3 , int (n * * 1 / 2 ) + 1 , 2 ):
x = 0
while (n % i = = 0 ):
x + = 1
n = n / i
ans = ans * (x + 1 )
if (n > 2 ):
ans = ans * 2
return ans
def getTotalCount(n, k):
k_count = countDivisors(k)
count = 0
for i in range ( 1 , n):
if (k_count = = countDivisors(i)):
count + = 1
if (k < n):
count = count - 1
return count
if __name__ = = '__main__' :
n, k = 500 , 6
print (getTotalCount(n, k))
|
C#
using System;
public class GFG{
static int countDivisors( int n)
{
int x = 0, ans = 1;
while (n % 2 == 0) {
x++;
n = n / 2;
}
ans = ans * (x + 1);
for ( int i = 3; i <= Math.Sqrt(n); i = i + 2) {
x = 0;
while (n % i == 0) {
x++;
n = n / i;
}
ans = ans * (x + 1);
}
if (n > 2)
ans = ans * 2;
return ans;
}
static int getTotalCount( int n, int k)
{
int k_count = countDivisors(k);
int count = 0;
for ( int i = 1; i < n; i++)
if (k_count == countDivisors(i))
count++;
if (k < n)
count = count - 1;
return count;
}
public static void Main()
{
int n = 500, k = 6;
Console.WriteLine(getTotalCount(n, k));
}
}
|
PHP
<?php
function countDivisors( $n )
{
$x = 0;
$ans = 1;
while ( $n % 2 == 0) {
$x ++;
$n = $n / 2;
}
$ans = $ans * ( $x + 1);
for ( $i = 3; $i <= sqrt( $n ); $i = $i + 2) {
$x = 0;
while ( $n % $i == 0) {
$x ++;
$n = $n / $i ;
}
$ans = $ans * ( $x + 1);
}
if ( $n > 2)
$ans = $ans * 2;
return $ans ;
}
function getTotalCount( $n , $k )
{
$k_count = countDivisors( $k );
$count = 0;
for ( $i = 1; $i < $n ; $i ++)
if ( $k_count == countDivisors( $i ))
$count ++;
if ( $k < $n )
$count = $count - 1;
return $count ;
}
$n = 500;
$k = 6;
echo getTotalCount( $n , $k );
#This code is contributed by Sachin..
?>
|
Javascript
<script>
function countDivisors(n)
{
var x = 0, ans = 1;
while (n % 2 == 0) {
x++;
n = n / 2;
}
ans = ans * (x + 1);
for ( var i = 3; i <= Math.sqrt(n); i = i + 2)
{
x = 0;
while (n % i == 0) {
x++;
n = n / i;
}
ans = ans * (x + 1);
}
if (n > 2)
ans = ans * 2;
return ans;
}
function getTotalCount( n, k)
{
var k_count = countDivisors(k);
var count = 0;
for ( var i = 1; i < n; i++)
if (k_count == countDivisors(i))
count++;
if (k < n)
count = count - 1;
return count;
}
var n = 500, k = 6;
document.write(getTotalCount(n, k));
</script>
|
Complexity Analysis:
- Time Complexity: O(n * sqrt(k)), where n is the maximum number to check for divisors and sqrt(k) is the maximum number of iterations for the loop that checks divisors of n.
- Auxiliary Space: O(1)
Optimization:
The above solution can be optimized using Sieve technique. Please refer Count number of integers less than or equal to N which has exactly 9 divisors for details.
Share your thoughts in the comments
Please Login to comment...