Cuente las strings que tienen una suma de valores ASCII de caracteres iguales a un número primo o de Armstrong

Dada una array arr[] de tamaño N que contiene strings, la tarea es contar el número de strings que tienen una suma de valores ASCII de caracteres iguales a un número de Armstrong o un número primo.

Ejemplos:

Entrada: arr[] = {“hello”, “nace”}
Salida: El
número de strings Armstrong es: 1
El número de strings principales es: 0
Explicación: la suma de los valores ASCII de los caracteres de cada string es: {532, 407}, de los cuales 407 es un número de Armstrong, y ninguno de ellos es un número primo.
Por lo tanto, la string valorada por armstrong es «nace».

Entrada: arr[] = {“geeksforgeeks”, “a”, “computer”, “science”, “portal”, “for”, “geeks”}
Salida: El
número de strings Armstrong es: 0
El número de strings principales es: 2 
Explicación: La suma de los valores ASCII de los caracteres de cada string es: {1381, 97, 879, 730, 658, 327, 527}, de los cuales 1381 y 97 son números primos y ninguno de ellos es un número de Armstrong.
Por lo tanto, las strings con valores primos son «geeksforgeeks» y «a».

Enfoque: este problema se puede resolver calculando el valor ASCII de cada string. Siga los pasos a continuación para resolver este problema:

A continuación se muestra la implementación del enfoque anterior:

C++

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if a
// number is prime number
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
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
bool 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 strings
int 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 strings
int 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 code
int 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

// Java program for the above approach
import 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.

Python3

# Python program for the above approach
 
# Function to check if a
# number is prime number
def 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 x
def 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 not
def 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 strings
def 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 strings
def 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 code
arr = ["geeksforgeeks", "a", "computer",
       "science", "portal", "for", "geeks"]
 
# Function Call
print("Number of Armstrong Strings are:", count_armstrong(arr))
print("Number of Prime Strings are:", count_prime(arr))

C#

// C# program for the above approach
using 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.

Javascript

<script>
 
// JavaScript program for the above approach
 
// Function to check if a
// number is prime number
function 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 x
function 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 not
function 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 strings
function 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 strings
function 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 Call
document.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>
Producción

Number of Armstrong Strings are: 0
Number of Prime Strings are: 2

Complejidad de tiempo: O(N*M), donde M es la longitud de la string más larga de la array arr[]
Espacio auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por santhoshcharan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *