Find numbers with K odd divisors in a given range
Last Updated :
23 Jun, 2022
Given two numbers a and b, and a number k which is odd. The task is to find all the numbers between a and b (both inclusive) having exactly k divisors.
Examples:
Input : a = 2, b = 49, k = 3
Output: 4
// Between 2 and 49 there are four numbers
// with three divisors
// 4 (Divisors 1, 2, 4), 9 (Divisors 1, 3, 9),
// 25 (Divisors 1, 5, 25} and 49 (1, 7 and 49)
Input : a = 1, b = 100, k = 9
Output: 2
// between 1 and 100 there are 36 (1, 2, 3, 4, 6, 9, 12, 18, 36)
// and 100 (1, 2, 4, 5, 10, 20, 25, 50, 100) having exactly 9
// divisors
This problem has simple solution, here we are given that k is odd and we know that only perfect square numbers have odd number of divisors , so we just need to check all perfect square numbers between a and b, and calculate divisors of only those perfect square numbers.
C++
#include<bits/stdc++.h>
using namespace std;
bool isPerfect( int n)
{
int s = sqrt (n);
return (s*s == n);
}
int divisorsCount( int n)
{
int count=0;
for ( int i=1; i<= sqrt (n)+1; i++)
{
if (n%i==0)
{
if (n/i == i)
count += 1;
else
count += 2;
}
}
return count;
}
int kDivisors( int a, int b, int k)
{
int count = 0;
for ( int i=a; i<=b; i++)
{
if (isPerfect(i))
if (divisors(i) == k)
count++;
}
return count;
}
int main()
{
int a = 2, b = 49, k = 3;
cout << kDivisors(a, b, k);
return 0;
}
|
Java
import java.io.*;
import java.math.*;
class GFG {
static boolean isPerfect( int n)
{
int s = ( int )(Math.sqrt(n));
return (s*s == n);
}
static int divisorsCount( int n)
{
int count= 0 ;
for ( int i = 1 ; i <= Math.sqrt(n) + 1 ; i++)
{
if (n % i == 0 )
{
if (n / i == i)
count += 1 ;
else
count += 2 ;
}
}
return count;
}
static int kDivisors( int a, int b, int k)
{
int count = 0 ;
for ( int i = a; i <= b; i++)
{
if (isPerfect(i))
if (divisorsCount(i) == k)
count++;
}
return count;
}
public static void main(String args[])
{
int a = 21 , b = 149 , k = 333 ;
System.out.println(kDivisors(a, b, k));
}
}
|
Python3
import math
def isPerfect(n) :
s = math.sqrt(n)
return (s * s = = n)
def divisorsCount(n) :
count = 0
for i in range ( 1 , ( int )(math.sqrt(n) + 2 )) :
if (n % i = = 0 ) :
if (n / / i = = i) :
count = count + 1
else :
count = count + 2
return count
def kDivisors(a, b, k) :
count = 0
for i in range (a, b + 1 ) :
if (isPerfect(i)) :
if (divisorsCount(i) = = k) :
count = count + 1
return count
a = 2
b = 49
k = 3
print (kDivisors(a, b, k))
|
C#
using System;
class GFG {
static bool isPerfect( int n)
{
int s = ( int )(Math.Sqrt(n));
return (s * s == n);
}
static int divisorsCount( int n)
{
int count=0;
for ( int i = 1; i <= Math.Sqrt(n) + 1; i++)
{
if (n % i == 0)
{
if (n / i == i)
count += 1;
else
count += 2;
}
}
return count;
}
static int kDivisors( int a, int b,
int k)
{
int count = 0;
for ( int i = a; i <= b; i++)
{
if (isPerfect(i))
if (divisorsCount(i) == k)
count++;
}
return count;
}
public static void Main(String []args)
{
int a = 21, b = 149, k = 333;
Console.Write(kDivisors(a, b, k));
}
}
|
PHP
<?php
function isPerfect( $n )
{
$s = sqrt( $n );
return ( $s * $s == $n );
}
function divisorsCount( $n )
{
$count = 0;
for ( $i = 1; $i <= sqrt( $n ) + 1; $i ++)
{
if ( $n % $i == 0)
{
if ( $n / $i == $i )
$count += 1;
else
$count += 2;
}
}
return $count ;
}
function kDivisors( $a , $b , $k )
{
$count = 0;
for ( $i = $a ; $i <= $b ; $i ++)
{
if (isPerfect( $i ))
if (divisorsCount( $i ) == $k )
$count ++;
}
return $count ;
}
$a = 2;
$b = 49;
$k = 3;
echo kDivisors( $a , $b , $k );
?>
|
Javascript
<script>
function isPerfect(n)
{
var s = parseInt((Math.sqrt(n)));
return (s * s == n);
}
function divisorsCount(n)
{
var count=0;
for ( var i = 1; i <= parseInt(Math.sqrt(n)) + 1; i++)
{
if (n % i == 0)
{
if (parseInt(n / i) == i)
count += 1;
else
count += 2;
}
}
return count;
}
function kDivisors(a, b, k)
{
var count = 0;
for ( var i = a; i <= b; i++)
{
if (isPerfect(i))
{
if (divisorsCount(i)==k)
{
count++;
}
}
}
return count;
}
var a = 2, b = 49, k = 3;
document.write(kDivisors(a, b, k));
</script>
|
Output:
4
Time Complexity: O(nsqrtn) , where n is the range of a and b
Auxiliary Space: O(1)
This problem can be solved more efficiently. Please refer method 2 of below post for an efficient solution.
Number of perfect squares between two given numbers
Share your thoughts in the comments
Please Login to comment...