N’th Smart Number
Last Updated :
08 May, 2023
Given a number n, find n’th smart number (1<=n<=1000). Smart number is a number which has at least three distinct prime factors. We are given an upper limit on value of result as MAX For example 30 is 1st smart number because it has 2, 3, 5 as it’s distinct prime factors. 42 is 2nd smart number because it has 2, 3, 7 as it’s distinct prime factors. Examples:
Input : n = 1
Output: 30
// three distinct prime factors 2, 3, 5
Input : n = 50
Output: 273
// three distinct prime factors 3, 7, 13
Input : n = 1000
Output: 2664
// three distinct prime factors 2, 3, 37
The idea is based on Sieve of Eratosthenes. We use an array to use an array prime[] to keep track of prime numbers. We also use the same array to keep track of the count of prime factors seen so far. Whenever the count reaches 3, we add the number to result.
- Take an array primes[] and initialize it with 0.
- Now we know that first prime number is i = 2 so mark primes[2] = 1 i.e; primes[i] = 1 indicates that ‘i’ is prime number.
- Now traverse the primes[] array and mark all multiples of ‘i’ by condition primes[j] -= 1 where ‘j’ is multiple of ‘i’, and check the condition primes[j]+3 = 0 because whenever primes[j] become -3 it indicates that previously it had been multiple of three distinct prime factors. If condition primes[j]+3=0 becomes true that means ‘j’ is a Smart Number so store it in a array result[].
- Now sort array result[] and return result[n-1].
Below is the implementation of above idea.
C++
#include<bits/stdc++.h>
using namespace std;
const int MAX = 3000;
int smartNumber( int n)
{
int primes[MAX] = {0};
vector< int > result;
for ( int i=2; i<MAX; i++)
{
if (primes[i] == 0)
{
primes[i] = 1;
for ( int j=i*2; j<MAX; j=j+i)
{
primes[j] -= 1;
if ( (primes[j] + 3) == 0)
result.push_back(j);
}
}
}
sort(result.begin(), result.end());
return result[n-1];
}
int main()
{
int n = 50;
cout << smartNumber(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG {
static int MAX = 3000 ;
public static int smartNumber( int n)
{
Integer[] primes = new Integer[MAX];
Arrays.fill(primes, new Integer( 0 ));
Vector<Integer> result = new Vector<>();
for ( int i = 2 ; i < MAX; i++)
{
if (primes[i] == 0 )
{
primes[i] = 1 ;
for ( int j = i* 2 ; j < MAX; j = j+i)
{
primes[j] -= 1 ;
if ( (primes[j] + 3 ) == 0 )
result.add(j);
}
}
}
Collections.sort(result);
return result.get(n- 1 );
}
public static void main(String[] args)
{
int n = 50 ;
System.out.println(smartNumber(n));
}
}
|
Python3
MAX = 3000 ;
def smartNumber(n):
primes = [ 0 ] * MAX ;
result = [];
for i in range ( 2 , MAX ):
if (primes[i] = = 0 ):
primes[i] = 1 ;
j = i * 2 ;
while (j < MAX ):
primes[j] - = 1 ;
if ( (primes[j] + 3 ) = = 0 ):
result.append(j);
j = j + i;
result.sort();
return result[n - 1 ];
n = 50 ;
print (smartNumber(n));
|
C#
using System.Collections.Generic;
class GFG {
static int MAX = 3000;
public static int smartNumber( int n)
{
int [] primes = new int [MAX];
List< int > result = new List< int >();
for ( int i = 2; i < MAX; i++)
{
if (primes[i] == 0)
{
primes[i] = 1;
for ( int j = i*2; j < MAX; j = j+i)
{
primes[j] -= 1;
if ( (primes[j] + 3) == 0)
result.Add(j);
}
}
}
result.Sort();
return result[n-1];
}
public static void Main()
{
int n = 50;
System.Console.WriteLine(smartNumber(n));
}
}
|
PHP
<?php
$MAX = 3000;
function smartNumber( $n )
{
global $MAX ;
$primes = array_fill (0, $MAX ,0);
$result = array ();
for ( $i =2; $i < $MAX ; $i ++)
{
if ( $primes [ $i ] == 0)
{
$primes [ $i ] = 1;
for ( $j = $i *2; $j < $MAX ; $j = $j + $i )
{
$primes [ $j ] -= 1;
if ( ( $primes [ $j ] + 3) == 0)
array_push ( $result , $j );
}
}
}
sort( $result );
return $result [ $n -1];
}
$n = 50;
echo smartNumber( $n );
?>
|
Javascript
<script>
const MAX = 3000;
function smartNumber(n)
{
let primes = new Array(MAX).fill(0);
let result = [];
for (let i=2; i<MAX; i++)
{
if (primes[i] == 0)
{
primes[i] = 1;
for (let j=i*2; j<MAX; j=j+i)
{
primes[j] -= 1;
if ( (primes[j] + 3) == 0)
result.push(j);
}
}
}
result.sort((a,b)=>a-b);
return result[n-1];
}
let n = 50;
document.write(smartNumber(n));
</script>
|
Output:
273
Time Complexity: O(MAX)
Auxiliary Space: O(MAX)
Share your thoughts in the comments
Please Login to comment...