Smallest composite number not divisible by first N prime numbers | Initializing the max value ; Function to generate N prime numbers using Sieve of Eratosthenes ; Stores the primes ; Setting all numbers to be prime initially ; If a prime number is encountered ; Set all its multiples as composites ; Store all the prime numbers ; Function to find the square of the ( N + 1 ) - th prime number ; Driver Code ; Stores all prime numbers
var MAX_SIZE = 1000005 ; function SieveOfEratosthenes ( StorePrimes ) { var IsPrime = Array ( MAX_SIZE ) . fill ( true ) ; var p , i ; for ( p = 2 ; p * p < MAX_SIZE ; p ++ ) { if ( IsPrime [ p ] == true ) { for ( i = p * p ; i < MAX_SIZE ; i += p ) IsPrime [ i ] = false ; } } for ( p = 2 ; p < MAX_SIZE ; p ++ ) if ( IsPrime [ p ] ) StorePrimes . push ( p ) ; } function Smallest_non_Prime ( StorePrimes , N ) { var x = StorePrimes [ N ] ; return x * x ; } N = 3 ; var StorePrimes = [ ] ; SieveOfEratosthenes ( StorePrimes ) ; document . write ( Smallest_non_Prime ( StorePrimes , N ) ) ;