Count of ways to split a given number into prime segments | Python3 implementation to count total number of ways to split a string to get prime numbers ; Function to check whether a number is a prime number or not ; Function to find the count of ways to split string into prime numbers ; 1 based indexing ; Consider every suffix up to 6 digits ; Number should not have a leading zero and it should be a prime number ; Return the final result ; Driver code
MOD = 1000000007 NEW_LINE def isPrime ( number ) : NEW_LINE INDENT num = int ( number ) NEW_LINE i = 2 NEW_LINE while i * i <= num : NEW_LINE INDENT if ( ( num % i ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if num > 1 : NEW_LINE return True NEW_LINE else : NEW_LINE return False NEW_LINE DEDENT def countPrimeStrings ( number , i ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT cnt = 0 NEW_LINE for j in range ( 1 , 7 ) : NEW_LINE INDENT if ( i - j >= 0 and number [ i - j ] != '0' and isPrime ( number [ i - j : i ] ) ) : NEW_LINE INDENT cnt += countPrimeStrings ( number , i - j ) NEW_LINE cnt %= MOD NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = "3175" NEW_LINE l = len ( s1 ) NEW_LINE print ( countPrimeStrings ( s1 , l ) ) NEW_LINE DEDENT