Find Nth smallest number having exactly 4 divisors
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++
#include <bits/stdc++.h>
using namespace std;
int nthNumber( int n)
{
int divs[1000000];
bool vis[1000000];
int cnt = 0;
for ( int i = 2; cnt < n; i++) {
if (divs[i] == 0) {
for ( int j = 2 * i; j < 1000000; j += i) {
if (vis[j]) {
continue ;
}
vis[j] = 1;
int currNum = j;
int count = 0;
while (currNum % i == 0) {
divs[j]++;
currNum = currNum / i;
count++;
}
if (currNum == 1 && count == 3 && divs[j] == 3) {
cnt++;
}
else if (currNum != 1
&& divs[currNum] == 0
&& count == 1
&& divs[j] == 1) {
cnt++;
}
if (cnt == n) {
return j;
}
}
}
}
return -1;
}
int main()
{
int N = 24;
cout << nthNumber(N) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int nthNumber( int n)
{
int divs[] = new int [ 1000000 ];
boolean vis[] = new boolean [ 1000000 ];
int cnt = 0 ;
for ( int i = 2 ; cnt < n; i++) {
if (divs[i] == 0 ) {
for ( int j = 2 * i; j < 1000000 ; j += i) {
if (vis[j]) {
continue ;
}
vis[j] = true ;
int currNum = j;
int count = 0 ;
while (currNum % i == 0 ) {
divs[j]++;
currNum = currNum / i;
count++;
}
if (currNum == 1 && count == 3
&& divs[j] == 3 ) {
cnt++;
}
else if (currNum != 1
&& divs[currNum] == 0
&& count == 1
&& divs[j] == 1 ) {
cnt++;
}
if (cnt == n) {
return j;
}
}
}
}
return - 1 ;
}
public static void main(String[] args)
{
int N = 24 ;
System.out.println(nthNumber(N));
}
}
|
Python3
def nthNumber(n):
divs = [ 0 for i in range ( 1000000 )];
vis = [ 0 for i in range ( 1000000 )];
cnt = 0 ;
for i in range ( 2 , n):
if (divs[i] = = 0 ):
for j in range ( 2 * i, 1000000 ):
if (vis[j]):
continue ;
vis[j] = 1 ;
currNum = j;
count = 0 ;
while (currNum % i = = 0 ):
divs[j] + = 1 ;
currNum = currNum / / i;
count + = 1 ;
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 ):
cnt + = 1
if (cnt = = n):
return j;
return - 1 ;
N = 24 ;
print (nthNumber(N));
|
C#
using System;
class GFG{
static int nthNumber( int n)
{
int [] divs = new int [1000000];
int [] vis = new int [1000000];
int cnt = 0;
for ( int i = 2; cnt < n; i++)
{
if (divs[i] == 0)
{
for ( int j = 2 * i; j < 1000000; j += i)
{
if (vis[j] != 0)
{
continue ;
}
vis[j] = 1;
int currNum = j;
int count = 0;
while (currNum % i == 0)
{
divs[j]++;
currNum = currNum / i;
count++;
}
if (currNum == 1 && count == 3 && divs[j] == 3)
{
cnt++;
}
else if (currNum != 1 && divs[currNum] == 0 &&
count == 1 && divs[j] == 1)
{
cnt++;
}
if (cnt == n)
{
return j;
}
}
}
}
return -1;
}
static public void Main ()
{
int N = 24;
Console.Write(nthNumber(N));
}
}
|
Javascript
<script>
function nthNumber(n)
{
let divs = new Array(1000000);
for ( var i = 0; i < divs.length; i++)
{
divs[i] = 0;
}
let vis = new Array(1000000);
for ( var i = 0; i < vis.length; i++)
{
vis[i] = 0;
}
let cnt = 0;
for (let i = 2; cnt < n; i++)
{
if (divs[i] == 0)
{
for (let j = 2 * i; j < 1000000; j += i)
{
if (vis[j])
{
continue ;
}
vis[j] = true ;
let currNum = j;
let count = 0;
while (currNum % i == 0)
{
divs[j]++;
currNum = Math.floor(currNum / i);
count++;
}
if (currNum == 1 && count == 3 &&
divs[j] == 3)
{
cnt++;
}
else if (currNum != 1 && divs[currNum] == 0 &&
count == 1 && divs[j] == 1)
{
cnt++;
}
if (cnt == n)
{
return j;
}
}
}
}
return -1;
}
let N = 24;
document.write(nthNumber(N));
</script>
|
Time Complexity: O(Nlog(log(N))), where N is 1000000.
Auxiliary Space: O(N)
Last Updated :
21 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...