Count strings having sum of ASCII values of characters equal to a Prime or Armstrong Number - GeeksforGeeks

05 Jul, 2021
Given an array arr[] of size N containing strings, the task is to count the number of strings having sum of ASCII values of characters equal to an Armstrong Number number or a Prime Number.
Examples:
Input: arr[] = {“hello”, “nace”}Output:Number of Armstrong Strings are: 1Number of Prime Strings are: 0Explanation: Sum of ASCII values of characters of each string is: {532, 407}, out of which 407 is an Armstrong Number, and none of them is a Prime Number.Hence, the armstrong valued string is “nace”.
Input: arr[] = {“geeksforgeeks”, “a”, “computer”, “science”, “portal”, “for”, “geeks”}Output:Number of Armstrong Strings are: 0Number of Prime Strings are: 2 Explanation: Sum of ASCII values of characters of each string is: {1381, 97, 879, 730, 658, 327, 527}, out of which 1381 and 97 are Prime Numbers, and none of them is an Armstrong Number.Hence, prime valued strings are “geeksforgeeks” and “a”.
Approach: This problem can be solved by calculating the ASCII value of each string. Follow the steps below to solve this problem:
Initialize two variables, countPrime and countArmstrong as 0, to store the count of Prime and Armstrong valued strings.
Iterate over the range of indices [0, N – 1] using a variable, say i and perform the following steps: Store the sum of ASCII values of characters of the current string arr[i] in a variable, say val.If the number val is an Armstrong Number, increment countArmstrong by 1.If the number val is a Prime Number, increment countPrime by 1.
Store the sum of ASCII values of characters of the current string arr[i] in a variable, say val.
If the number val is an Armstrong Number, increment countArmstrong by 1.
If the number val is a Prime Number, increment countPrime by 1.
Print the values of countPrime and countArmstrong as the result.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to check if a// number is prime numberbool isPrime(int num){         // Define a flag variable    bool flag = false;     if (num > 1)    {                 // Check for factors of num        for(int i = 2; i < num; i++)        {                         // If factor is found,            // set flag to True and            // break out of loop            if ((num % i) == 0)            {                flag = true;                break;            }        }    }     // Check if flag is True    if (flag)        return false;    else        return true;} // Function to calculate// order of the number xint order(int x){    int n = 0;    while (x != 0)    {        n = n + 1;        x = x / 10;    }    return n;} // Function to check whether the given// number is Armstrong number or notbool isArmstrong(int x){    int n = order(x);    int temp = x;    int sum1 = 0;     while (temp != 0)    {        int r = temp % 10;        sum1 = sum1 + pow(r, n);        temp = temp / 10;    }         // If the condition satisfies    return (sum1 == x);} // Function to count// Armstrong valued stringsint count_armstrong(vector<string> li){         // Stores the count of    // Armstrong valued strings    int c = 0;     // Iterate over the list    for(string ele : li)    {                 // Store the value        // of the string        int val = 0;         // Find value of the string        for(char che:ele)            val += che;         // Check if it an Armstrong number        if (isArmstrong(val))            c += 1;    }    return c;} // Function to count// prime valued stringsint count_prime(vector<string> li){         // Store the count of    // prime valued strings    int c = 0;     // Iterate over the list    for(string ele:li)    {                 // Store the value        // of the string        int val = 0;         // Find value of the string        for(char che : ele)            val += che;         // Check if it        // is a Prime Number        if (isPrime(val))            c += 1;    }    return c;} // Driver codeint main(){    vector<string> arr = { "geeksforgeeks", "a", "computer",                           "science", "portal", "for", "geeks"};         // Function Call    cout << "Number of Armstrong Strings are: "         << count_armstrong(arr) << endl;    cout << "Number of Prime Strings are: "         << count_prime(arr) << endl;} // This code is contributed by mohit kumar 29
// Java program for the above approachimport java.io.*; class GFG {         // Function to check if a    // number is prime number    static boolean isPrime(int num)    {          // Define a flag variable        boolean flag = false;          if (num > 1) {              // Check for factors of num            for (int i = 2; i < num; i++) {                  // If factor is found,                // set flag to True and                // break out of loop                if ((num % i) == 0) {                    flag = true;                    break;                }            }        }          // Check if flag is True        if (flag)            return false;        else            return true;    }      // Function to calculate    // order of the number x    static int order(int x)    {        int n = 0;        while (x != 0) {            n = n + 1;            x = x / 10;        }        return n;    }      // Function to check whether the given    // number is Armstrong number or not    static boolean isArmstrong(int x)    {        int n = order(x);        int temp = x;        int sum1 = 0;          while (temp != 0) {            int r = temp % 10;            sum1 = sum1 + (int)(Math.pow(r, n));            temp = temp / 10;        }          // If the condition satisfies        return (sum1 == x);    }      // Function to count    // Armstrong valued strings    static int count_armstrong(String[] li)    {          // Stores the count of        // Armstrong valued strings        int c = 0;          // Iterate over the list        for(String ele : li)        {              // Store the value            // of the string            int val = 0;              // Find value of the string            for(char che : ele.toCharArray()) val += che;              // Check if it an Armstrong number            if (isArmstrong(val))                c += 1;        }        return c;    }      // Function to count    // prime valued strings    static int count_prime(String[] li)    {          // Store the count of        // prime valued strings        int c = 0;          // Iterate over the list        for(String ele : li)        {              // Store the value            // of the string            int val = 0;              // Find value of the string            for(char che : ele.toCharArray()) val += che;              // Check if it            // is a Prime Number            if (isPrime(val))                c += 1;        }        return c;    }      // Driver code         public static void main (String[] args) {        String[] arr            = { "geeksforgeeks", "a",      "computer",                "science",       "portal", "for",                "geeks" };          // Function Call        System.out.println(            "Number of Armstrong Strings are: "            + count_armstrong(arr));        System.out.println("Number of Prime Strings are: "                          + count_prime(arr));    }} // This code is contributed by patel2127.
# Python program for the above approach # Function to check if a# number is prime numberdef isPrime(num):     # Define a flag variable    flag = False     if num > 1:         # Check for factors of num        for i in range(2, num):             # If factor is found,            # set flag to True and            # break out of loop            if (num % i) == 0:                flag = True                break     # Check if flag is True    if flag:        return False    else:        return True # Function to calculate# order of the number xdef order(x):    n = 0    while (x != 0):        n = n + 1        x = x // 10    return n # Function to check whether the given# number is Armstrong number or notdef isArmstrong(x):    n = order(x)    temp = x    sum1 = 0     while (temp != 0):         r = temp % 10        sum1 = sum1 + r**n        temp = temp // 10     # If the condition satisfies    return (sum1 == x) # Function to count# Armstrong valued stringsdef count_armstrong(li):     # Stores the count of    # Armstrong valued strings    c = 0     # Iterate over the list    for ele in li:         # Store the value        # of the string        val = 0                 # Find value of the string        for che in ele:            val += ord(che)                     # Check if it an Armstrong number        if isArmstrong(val):            c += 1    return c # Function to count# prime valued stringsdef count_prime(li):         # Store the count of    # prime valued strings    c = 0         # Iterate over the list    for ele in li:               # Store the value        # of the string        val = 0                 # Find value of the string        for che in ele:            val += ord(che)                     # Check if it        # is a Prime Number        if isPrime(val):            c += 1    return c  # Driver codearr = ["geeksforgeeks", "a", "computer",       "science", "portal", "for", "geeks"] # Function Callprint("Number of Armstrong Strings are:", count_armstrong(arr))print("Number of Prime Strings are:", count_prime(arr))
// C# program for the above approachusing System;using System.Collections.Generic;class GFG{       // Function to check if a    // number is prime number    static bool isPrime(int num)    {         // Define a flag variable        bool flag = false;         if (num > 1) {             // Check for factors of num            for (int i = 2; i < num; i++) {                 // If factor is found,                // set flag to True and                // break out of loop                if ((num % i) == 0) {                    flag = true;                    break;                }            }        }         // Check if flag is True        if (flag)            return false;        else            return true;    }     // Function to calculate    // order of the number x    static int order(int x)    {        int n = 0;        while (x != 0) {            n = n + 1;            x = x / 10;        }        return n;    }     // Function to check whether the given    // number is Armstrong number or not    static bool isArmstrong(int x)    {        int n = order(x);        int temp = x;        int sum1 = 0;         while (temp != 0) {            int r = temp % 10;            sum1 = sum1 + (int)(Math.Pow(r, n));            temp = temp / 10;        }         // If the condition satisfies        return (sum1 == x);    }     // Function to count    // Armstrong valued strings    static int count_armstrong(string[] li)    {         // Stores the count of        // Armstrong valued strings        int c = 0;         // Iterate over the list        foreach(string ele in li)        {             // Store the value            // of the string            int val = 0;             // Find value of the string            foreach(char che in ele) val += che;             // Check if it an Armstrong number            if (isArmstrong(val))                c += 1;        }        return c;    }     // Function to count    // prime valued strings    static int count_prime(string[] li)    {         // Store the count of        // prime valued strings        int c = 0;         // Iterate over the list        foreach(string ele in li)        {             // Store the value            // of the string            int val = 0;             // Find value of the string            foreach(char che in ele) val += che;             // Check if it            // is a Prime Number            if (isPrime(val))                c += 1;        }        return c;    }     // Driver code    public static void Main()    {        string[] arr            = { "geeksforgeeks", "a",      "computer",                "science",       "portal", "for",                "geeks" };         // Function Call        Console.WriteLine(            "Number of Armstrong Strings are: "            + count_armstrong(arr));        Console.WriteLine("Number of Prime Strings are: "                          + count_prime(arr));    }} // This code is contributed by ukasp.
<script> // JavaScript program for the above approach // Function to check if a// number is prime numberfunction isPrime(num) {     // Define a flag variable    let flag = false;     if (num > 1) {         // Check for factors of num        for (let i = 2; i < num; i++) {             // If factor is found,            // set flag to True and            // break out of loop            if ((num % i) == 0) {                flag = true;                break;            }        }    }     // Check if flag is True    if (flag)        return false;    else        return true;} // Function to calculate// order of the number xfunction order(x) {    let n = 0;    while (x != 0) {        n = n + 1;        x = x / 10;    }    return n;} // Function to check whether the given// number is Armstrong number or notfunction isArmstrong(x) {    let n = order(x);    let temp = x;    let sum1 = 0;     while (temp != 0) {        let r = temp % 10;        sum1 = sum1 + Math.pow(r, n);        temp = temp / 10;    }     // If the condition satisfies    return (sum1 == x);} // Function to count// Armstrong valued stringsfunction count_armstrong(li) {     // Stores the count of    // Armstrong valued strings    let c = 0;     // Iterate over the list    for (let ele of li) {         // Store the value        // of the string        let val = 0;         // Find value of the string        for (let che of ele)            val += che.charCodeAt(0);         // Check if it an Armstrong number        if (isArmstrong(val))            c += 1;    }    return c;} // Function to count// prime valued stringsfunction count_prime(li) {     // Store the count of    // prime valued strings    let c = 0;     // Iterate over the list    for (let ele of li) {         // Store the value        // of the string        let val = 0;         // Find value of the string        for (let che of ele)            val += che.charCodeAt(0);         // Check if it        // is a Prime Number        if (isPrime(val))            c += 1;    }    return c;} // Driver code let arr = ["geeksforgeeks", "a", "computer",    "science", "portal", "for", "geeks"]; // Function Calldocument.write("Number of Armstrong Strings are: "    + count_armstrong(arr) + "<br>");document.write("Number of Prime Strings are: "    + count_prime(arr) + "<br>");  // This code is contributed by gfgking </script>
Number of Armstrong Strings are: 0
Number of Prime Strings are: 2
Time Complexity: O(N*M), where M is the length of the longest string in the array arr[]Auxiliary Space: O(1)
mohit kumar 29
ukasp
gfgking
patel2127
ASCII
Numbers
Prime Number
Arrays
Mathematical
Strings
Arrays
Strings
Mathematical
Prime Number
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Chocolate Distribution Problem
Window Sliding Technique
Reversal algorithm for array rotation
Next Greater Element
Find duplicates in O(n) time and O(1) extra space | Set 1
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7