Find the largest composite number that divides N but is strictly lesser than N | Function to check whether a number is prime or not ; Corner case ; Check from 2 to n - 1 ; Function that find the largest composite number which divides N and less than N ; Find the prime number ; Driver 's Code ; Get the smallest prime factor ; Check if ( N / a ) is prime or not If Yes print " - 1" ; Else print largest composite number ( N / a )
function isPrime ( n ) { if ( n <= 1 ) return false ; var i ; for ( i = 2 ; i < n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } function getSmallestPrimefactor ( n ) { var i ; for ( i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) return i ; } } var N = 100 ; var a ; a = getSmallestPrimefactor ( N ) ; if ( isPrime ( N / a ) ) document . write ( " " ) ; else document . write ( N / a ) ;