Open In App

Find Nth smallest number having exactly 4 divisors

Last Updated : 21 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given a positive integer N, the task is to find the Nth smallest number in the sequence which is having exactly 4 divisors.

Examples:

Input: 4
Output: 14
Explanation: The numbers in the sequence that has 4 divisors are 6, 8, 10, 14, …, the fourth number in the sequence is 14.

Input: 24
Output: 94

 

Approach: This problem can be solved by observing that the number i having a prime factorization of p1a1 * p2a2 * p3a3…pkak, then the number of divisors of i is (a1+1)(a2+1)…(ak+1). So for i to have exactly 4 divisors, it should be equal to the product of two distinct primes or some prime number raised to the power 3. Follow the steps below to solve the problem: 

  • Initialize arrays divs[] and vis[] to store the number of divisors of any number and to check if given number is considered or not respectively.
  • Initialize a variable cnt as 0 to store number of elements having exactly 4 divisors.
  • Now, use the sieve of Eratosthenes algorithm.
  • Iterate while cnt is less than n, using a variable i start from 2:
    • If i is prime:
      • Iterate in the range [2*i, 1000000] using the variable j with increment of i:
        • If the number j is already considered, then continue.
        • Update vis[j] as true and initialize variables currNum as j and count as 0.
        • Divide the currNum by i while currNum % i is equal to 0, increment div[j] and count by 1. 
        • If currNum is equal to 1, count is equal to 3 and divs[j] is equal to 3, then increment cnt by 1.
        • Otherwise, if currNum is not equal to 1, count is equal to 1, divs[j] is equal to 1 and divs[currNum] is equal to 0, then increment cnt by 1.
        • If cnt is equal to N, then print j and return.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the nth number which
// has exactly 4 divisors
int nthNumber(int n)
{
 
    // The divs[] array to store number of
    // divisors of every element
    int divs[1000000];
 
    // The vis[] array to check if given number
    // is considered or not
    bool vis[1000000];
 
    // The cnt stores number of elements having
    // exactly 4 divisors
    int cnt = 0;
 
    // Iterate while cnt less than n
    for (int i = 2; cnt < n; i++) {
 
        // If i is a prime
        if (divs[i] == 0) {
 
            // Iterate in the range [2*i, 1000000] with
            // increment of i
            for (int j = 2 * i; j < 1000000; j += i) {
 
                // If the number j is already considered
                if (vis[j]) {
                    continue;
                }
 
                vis[j] = 1;
 
                int currNum = j;
                int count = 0;
 
                // Dividing currNum by i until currNum % i is
                // equal to 0
                while (currNum % i == 0) {
                    divs[j]++;
                    currNum = currNum / i;
                    count++;
                }
 
                // Case a single prime in its factorization
                if (currNum == 1 && count == 3 && divs[j] == 3) {
                    cnt++;
                }
 
                else if (currNum != 1
                         && divs[currNum] == 0
                         && count == 1
                         && divs[j] == 1) {
                    // Case of two distinct primes which
                    // divides j exactly once each
                    cnt++;
                }
 
                if (cnt == n) {
                    return j;
                }
            }
        }
    }
 
    return -1;
}
 
