Given a number N(1<=N<=109), the task is to find the total number of integers less than equal to n which have exactly 9 divisors.
Examples:
Input: N = 100
Output: 2
The two numbers which have exactly 9 divisors are 36 and 100.
Input: N = 1000
Output: 8
The numbers are 36 100 196 225 256 441 484 676
A naive approach is to iterate for all numbers till N and count the numbers that have exactly 9 divisors. For counting the number of divisors, one can easily iterate till N and check if N is divisible by i or not and keep a count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int numberOfDivisors( int num)
{
int c = 0;
for ( int i = 1; i <= num; i++) {
if (num % i == 0) {
c += 1;
}
}
return c;
}
int countNumbers( int n)
{
int c = 0;
for ( int i = 1; i <= n; i++) {
if (numberOfDivisors(i) == 9)
c += 1;
}
return c;
}
int main()
{
int n = 1000;
cout << countNumbers(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int numberOfDivisors( int num)
{
int c = 0 ;
for ( int i = 1 ; i <= num; i++) {
if (num % i == 0 ) {
c += 1 ;
}
}
return c;
}
static int countNumbers( int n)
{
int c = 0 ;
for ( int i = 1 ; i <= n; i++) {
if (numberOfDivisors(i) == 9 )
c += 1 ;
}
return c;
}
public static void main (String[] args) {
int n = 1000 ;
System.out.print(countNumbers(n));
}
}
|
Python 3
def numberOfDivisors(num):
c = 0
for i in range ( 1 , num + 1 ) :
if (num % i = = 0 ) :
c + = 1
return c
def countNumbers(n):
c = 0
for i in range ( 1 , n + 1 ) :
if (numberOfDivisors(i) = = 9 ):
c + = 1
return c
if __name__ = = "__main__" :
n = 1000
print (countNumbers(n))
|
C#
using System;
class GFG
{
static int numberOfDivisors( int num)
{
int c = 0;
for ( int i = 1; i <= num; i++)
{
if (num % i == 0)
{
c += 1;
}
}
return c;
}
static int countNumbers( int n)
{
int c = 0;
for ( int i = 1; i <= n; i++) {
if (numberOfDivisors(i) == 9)
c += 1;
}
return c;
}
public static void Main ()
{
int n = 1000;
Console.Write(countNumbers(n));
}
}
|
PHP
<?php
Function numberOfDivisors( $num )
{
$c = 0;
for ( $i = 1; $i <= $num ; $i ++) {
if ( $num % $i == 0) {
$c += 1;
}
}
return $c ;
}
Function countNumbers( $n )
{
$c = 0;
for ( $i = 1; $i <= $n ; $i ++) {
if (numberOfDivisors( $i ) == 9)
$c += 1;
}
return $c ;
}
$n = 1000;
echo countNumbers( $n );
?>
|
Javascript
<script>
function numberOfDivisors(num)
{
let c = 0;
for (let i = 1; i <= num; i++)
{
if (num % i == 0)
{
c += 1;
}
}
return c;
}
function countNumbers(n)
{
let c = 0;
for (let i = 1; i <= n; i++) {
if (numberOfDivisors(i) == 9)
c += 1;
}
return c;
}
let n = 1000;
document.write(countNumbers(n));
</script>
|
Time Complexity: O(N2), as we are using a loop to traverse N times and in each traversal we are calling the function numberofDivisors which will cost O (N) in worst case, hence the complexity of the program will be O(N*N).
Auxiliary Space: O(1), as we are not using any extra space.
An efficient approach is to use the property of the prime factor to count the number of divisors of a number. The method can be found here. If any number(let x) can be expressed in terms of (p^2 * q^2) or (p^8), where p and q are prime factors of X, then X has a total of 9 divisors. The below steps can be followed to solve the above problem.
- Use Sieve technique to mark the smallest prime factor of a number.
- We just need to check for all the numbers in the range[1-sqrt(n)] that can be expressed in terms of p*q since (p^2*q^2) has 9 factors, hence (p*q)^2 will also have exactly 9 factors.
- Iterate from 1 to sqrt(n) and check if i can be expressed as p*q, where p and q are prime numbers.
- Also, check if i is prime then pow(i, 8)<=n or not, in that case, count that number also.
- The summation of the count of numbers that can be expressed in the form p*q and p^8 is our answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countNumbers( int n)
{
int c = 0;
int limit = sqrt (n);
int prime[limit + 1];
for ( int i = 1; i <= limit; i++)
prime[i] = i;
for ( int i = 2; i * i <= limit; i++) {
if (prime[i] == i) {
for ( int j = i * i; j <= limit; j += i)
if (prime[j] == j)
prime[j] = i;
}
}
for ( int i = 2; i <= limit; i++) {
int p = prime[i];
int q = prime[i / prime[i]];
if (p * q == i && q != 1 && p != q) {
c += 1;
}
else if (prime[i] == i) {
if ( pow (i, 8) <= n) {
c += 1;
}
}
}
return c;
}
int main()
{
int n = 1000;
cout << countNumbers(n);
return 0;
}
|
Java
public class GFG {
static int countNumbers( int n) {
int c = 0 ;
int limit = ( int ) Math.sqrt(n);
int prime[] = new int [limit + 1 ];
for ( int i = 1 ; i <= limit; i++) {
prime[i] = i;
}
for ( int i = 2 ; i * i <= limit; i++) {
if (prime[i] == i) {
for ( int j = i * i; j <= limit; j += i) {
if (prime[j] == j) {
prime[j] = i;
}
}
}
}
for ( int i = 2 ; i <= limit; i++) {
int p = prime[i];
int q = prime[i / prime[i]];
if (p * q == i && q != 1 && p != q) {
c += 1 ;
} else if (prime[i] == i) {
if (Math.pow(i, 8 ) <= n) {
c += 1 ;
}
}
}
return c;
}
public static void main(String[] args) {
int n = 1000 ;
System.out.println(countNumbers(n));
}
}
|
Python3
def countNumbers(n):
c = 0
limit = int (n * * ( 0.5 ))
prime = [i for i in range (limit + 1 )]
i = 2
while i * i < = limit:
if prime[i] = = i:
for j in range (i * i, limit + 1 , i):
if prime[j] = = j:
prime[j] = i
i + = 1
for i in range ( 2 , limit + 1 ):
p = prime[i]
q = prime[i / / prime[i]]
if p * q = = i and q ! = 1 and p ! = q:
c + = 1
elif prime[i] = = i:
if i * * 8 < = n:
c + = 1
return c
if __name__ = = "__main__" :
n = 1000
print (countNumbers(n))
|
C#
using System;
public class GFG {
static int countNumbers( int n) {
int c = 0;
int limit = ( int ) Math.Sqrt(n);
int []prime = new int [limit + 1];
for ( int i = 1; i <= limit; i++) {
prime[i] = i;
}
for ( int i = 2; i * i <= limit; i++) {
if (prime[i] == i) {
for ( int j = i * i; j <= limit; j += i) {
if (prime[j] == j) {
prime[j] = i;
}
}
}
}
for ( int i = 2; i <= limit; i++) {
int p = prime[i];
int q = prime[i / prime[i]];
if (p * q == i && q != 1 && p != q) {
c += 1;
} else if (prime[i] == i) {
if (Math.Pow(i, 8) <= n) {
c += 1;
}
}
}
return c;
}
public static void Main() {
int n = 1000;
Console.WriteLine(countNumbers(n));
}
}
|
PHP
<?php
function countNumbers( $n )
{
$c = 0;
$limit = sqrt( $n );
$prime [ $limit + 1] = array (0);
for ( $i = 1; $i <= $limit ; $i ++)
$prime [ $i ] = $i ;
for ( $i = 2; $i * $i <= $limit ; $i ++)
{
if ( $prime [ $i ] == $i )
{
for ( $j = $i * $i ;
$j <= $limit ; $j += $i )
if ( $prime [ $j ] == $j )
$prime [ $j ] = $i ;
}
}
for ( $i = 2; $i <= $limit ; $i ++)
{
$p = $prime [ $i ];
$q = $prime [ $i / $prime [ $i ]];
if ( $p * $q == $i && $q != 1 && $p != $q )
{
$c += 1;
}
else if ( $prime [ $i ] == $i )
{
if (pow( $i , 8) <= $n )
{
$c += 1;
}
}
}
return $c ;
}
$n = 1000;
echo countNumbers( $n );
?>
|
Javascript
<script>
function countNumbers(n) {
let c = 0;
let limit = parseInt(Math.sqrt(n), 10);
let prime = new Array(limit + 1);
prime.fill(0);
for (let i = 1; i <= limit; i++) {
prime[i] = i;
}
for (let i = 2; i * i <= limit; i++) {
if (prime[i] == i) {
for (let j = i * i; j <= limit; j += i) {
if (prime[j] == j) {
prime[j] = i;
}
}
}
}
for (let i = 2; i <= limit; i++) {
let p = prime[i];
let q = prime[parseInt(i / prime[i], 10)];
if (p * q == i && q != 1 && p != q) {
c += 1;
} else if (prime[i] == i) {
if (Math.pow(i, 8) <= n) {
c += 1;
}
}
}
return c;
}
let n = 1000;
document.write(countNumbers(n));
</script>
|
Time Complexity: O(N), as we are using nested loops where the outer loop traverses sqrt(N) times and the inner loop also traverses sqrt(N) time in the worst case so the effective time complexity of the program will be O(N)
Auxiliary Space: O(sqrt(N)), as we are using extra space for the prime array.