N 'th Smart Number | Java implementation to find n 'th smart number ; Limit on result ; Function to calculate n 'th smart number ; Initialize all numbers as not prime ; iterate to mark all primes and smart number ; Traverse all numbers till maximum limit ; ' i ' is maked as prime number because it is not multiple of any other prime ; mark all multiples of ' i ' as non prime ; If i is the third prime factor of j then add it to result as it has at least three prime factors . ; Sort all smart numbers ; return n 'th smart number ; Driver program to run the case
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 ) ) ; } }