// Driver Code
int main()
{
    // Given Input
    int N = 24;
 
    // Function Call
    cout << nthNumber(N) << endl;
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
 
class GFG {
 
    // Function to find the nth number which
    // has exactly 4 divisors
    static int nthNumber(int n)
    {
 
        // The divs[] array to store number of
        // divisors of every element
        int divs[] = new int[1000000];
 
        // The vis[] array to check if given number
        // is considered or not
        boolean vis[] = new boolean[1000000];
 
        // The cnt stores number of elements having
        // exactly 4 divisors
        int cnt = 0;
 
        // Iterate while cnt less than n
        for (int i = 2; cnt < n; i++) {
 
            // If i is a prime
            if (divs[i] == 0) {
 
                // Iterate in the range [2*i, 1000000] with
                // increment of i
                for (int j = 2 * i; j < 1000000; j += i) {
 
                    // If the number j is already considered
                    if (vis[j]) {
                        continue;
                    }
 
                    vis[j] = true;
 
                    int currNum = j;
                    int count = 0;
 
                    // Dividing currNum by i until currNum %
                    // i is equal to 0
                    while (currNum % i == 0) {
                        divs[j]++;
                        currNum = currNum / i;
                        count++;
                    }
 
                    // Case a single prime in its
                    // factorization
                    if (currNum == 1 && count == 3
                        && divs[j] == 3) {
                        cnt++;
                    }
 
                    else if (currNum != 1
                             && divs[currNum] == 0
                             && count == 1
                             && divs[j] == 1) {
                        // Case of two distinct primes which
                        // divides j exactly once each
                        cnt++;
                    }
 
                    if (cnt == n) {
                        return j;
                    }
                }
            }
        }
 
        return -1;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given Input
        int N = 24;
 
        // Function Call
        System.out.println(nthNumber(N));
    }
}
 
// This code is contributed by Potta Lokesh


Python3




# Python program for the above approach
 
# Function to find the nth number which
# has exactly 4 divisors
def nthNumber(n):
 
    # The divs[] array to store number of
    # divisors of every element
    divs = [0 for i in range(1000000)];
 
    # The vis[] array to check if given number
    # is considered or not
    vis = [0 for i in range(1000000)];
 
    # The cnt stores number of elements having
    # exactly 4 divisors
    cnt = 0;
 
    # Iterate while cnt less than n
    for i in range(2, n):
 
        # If i is a prime
        if (divs[i] == 0):
 
            # Iterate in the range [2*i, 1000000] with
            # increment of i
            for j in range(2 * i, 1000000):
 
                # If the number j is already considered
                if (vis[j]):
                    continue;
 
                vis[j] = 1;
 
                currNum = j;
                count = 0;
 
                # Dividing currNum by i until currNum % i is
                # equal to 0
                while (currNum % i == 0):
                    divs[j] += 1;
                    currNum = currNum // i;
                    count += 1;
     
                # Case a single prime in its factorization
                if (currNum == 1 and count == 3 and divs[j] == 3):
                    cnt += 1
                elif (currNum != 1
                    and divs[currNum] == 0
                    and count == 1
                    and divs[j] == 1):
                 
                    # Case of two distinct primes which
                    # divides j exactly once each
                    cnt += 1
 
                if (cnt == n):
                    return j;
 
    return -1;
 
# Driver Code
 
# Given Input
N = 24;
 
# Function Call
print(nthNumber(N));
 
# This code is contributed by gfgking.


C#




// C# program for the above approach
using System;
 
// Function to find minimum number of
// elements required to obtain sum K
class GFG{
 
// Function to find the nth number which
// has exactly 4 divisors
static int nthNumber(int n)
{
     
    // The divs[] array to store number of
    // divisors of every element
    int[] divs = new int[1000000];
 
    // The vis[] array to check if given number
    // is considered or not
    int[] vis  = new int[1000000];
 
    // The cnt stores number of elements having
    // exactly 4 divisors
    int cnt = 0;
 
    // Iterate while cnt less than n
    for(int i = 2; cnt < n; i++)
    {
         
        // If i is a prime
        if (divs[i] == 0)
        {
             
            // Iterate in the range [2*i, 1000000] with
            // increment of i
            for(int j = 2 * i; j < 1000000; j += i)
            {
                 
                // If the number j is already considered
                if (vis[j] != 0)
                {
                    continue;
                }
 
                vis[j] = 1;
 
                int currNum = j;
                int count = 0;
 
                // Dividing currNum by i until currNum % i is
                // equal to 0
                while (currNum % i == 0)
                {
                    divs[j]++;
                    currNum = currNum / i;
                    count++;
                }
 
                // Case a single prime in its factorization
                if (currNum == 1 && count == 3 && divs[j] == 3)
                {
                    cnt++;
                }
 
                else if (currNum != 1 && divs[currNum] == 0 &&
                           count == 1 && divs[j] == 1)
                {
                     
                    // Case of two distinct primes which
                    // divides j exactly once each
                    cnt++;
                }
 
                if (cnt == n)
                {
                    return j;
                }
            }
        }
    }
    return -1;
}
 
// Driver Code
static public void Main ()
{
     
    // Given Input
    int N = 24;
 
    // Function Call
    Console.Write(nthNumber(N));
}
}
 
// This code is contributed by sanjoy_62


Javascript




<script>
 
// JavaScript program for the above approach
 
// Function to find the nth number which
// has exactly 4 divisors
function nthNumber(n)
{
     
    // The divs[] array to store number of
    // divisors of every element
    let divs = new Array(1000000);
    for(var i = 0; i < divs.length; i++)
    {
        divs[i] = 0;
    }
     
    // The vis[] array to check if given number
    // is considered or not
    let vis = new Array(1000000);
    for(var i = 0; i < vis.length; i++)
    {
        vis[i] = 0;
    }
 
    // The cnt stores number of elements having
    // exactly 4 divisors
    let cnt = 0;
 
    // Iterate while cnt less than n
    for(let i = 2; cnt < n; i++)
    {
         
        // If i is a prime
        if (divs[i] == 0)
        {
             
            // Iterate in the range [2*i, 1000000] with
            // increment of i
            for(let j = 2 * i; j < 1000000; j += i)
            {
                 
                // If the number j is already considered
                if (vis[j])
                {
                    continue;
                }
 
                vis[j] = true;
 
                let currNum = j;
                let count = 0;
 
                // Dividing currNum by i until currNum %
                // i is equal to 0
                while (currNum % i == 0)
                {
                    divs[j]++;
                    currNum = Math.floor(currNum / i);
                    count++;
                }
 
                // Case a single prime in its
                // factorization
                if (currNum == 1 && count == 3 &&
                    divs[j] == 3)
                {
                    cnt++;
                }
 
                else if (currNum != 1 && divs[currNum] == 0 &&
                           count == 1 && divs[j] == 1)
                {
                     
                    // Case of two distinct primes which
                    // divides j exactly once each
                    cnt++;
                }
 
                if (cnt == n)
                {
                    return j;
                }
            }
        }
    }
    return -1;
}
 
// Driver Code
 
// Given Input
let N = 24;
 
// Function Call
document.write(nthNumber(N));
 
// This code is contributed by code_hunt
 
</script>


Output

94

Time Complexity: O(Nlog(log(N))), where N is 1000000.
Auxiliary Space: O(N)



Similar Reads

Maximum possible prime divisors that can exist in numbers having exactly N divisors
Given an integer N which denotes the number of divisors of any number, the task is to find the maximum prime divisors that are possible in number having N divisors. Examples: Input: N = 4 Output: 2 Input: N = 8 Output: 3 Naive Approach: In this approach, the idea is to generate all the numbers having exactly N divisors and check for the maximum num
11 min read
Find Nth smallest number that is divisible by 100 exactly K times
Given two numbers N and K . The task is to find N'th smallest number that is divided by 100 exactly K times.Examples: Input : N = 12, K = 2 Output : 120000 120000 is divisible by 100 exactly 2 times and is the 12 th smallest number also.Input : N = 1000, K = 2 Output : 10010000 Approach: First, find the smallest number that is divisible by 100 exac
5 min read
Count array elements having exactly K divisors
Given an array arr[] consisting of N integers and an integer K, the task is to count the number of array elements having exactly K divisors. Examples: Input: N = 5, arr[] = { 3, 6, 2, 9, 4 }, K = 2Output: 2Explanation: arr[0] (= 3) and arr[2] (= 2) have exactly 2 divisors. Input: N = 5, arr[] = { 3, 6, 2, 9, 4 }, K = 3Output: 2Explanation: arr[3] (
14 min read
Count numbers up to N having exactly 5 divisors
Given a positive integer N, the task is to count the number of integers from the range [1, N] having exactly 5 divisors. Examples: Input: N = 18Output: 1Explanation:From all the integers over the range [1, 18], 16 is the only integer that has exactly 5 divisors, i.e. 1, 2, 8, 4 and 16.Therefore, the count of such integers is 1. Input: N = 100Output
31 min read
Find sum of inverse of the divisors when sum of divisors and the number is given
Given an integer N and the sum of its divisors. The task is to find the sum of the inverse of the divisors of N.Examples: Input: N = 6, Sum = 12 Output: 2.00 Divisors of N are {1, 2, 3, 6} Sum of inverse of divisors is equal to (1/1 + 1/2 + 1/3 + 1/6) = 2.0Input: N = 9, Sum = 13 Output: 1.44 Naive Approach: Calculate all the divisors of the given i
3 min read
Find sum of divisors of all the divisors of a natural number
Given a natural number n, the task is to find sum of divisors of all the divisors of n. Examples: Input : n = 54 Output : 232 Divisors of 54 = 1, 2, 3, 6, 9, 18, 27, 54. Sum of divisors of 1, 2, 3, 6, 9, 18, 27, 54 are 1, 3, 4, 12, 13, 39, 40, 120 respectively. Sum of divisors of all the divisors of 54 = 1 + 3 + 4 + 12 + 13 + 39 + 40 + 120 = 232. I
7 min read
Smallest number having only 4 divisors with difference between any two at most D
Given the number D, find the smallest number N such that it has exactly four divisors and the difference between any two of them is greater than or equal to D. Examples: Input: 1Output: 6Explanation: 6 has four divisors 1, 2, 3, and 6. Difference between any two of them is always greater or equal to 1. Input: 2Output: 15Explanation: 15 has four div
6 min read
Check if a number has an odd count of odd divisors and even count of even divisors
Given an integer N, the task is to check if N has an odd number of odd divisors and even number of even divisors. Examples: Input: N = 36Output: YesExplanation:Divisors of 36 = 1, 2, 3, 4, 6, 9, 12, 18, 36Count of Odd Divisors(1, 3, 9) = 3 [Odd]Count of Even Divisors(2, 4, 6, 12, 18, 36) = 6 [Even] Input: N = 28Output: No Naive Approach: The idea i
9 min read
Smallest integer having at least K prime divisors with difference between each factor at least D
Given two integers D and K. The task is to find the smallest number N which has at least K prime divisors and the difference between each pair of divisors is at least D. Examples Input: D = 3, K = 2Output: 55Explanation: It is smallest number which has 4 divisors 1 and 2 prime divisors 5, 11 and their difference between any of the pair is D. Input:
7 min read
Program to find the Nth natural number with exactly two bits set
Given an integer N, the task is to find the Nth natural number with exactly two bits set. Examples: Input: N = 4 Output: 9 Explanation: Numbers with exactly two bits set: 3, 5, 6, 9, 10, 12, ... 4th number in this is 9Input: N = 15 Output: 48 Naive Approach: Run a loop through all natural numbers, and for each number, check if it has two bits set o
4 min read