Compositorial of a number | Javascript program to find compositorial of composite numbers ; Function to check if a number is composite . ; Corner cases ; This is checked so that we can skip the middle five numbers in the below loop ; This function stores all composite numbers less than N ; Function to calculate the compositorial of n ; Multiply first n composite number ; Driver code ; Vector to store all the composite less than N
let compo = [ ] ; function isComposite ( n ) { if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; let i = 5 ; while ( i * i <= n ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; i = i + 6 ; } return false ; } function Compositorial_list ( n ) { let l = 0 ; for ( let i = 4 ; i < 1000000 ; i ++ ) { if ( l < n ) { if ( isComposite ( i ) ) { compo . push ( i ) ; l += 1 ; } } } } function calculateCompositorial ( n ) { let result = 1 ; for ( let i = 0 ; i < n ; i ++ ) result = result * compo [ i ] ; return result ; } let n = 5 ; Compositorial_list ( n ) ; document . write ( calculateCompositorial ( n ) ) ;