Count prime numbers that can be expressed as sum of consecutive prime numbers | Function to check if a number is prime or not ; Base Case ; Iterate till [ 5 , sqrt ( N ) ] to detect primality of numbers ; If N is divisible by i or i + 2 ; Return 1 if N is prime ; Function to count the prime numbers which can be expressed as sum of consecutive prime numbers ; Initialize count as 0 ; Stores prime numbers ; If i is prime ; Initialize the sum ; Find all required primes upto N ; Add it to the sum ; Return the final count ; Given number N ; Function call
def isprm ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT i = 5 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT i = i + 6 NEW_LINE DEDENT return 1 NEW_LINE DEDENT def countprime ( n ) : NEW_LINE INDENT count = 0 NEW_LINE primevector = [ ] NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( isprm ( i ) == 1 ) : NEW_LINE INDENT primevector . append ( i ) NEW_LINE DEDENT DEDENT sum = primevector [ 0 ] NEW_LINE for i in range ( 1 , len ( primevector ) ) : NEW_LINE INDENT sum += primevector [ i ] NEW_LINE if ( sum > n ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( isprm ( sum ) == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT N = 45 NEW_LINE print ( countprime ( N ) ) NEW_LINE