Count strings having sum of ASCII values of characters equal to a Prime or Armstrong Number | Function to check if a number is prime number ; Define a flag variable ; Check for factors of num ; If factor is found , set flag to True and break out of loop ; Check if flag is True ; Function to calculate order of the number x ; Function to check whether the given number is Armstrong number or not ; If the condition satisfies ; Function to count Armstrong valued strings ; Stores the count of Armstrong valued strings ; Iterate over the list ; Store the value of the string ; Find value of the string ; Check if it an Armstrong number ; Function to count prime valued strings ; Store the count of prime valued strings ; Iterate over the list ; Store the value of the string ; Find value of the string ; Check if it is a Prime Number ; Driver code ; Function Call
def isPrime ( num ) : NEW_LINE INDENT flag = False NEW_LINE if num > 1 : NEW_LINE INDENT for i in range ( 2 , num ) : NEW_LINE INDENT if ( num % i ) == 0 : NEW_LINE INDENT flag = True NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if flag : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT def order ( x ) : NEW_LINE INDENT n = 0 NEW_LINE while ( x != 0 ) : NEW_LINE INDENT n = n + 1 NEW_LINE x = x // 10 NEW_LINE DEDENT return n NEW_LINE DEDENT def isArmstrong ( x ) : NEW_LINE INDENT n = order ( x ) NEW_LINE temp = x NEW_LINE sum1 = 0 NEW_LINE while ( temp != 0 ) : NEW_LINE INDENT r = temp % 10 NEW_LINE sum1 = sum1 + r ** n NEW_LINE temp = temp // 10 NEW_LINE DEDENT return ( sum1 == x ) NEW_LINE DEDENT def count_armstrong ( li ) : NEW_LINE INDENT c = 0 NEW_LINE for ele in li : NEW_LINE INDENT val = 0 NEW_LINE for che in ele : NEW_LINE INDENT val += ord ( che ) NEW_LINE DEDENT if isArmstrong ( val ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT return c NEW_LINE DEDENT def count_prime ( li ) : NEW_LINE INDENT c = 0 NEW_LINE for ele in li : NEW_LINE INDENT val = 0 NEW_LINE for che in ele : NEW_LINE INDENT val += ord ( che ) NEW_LINE DEDENT if isPrime ( val ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT return c NEW_LINE DEDENT arr = [ " geeksforgeeks " , " a " , " computer " , " science " , " portal " , " for " , " geeks " ] NEW_LINE print ( " Number ▁ of ▁ Armstrong ▁ Strings ▁ are : " , count_armstrong ( arr ) ) NEW_LINE print ( " Number ▁ of ▁ Prime ▁ Strings ▁ are : " , count_prime ( arr ) ) NEW_LINE