Count of subarrays consisting of only prime numbers | Function to check if a number is prime or not . ; If n has any factor other than 1 , then n is non - prime . ; Function to return the count of subarrays made up of prime numbers only ; Stores the answer ; Stores the count of continuous prime numbers in an array ; If the current array element is prime ; Increase the count ; Update count of subarrays ; If the array ended with a continuous prime sequence ; Driver Code
function is_prime ( n ) { if ( n <= 1 ) return 0 ; for ( var i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) return 0 ; } return 1 ; } function count_prime_subarrays ( ar , n ) { var ans = 0 ; var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( is_prime ( ar [ i ] ) ) count ++ ; else { if ( count ) { ans += ( count * ( count + 1 ) ) / 2 ; count = 0 ; } } } if ( count ) ans += ( count * ( count + 1 ) ) / 2 ; return ans ; } var N = 10 ; var ar = [ 2 , 3 , 5 , 6 , 7 , 11 , 3 , 5 , 9 , 3 ] ; document . write ( count_prime_subarrays ( ar , N ) ) ;