Count strings having sum of ASCII values of characters equal to a Prime or Armstrong Number
Last Updated :
08 Feb, 2024
Given an array arr[] of size N containing strings, the task is to count the number of strings having the sum of ASCII (American Standard Code for Information Interchange) values of characters equal to an Armstrong Number number or a Prime Number.
Examples:
Input: arr[] = {“hello”, “nace”}
Output:
Number of Armstrong Strings are: 1
Number of Prime Strings are: 0
Explanation: 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: 0
Number 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:
- Print the values of countPrime and countArmstrong as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int num)
{
bool flag = false ;
if (num > 1)
{
for ( int i = 2; i < num; i++)
{
if ((num % i) == 0)
{
flag = true ;
break ;
}
}
}
if (flag)
return false ;
else
return true ;
}
int order( int x)
{
int n = 0;
while (x != 0)
{
n = n + 1;
x = x / 10;
}
return n;
}
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;
}
return (sum1 == x);
}
int count_armstrong(vector<string> li)
{
int c = 0;
for (string ele : li)
{
int val = 0;
for ( char che:ele)
val += che;
if (isArmstrong(val))
c += 1;
}
return c;
}
int count_prime(vector<string> li)
{
int c = 0;
for (string ele:li)
{
int val = 0;
for ( char che : ele)
val += che;
if (isPrime(val))
c += 1;
}
return c;
}
int main()
{
vector<string> arr = { "geeksforgeeks" , "a" , "computer" ,
"science" , "portal" , "for" , "geeks" };
cout << "Number of Armstrong Strings are: "
<< count_armstrong(arr) << endl;
cout << "Number of Prime Strings are: "
<< count_prime(arr) << endl;
}
|
Java
import java.io.*;
class GFG {
static boolean isPrime( int num)
{
boolean flag = false ;
if (num > 1 ) {
for ( int i = 2 ; i < num; i++) {
if ((num % i) == 0 ) {
flag = true ;
break ;
}
}
}
if (flag)
return false ;
else
return true ;
}
static int order( int x)
{
int n = 0 ;
while (x != 0 ) {
n = n + 1 ;
x = x / 10 ;
}
return n;
}
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 ;
}
return (sum1 == x);
}
static int count_armstrong(String[] li)
{
int c = 0 ;
for (String ele : li)
{
int val = 0 ;
for ( char che : ele.toCharArray()) val += che;
if (isArmstrong(val))
c += 1 ;
}
return c;
}
static int count_prime(String[] li)
{
int c = 0 ;
for (String ele : li)
{
int val = 0 ;
for ( char che : ele.toCharArray()) val += che;
if (isPrime(val))
c += 1 ;
}
return c;
}
public static void main (String[] args) {
String[] arr
= { "geeksforgeeks" , "a" , "computer" ,
"science" , "portal" , "for" ,
"geeks" };
System.out.println(
"Number of Armstrong Strings are: "
+ count_armstrong(arr));
System.out.println( "Number of Prime Strings are: "
+ count_prime(arr));
}
}
|
Python3
def isPrime(num):
flag = False
if num > 1 :
for i in range ( 2 , num):
if (num % i) = = 0 :
flag = True
break
if flag:
return False
else :
return True
def order(x):
n = 0
while (x ! = 0 ):
n = n + 1
x = x / / 10
return n
def isArmstrong(x):
n = order(x)
temp = x
sum1 = 0
while (temp ! = 0 ):
r = temp % 10
sum1 = sum1 + r * * n
temp = temp / / 10
return (sum1 = = x)
def count_armstrong(li):
c = 0
for ele in li:
val = 0
for che in ele:
val + = ord (che)
if isArmstrong(val):
c + = 1
return c
def count_prime(li):
c = 0
for ele in li:
val = 0
for che in ele:
val + = ord (che)
if isPrime(val):
c + = 1
return c
arr = [ "geeksforgeeks" , "a" , "computer" ,
"science" , "portal" , "for" , "geeks" ]
print ( "Number of Armstrong Strings are:" , count_armstrong(arr))
print ( "Number of Prime Strings are:" , count_prime(arr))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static bool isPrime( int num)
{
bool flag = false ;
if (num > 1) {
for ( int i = 2; i < num; i++) {
if ((num % i) == 0) {
flag = true ;
break ;
}
}
}
if (flag)
return false ;
else
return true ;
}
static int order( int x)
{
int n = 0;
while (x != 0) {
n = n + 1;
x = x / 10;
}
return n;
}
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;
}
return (sum1 == x);
}
static int count_armstrong( string [] li)
{
int c = 0;
foreach ( string ele in li)
{
int val = 0;
foreach ( char che in ele) val += che;
if (isArmstrong(val))
c += 1;
}
return c;
}
static int count_prime( string [] li)
{
int c = 0;
foreach ( string ele in li)
{
int val = 0;
foreach ( char che in ele) val += che;
if (isPrime(val))
c += 1;
}
return c;
}
public static void Main()
{
string [] arr
= { "geeksforgeeks" , "a" , "computer" ,
"science" , "portal" , "for" ,
"geeks" };
Console.WriteLine(
"Number of Armstrong Strings are: "
+ count_armstrong(arr));
Console.WriteLine( "Number of Prime Strings are: "
+ count_prime(arr));
}
}
|
Javascript
<script>
function isPrime(num) {
let flag = false ;
if (num > 1) {
for (let i = 2; i < num; i++) {
if ((num % i) == 0) {
flag = true ;
break ;
}
}
}
if (flag)
return false ;
else
return true ;
}
function order(x) {
let n = 0;
while (x != 0) {
n = n + 1;
x = x / 10;
}
return n;
}
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;
}
return (sum1 == x);
}
function count_armstrong(li) {
let c = 0;
for (let ele of li) {
let val = 0;
for (let che of ele)
val += che.charCodeAt(0);
if (isArmstrong(val))
c += 1;
}
return c;
}
function count_prime(li) {
let c = 0;
for (let ele of li) {
let val = 0;
for (let che of ele)
val += che.charCodeAt(0);
if (isPrime(val))
c += 1;
}
return c;
}
let arr = [ "geeksforgeeks" , "a" , "computer" ,
"science" , "portal" , "for" , "geeks" ];
document.write( "Number of Armstrong Strings are: "
+ count_armstrong(arr) + "<br>" );
document.write( "Number of Prime Strings are: "
+ count_prime(arr) + "<br>" );
</script>
|
Output
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)
Share your thoughts in the comments
Please Login to comment...