Count Sexy Prime Pairs in the given array | A utility function to check if the number n is prime or not ; Base Cases ; Check to skip middle five numbers in below loop ; If n is divisible by i and i + 2 then it is not prime ; A utility function that check if n1 and n2 are SPP ( Sexy Prime Pair ) or not ; Function to find SPP ( Sexy Prime Pair ) pairs from the given array ; Iterate through all pairs ; Increment count if SPP ( Sexy Prime Pair ) pair ; Driver code ; Function call to find SPP ( Sexy Prime Pair ) pair
function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( var i = 5 ; i * i <= n ; i += 6 ) { if ( n % i == 0 || n % ( i + 6 ) == 0 ) { return false ; } } return true ; } function SexyPrime ( n1 , n2 ) { return ( isPrime ( n1 ) && isPrime ( n2 ) && Math . abs ( n1 - n2 ) == 6 ) ; } function countSexyPairs ( arr , n ) { var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i + 1 ; j < n ; j ++ ) { if ( SexyPrime ( arr [ i ] , arr [ j ] ) ) { count ++ ; } } } return count ; } var arr = [ 6 , 7 , 5 , 11 , 13 ] var n = arr . length ; document . write ( countSexyPairs ( arr , n ) ) ;