Count number of integers less than or equal to N which has exactly 9 divisors - GeeksforGeeks

10 May, 2021
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++
Java
Python 3
C#
PHP
Javascript
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std; // Function to count factors in O(N)int numberOfDivisors(int num){    int c = 0;     // iterate and check if factor or not    for (int i = 1; i <= num; i++) {        if (num % i == 0) {            c += 1;        }    }    return c;} // Function to count numbers having// exactly 9 divisorsint countNumbers(int n){    int c = 0;     // check for all numbers <=N    for (int i = 1; i <= n; i++) {        // check if exactly 9 factors or not        if (numberOfDivisors(i) == 9)            c += 1;    }    return c;} // Driver Codeint main(){    int n = 1000;     cout << countNumbers(n); return 0;}
// Java implementation of above approach import java.io.*; class GFG { // Function to count factors in O(N)static int numberOfDivisors(int num){    int c = 0;     // iterate and check if factor or not    for (int i = 1; i <= num; i++) {        if (num % i == 0) {            c += 1;        }    }    return c;} // Function to count numbers having// exactly 9 divisorsstatic int countNumbers(int n){    int c = 0;     // check for all numbers <=N    for (int i = 1; i <= n; i++) {        // check if exactly 9 factors or not        if (numberOfDivisors(i) == 9)            c += 1;    }    return c;}        // Driver Code    public static void main (String[] args) {    int n = 1000;     System.out.print(countNumbers(n));    }} // This code is contributed by inder_verma..
# Python 3 implementation of# above approach # Function to count factors in O(N)def numberOfDivisors(num):    c = 0     # iterate and check if    # factor or not    for i in range(1, num + 1) :        if (num % i == 0) :            c += 1             return c # Function to count numbers having# exactly 9 divisorsdef countNumbers(n):     c = 0     # check for all numbers <=N    for i in range(1, n + 1) :                 # check if exactly 9 factors or not        if (numberOfDivisors(i) == 9):            c += 1    return c # Driver Codeif __name__ == "__main__":    n = 1000     print(countNumbers(n)) # This code is contributed# by ChitraNayal
// C# implementation of above approachusing System; class GFG{ // Function to count factors in O(N)static int numberOfDivisors(int num){    int c = 0;     // iterate and check if factor or not    for (int i = 1; i <= num; i++)    {        if (num % i == 0)        {            c += 1;        }    }    return c;} // Function to count numbers having// exactly 9 divisorsstatic int countNumbers(int n){    int c = 0;     // check for all numbers <=N    for (int i = 1; i <= n; i++) {        // check if exactly 9 factors or not        if (numberOfDivisors(i) == 9)            c += 1;    }    return c;} // Driver Codepublic static void Main (){int n = 1000; Console.Write(countNumbers(n));}} // This code is contributed// by Akanksha Rai(Abby_akku)
<?php// PHP implementation of above approach // Function to count factors in O(N)Function numberOfDivisors($num){    $c = 0;     // iterate and check    // if factor or not    for ($i = 1; $i <= $num; $i++) {                 if ($num % $i == 0) {            $c += 1;        }    }    return $c;} // Function to count numbers// having exactly 9 divisorsFunction countNumbers($n){    $c = 0;     // check for all numbers <=N    for ($i = 1; $i <= $n; $i++) {                 // check if exactly 9 factors or not        if (numberOfDivisors($i) == 9)            $c += 1;    }    return $c;} // Driver Code$n = 1000; echo countNumbers($n); // This code is contributed// by Shivi_Aggarwal?>
<script>     // Javascript implementation of above approach         // Function to count factors in O(N)    function numberOfDivisors(num)    {        let c = 0;         // iterate and check if factor or not        for (let i = 1; i <= num; i++)        {            if (num % i == 0)            {                c += 1;            }        }        return c;    }     // Function to count numbers having    // exactly 9 divisors    function countNumbers(n)    {        let c = 0;         // check for all numbers <=N        for (let i = 1; i <= n; i++) {            // check if exactly 9 factors or not            if (numberOfDivisors(i) == 9)                c += 1;        }        return c;    }         let n = 1000;       document.write(countNumbers(n));                                  </script>
8
 
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.
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++
Java
Python3
C#
PHP
Javascript
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std; // Function to count numbers having// exactly 9 divisorsint countNumbers(int n){    int c = 0;     int limit = sqrt(n);     // Sieve array    int prime[limit + 1];     // initially prime[i] = i    for (int i = 1; i <= limit; i++)        prime[i] = i;     // use sieve concept to store the    // first prime factor of every number    for (int i = 2; i * i <= limit; i++) {        if (prime[i] == i) {            // mark all factors of i            for (int j = i * i; j <= limit; j += i)                if (prime[j] == j)                    prime[j] = i;        }    }     // check for all numbers if they can be    // expressed in form p*q    for (int i = 2; i <= limit; i++) {        // p prime factor        int p = prime[i];         // q prime factor        int q = prime[i / prime[i]];         // if both prime factors are different        // if p*q<=n and q!=        if (p * q == i && q != 1 && p != q) {            c += 1;        }        else if (prime[i] == i) {             // Check if it can be expressed as p^8            if (pow(i, 8) <= n) {                 c += 1;            }        }    }     return c;} // Driver Codeint main(){    int n = 1000;     cout << countNumbers(n); return 0;}
// Java implementation of above approachpublic class GFG { // Function to count numbers having// exactly 9 divisors    static int countNumbers(int n) {        int c = 0;         int limit = (int) Math.sqrt(n);         // Sieve array        int prime[] = new int[limit + 1];         // initially prime[i] = i        for (int i = 1; i <= limit; i++) {            prime[i] = i;        }         // use sieve concept to store the        // first prime factor of every number        for (int i = 2; i * i <= limit; i++) {            if (prime[i] == i) {                // mark all factors of i                for (int j = i * i; j <= limit; j += i) {                    if (prime[j] == j) {                        prime[j] = i;                    }                }            }        }         // check for all numbers if they can be        // expressed in form p*q        for (int i = 2; i <= limit; i++) {            // p prime factor            int p = prime[i];             // q prime factor            int q = prime[i / prime[i]];             // if both prime factors are different            // if p*q<=n and q!=            if (p * q == i && q != 1 && p != q) {                c += 1;            } else if (prime[i] == i) {                 // Check if it can be expressed as p^8                if (Math.pow(i, 8) <= n) {                     c += 1;                }            }        }         return c;    } // Driver Code    public static void main(String[] args) {        int n = 1000;         System.out.println(countNumbers(n));     }}/*This code is contributed by PrinciRaj1992*/
# Python3 implementation of the above approach # Function to count numbers# having exactly 9 divisorsdef countNumbers(n):         c = 0    limit = int(n ** (0.5))     # Sieve array, initially prime[i] = i    prime = [i for i in range(limit + 1)]         # use sieve concept to store the    # first prime factor of every number    i = 2    while i * i <= limit:        if prime[i] == i:                         # mark all factors of i            for j in range(i * i, limit + 1, i):                if prime[j] == j:                    prime[j] = i                 i += 1     # check for all numbers if they    # can be expressed in form p*q    for i in range(2, limit + 1):                 # p prime factor        p = prime[i]         # q prime factor        q = prime[i // prime[i]]         # if both prime factors are different        # if p*q<=n and q!=        if p * q == i and q != 1 and p != q:            c += 1                 elif prime[i] == i:             # Check if it can be            # expressed as p^8            if i ** 8 <= n:                c += 1         return c # Driver Codeif __name__ == "__main__":     n = 1000    print(countNumbers(n)) # This code is contributed# by Rituraj Jain
// C# implementation of above approachusing System; public class GFG { // Function to count numbers having// exactly 9 divisors    static int countNumbers(int n) {        int c = 0;          int limit = (int) Math.Sqrt(n);          // Sieve array        int []prime = new int[limit + 1];          // initially prime[i] = i        for (int i = 1; i <= limit; i++) {            prime[i] = i;        }          // use sieve concept to store the        // first prime factor of every number        for (int i = 2; i * i <= limit; i++) {            if (prime[i] == i) {                // mark all factors of i                for (int j = i * i; j <= limit; j += i) {                    if (prime[j] == j) {                        prime[j] = i;                    }                }            }        }          // check for all numbers if they can be        // expressed in form p*q        for (int i = 2; i <= limit; i++) {            // p prime factor            int p = prime[i];              // q prime factor            int q = prime[i / prime[i]];              // if both prime factors are different            // if p*q<=n and q!=            if (p * q == i && q != 1 && p != q) {                c += 1;            } else if (prime[i] == i) {                  // Check if it can be expressed as p^8                if (Math.Pow(i, 8) <= n) {                      c += 1;                }            }        }          return c;    }  // Driver Code    public static void Main() {        int n = 1000;          Console.WriteLine(countNumbers(n));      }}/*This code is contributed by PrinciRaj1992*/
<?php// PHP implementation of above approach// Function to count numbers having// exactly 9 divisors function countNumbers($n){    $c = 0;    $limit = sqrt($n);     // Sieve array    $prime[$limit + 1] = array(0);     // initially prime[i] = i    for ($i = 1; $i <= $limit; $i++)        $prime[$i] = $i;     // use sieve concept to store the    // first prime factor of every number    for ($i = 2; $i * $i <= $limit; $i++)    {        if ($prime[$i] == $i)        {            // mark all factors of i            for ($j = $i * $i;                 $j <= $limit; $j += $i)                if ($prime[$j] == $j)                    $prime[$j] = $i;        }    }     // check for all numbers if they    // can be expressed in form p*q    for ($i = 2; $i <= $limit; $i++)    {        // p prime factor        $p = $prime[$i];         // q prime factor        $q = $prime[$i / $prime[$i]];         // if both prime factors are different        // if p*q<=n and q!=        if ($p * $q == $i && $q != 1 && $p != $q)        {            $c += 1;        }        else if ($prime[$i] == $i)        {             // Check if it can be expressed as p^8            if (pow($i, 8) <= $n)            {                 $c += 1;            }        }    }     return $c;} // Driver Code$n = 1000;echo countNumbers($n); // This code is contributed by jit_t?>
<script>    // Javascript implementation of above approach         // Function to count numbers having    // exactly 9 divisors    function countNumbers(n) {        let c = 0;           let limit = parseInt(Math.sqrt(n), 10);           // Sieve array        let prime = new Array(limit + 1);        prime.fill(0);           // initially prime[i] = i        for (let i = 1; i <= limit; i++) {            prime[i] = i;        }           // use sieve concept to store the        // first prime factor of every number        for (let i = 2; i * i <= limit; i++) {            if (prime[i] == i) {                // mark all factors of i                for (let j = i * i; j <= limit; j += i) {                    if (prime[j] == j) {                        prime[j] = i;                    }                }            }        }           // check for all numbers if they can be        // expressed in form p*q        for (let i = 2; i <= limit; i++) {            // p prime factor            let p = prime[i];               // q prime factor            let q = prime[parseInt(i / prime[i], 10)];               // if both prime factors are different            // if p*q<=n and q!=            if (p * q == i && q != 1 && p != q) {                c += 1;            } else if (prime[i] == i) {                   // Check if it can be expressed as p^8                if (Math.pow(i, 8) <= n) {                       c += 1;                }            }        }           return c;    }         let n = 1000;       document.write(countNumbers(n));     </script>
8
 
Time Complexity: O(sqrt(N)) Auxiliary Space: O(sqrt(N)) 
inderDuMCA
Shivi_Aggarwal
ukasp
Akanksha_Rai
jit_t
princiraj1992
rituraj_jain
shubham_singh
divyesh072019
divyeshrabadiya07
Directi
divisors
Prime Number
prime-factor
sieve
Technical Scripter 2018
Competitive Programming
Mathematical
Directi
Mathematical
Prime Number
sieve
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Prefix Sum Array - Implementation and Applications in Competitive Programming
Ordered Set and GNU C++ PBDS
Modulo 10^9+7 (1000000007)
Bits manipulation (Important tactics)
What is Competitive Programming and How to Prepare for It?
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7