Program to find the Nth Composite Number | Function to find the Nth Composite Numbers using Sieve of Eratosthenes ; Sieve of prime numbers ;  ; Iterate over the range [ 2 , 1000005 ] ; If IsPrime [ p ] is true ; Iterate over the range [ p * p , 1000005 ] ; Stores the list of composite numbers ; Iterate over the range [ 4 , 1000005 ] ; If i is not prime ; Return Nth Composite Number ; Driver Code
def NthComposite ( N ) : NEW_LINE INDENT IsPrime = [ True ] * 1000005 NEW_LINE DEDENT / * Initialize the array to true * / NEW_LINE INDENT for p in range ( 2 , 1000005 ) : NEW_LINE INDENT if p * p > 1000005 : NEW_LINE INDENT break NEW_LINE DEDENT if ( IsPrime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , 1000005 , p ) : NEW_LINE INDENT IsPrime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT Composites = [ ] NEW_LINE for p in range ( 4 , 1000005 ) : NEW_LINE INDENT if ( not IsPrime [ p ] ) : NEW_LINE INDENT Composites . append ( p ) NEW_LINE DEDENT DEDENT return Composites [ N - 1 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE print ( NthComposite ( N ) ) NEW_LINE DEDENT