Program to find the Nth Composite Number | C # program for the above approach ; Function to find the Nth Composite Numbers using Sieve of Eratosthenes ; Sieve of prime numbers ; Initialize the array to true ; Iterate over the range [ 2 , MAX_SIZE ] ; If IsPrime [ p ] is true ; Iterate over the range [ p * p , MAX_SIZE ] ; Stores the list of composite numbers ; Iterate over the range [ 4 , MAX_SIZE ] ; If i is not prime ; Return Nth Composite Number ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { public static int NthComposite ( int N ) { int MAX_SIZE = 1000005 ; bool [ ] IsPrime = new bool [ MAX_SIZE ] ; Array . Fill ( IsPrime , true ) ; for ( int p = 2 ; p * p < MAX_SIZE ; p ++ ) { if ( IsPrime [ p ] == true ) { for ( int i = p * p ; i < MAX_SIZE ; i += p ) IsPrime [ i ] = false ; } } List < int > Composites = new List < int > ( ) ; for ( int p = 4 ; p < MAX_SIZE ; p ++ ) if ( ! IsPrime [ p ] ) Composites . Add ( p ) ; return Composites [ N - 1 ] ; } static public void Main ( ) { int N = 4 ; Console . WriteLine ( NthComposite ( N ) ) ; } }