Count N | Stores the dp states ; Check if a number is a prime or not ; Function to generate all prime numbers that are less than or equal to n ; Base cases . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of as non - prime ; Function to find the count of N - digit numbers such that the sum of digits is a prime number ; If end of array is reached ; If the sum is equal to a prime number ; Otherwise ; If the dp - states are already computed ; If index = 1 , any digit from [ 1 - 9 ] can be placed . If N = 1 , 0 also can be placed . ; Otherwise , any digit from [ 0 - 9 ] can be placed . ; Return the answer . ; Find all primes less than or equal to 1000 , which is sufficient for N upto 100 ; Given Input ; Function call
dp = [ [ - 1 ] * 100 ] * 1000 NEW_LINE prime = [ True ] * 1005 NEW_LINE def SieveOfEratosthenes ( n ) : NEW_LINE INDENT prime [ 0 ] = prime [ 1 ] = False NEW_LINE p = 2 NEW_LINE while ( p * p <= n ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , n + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT DEDENT def countOfNumbers ( index , sum , N ) : NEW_LINE INDENT if ( index == N + 1 ) : NEW_LINE INDENT if ( prime [ sum ] == True ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT val = dp [ index ] [ sum ] NEW_LINE if ( val != - 1 ) : NEW_LINE INDENT return val NEW_LINE DEDENT val = 0 NEW_LINE if ( index == 1 ) : NEW_LINE INDENT for digit in range ( ( ( 0 , 1 ) [ N == 1 ] ) + 1 , 10 , 1 ) : NEW_LINE INDENT val += countOfNumbers ( index + 1 , sum + digit , N ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for digit in range ( 0 , 10 , 1 ) : NEW_LINE INDENT val += countOfNumbers ( index + 1 , sum + digit , N ) NEW_LINE DEDENT DEDENT return val NEW_LINE DEDENT SieveOfEratosthenes ( 1000 ) NEW_LINE N = 6 NEW_LINE print ( countOfNumbers ( 1 , 0 , N ) ) NEW_LINE