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
def is_prime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT i = 2 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return 1 NEW_LINE DEDENT def count_prime_subarrays ( ar , n ) : NEW_LINE INDENT ans = 0 NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( is_prime ( ar [ i ] ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( count ) : NEW_LINE INDENT ans += count * ( count + 1 ) // 2 NEW_LINE count = 0 NEW_LINE DEDENT DEDENT DEDENT if ( count ) : NEW_LINE INDENT ans += count * ( count + 1 ) // 2 NEW_LINE DEDENT return ans NEW_LINE DEDENT N = 10 NEW_LINE ar = [ 2 , 3 , 5 , 6 , 7 , 11 , 3 , 5 , 9 , 3 ] NEW_LINE print ( count_prime_subarrays ( ar , N ) ) NEW_LINE