# N’th Smart Number

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
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

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 = 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++

 `// C++ implementation to find n'th smart number ` `#include ` `using` `namespace` `std; ` ` `  `// Limit on result ` `const` `int` `MAX = 3000; ` ` `  `// Function to calculate n'th smart number ` `int` `smartNumber(``int` `n) ` `{ ` `    ``// Initialize all numbers as not prime ` `    ``int` `primes[MAX] = {0}; ` ` `  `    ``// iterate to mark all primes and smart number ` `    ``vector<``int``> result; ` ` `  `    ``// Traverse all numbers till maximum limit ` `    ``for` `(``int` `i=2; i

## Java

 `// Java implementation to find n'th smart number ` `import` `java.util.*; ` `import` `java.lang.*; ` ` `  `class` `GFG { ` ` `  `    ``// Limit on result ` `    ``static` `int` `MAX = ``3000``; ` ` `  `    ``// Function to calculate n'th smart number ` `    ``public` `static` `int` `smartNumber(``int` `n) ` `    ``{ ` `         `  `        ``// Initialize all numbers as not prime ` `        ``Integer[] primes = ``new` `Integer[MAX]; ` `        ``Arrays.fill(primes, ``new` `Integer(``0``)); ` ` `  `        ``// iterate to mark all primes and smart ` `        ``// number ` `        ``Vector result = ``new` `Vector<>(); ` ` `  `        ``// Traverse all numbers till maximum ` `        ``// limit ` `        ``for` `(``int` `i = ``2``; i < MAX; i++) ` `        ``{ ` `             `  `            ``// 'i' is maked as prime number ` `            ``// because it is not multiple of ` `            ``// any other prime ` `            ``if` `(primes[i] == ``0``) ` `            ``{ ` `                ``primes[i] = ``1``; ` ` `  `                ``// mark all multiples of 'i'  ` `                ``// as non prime ` `                ``for` `(``int` `j = i*``2``; j < MAX; j = j+i) ` `                ``{ ` `                    ``primes[j] -= ``1``; ` `     `  `                    ``// If i is the third prime ` `                    ``// factor of j then add it ` `                    ``// to result as it has at ` `                    ``// least three prime factors. ` `                    ``if` `( (primes[j] + ``3``) == ``0``) ` `                        ``result.add(j); ` `                ``} ` `            ``} ` `        ``} ` ` `  `        ``// Sort all smart numbers ` `        ``Collections.sort(result); ` ` `  `        ``// return n'th smart number ` `        ``return` `result.get(n-``1``); ` ` `  `    ``} ` ` `  `    ``// Driver program to run the case ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `n = ``50``; ` `        ``System.out.println(smartNumber(n)); ` ` `  `    ``} ` `} ` ` `  `// This code is contributed by Prasad Kshirsagar `

## Python3

 `# Python3 implementation to find ` `# n'th smart number  ` ` `  `# Limit on result  ` `MAX` `=` `3000``;  ` ` `  `# Function to calculate n'th ` `# smart number  ` `def` `smartNumber(n):  ` ` `  `    ``# Initialize all numbers as not prime  ` `    ``primes ``=` `[``0``] ``*` `MAX``;  ` ` `  `    ``# iterate to mark all primes  ` `    ``# and smart number  ` `    ``result ``=` `[];  ` ` `  `    ``# Traverse all numbers till maximum limit  ` `    ``for` `i ``in` `range``(``2``, ``MAX``):  ` `         `  `        ``# 'i' is maked as prime number because  ` `        ``# it is not multiple of any other prime  ` `        ``if` `(primes[i] ``=``=` `0``):  ` `            ``primes[i] ``=` `1``;  ` ` `  `            ``# mark all multiples of 'i' as non prime ` `            ``j ``=` `i ``*` `2``; ` `            ``while` `(j < ``MAX``):  ` `                ``primes[j] ``-``=` `1``;  ` ` `  `                ``# If i is the third prime factor of j  ` `                ``# then add it to result as it has at  ` `                ``# least three prime factors.  ` `                ``if` `( (primes[j] ``+` `3``) ``=``=` `0``):  ` `                    ``result.append(j); ` `                ``j ``=` `j ``+` `i; ` ` `  `    ``# Sort all smart numbers  ` `    ``result.sort();  ` ` `  `    ``# return n'th smart number  ` `    ``return` `result[n ``-` `1``];  ` ` `  `# Driver Code ` `n ``=` `50``;  ` `print``(smartNumber(n));  ` ` `  `# This code is contributed by mits  `

## C#

 `// C# implementation to find n'th smart number ` `using` `System.Collections.Generic; ` ` `  `class` `GFG { ` ` `  `    ``// Limit on result ` `    ``static` `int` `MAX = 3000; ` ` `  `    ``// Function to calculate n'th smart number ` `    ``public` `static` `int` `smartNumber(``int` `n) ` `    ``{ ` `         `  `        ``// Initialize all numbers as not prime ` `        ``int``[] primes = ``new` `int``[MAX]; ` ` `  `        ``// iterate to mark all primes and smart ` `        ``// number ` `        ``List<``int``> result = ``new` `List<``int``>(); ` ` `  `        ``// Traverse all numbers till maximum ` `        ``// limit ` `        ``for` `(``int` `i = 2; i < MAX; i++) ` `        ``{ ` `             `  `            ``// 'i' is maked as prime number ` `            ``// because it is not multiple of ` `            ``// any other prime ` `            ``if` `(primes[i] == 0) ` `            ``{ ` `                ``primes[i] = 1; ` ` `  `                ``// mark all multiples of 'i'  ` `                ``// as non prime ` `                ``for` `(``int` `j = i*2; j < MAX; j = j+i) ` `                ``{ ` `                    ``primes[j] -= 1; ` `     `  `                    ``// If i is the third prime ` `                    ``// factor of j then add it ` `                    ``// to result as it has at ` `                    ``// least three prime factors. ` `                    ``if` `( (primes[j] + 3) == 0) ` `                        ``result.Add(j); ` `                ``} ` `            ``} ` `        ``} ` ` `  `        ``// Sort all smart numbers ` `        ``result.Sort(); ` ` `  `        ``// return n'th smart number ` `        ``return` `result[n-1]; ` ` `  `    ``} ` ` `  `    ``// Driver program to run the case ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 50; ` `        ``System.Console.WriteLine(smartNumber(n)); ` ` `  `    ``} ` `} ` ` `  `// This code is contributed by mits `

## PHP

 ` `

Output:

```273
```

This article is contributed by Shashak Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.