Check whether the given numbers are Cousin prime or not | Function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Returns true if n1 and n2 are Cousin primes ; Check if they differ by 4 or not ; Check if both are prime number or not ; Get the 2 numbers ; Check the numbers for cousin prime
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 = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } function isCousinPrime ( n1 , n2 ) { if ( Math . abs ( n1 - n2 ) != 4 ) return false ; else return ( isPrime ( n1 ) && isPrime ( n2 ) ) ; } var n1 = 7 , n2 = 11 ; if ( isCousinPrime ( n1 , n2 ) ) document . write ( " " ) ; else document . write ( " " ) ;