Prime String

Given a String str , the task is to check if the sum of ASCII value of all characters is a Prime Number or not.

Examples :

Input  : geeksforgeeks
Output : Yes
Input  : GeeksForGeeks
Output : No

Algorithm

  1. Calculate the string length
  2. Calculate sum of ASCII value of all characters
  3. Now we can check till n1/2 because a larger factor of n must be a multiple of smaller factor. (Please see Check If the Number is Prime or Not for details)
  4. If the Sum is Prime then print “Yes” otherwise “No”

Now the implementation of above approach given below:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find if string is a
// Prime or not.
#include <bits/stdc++.h>
using namespace std;
  
// Function that checks if sum 
// is prime or not
bool isPrimeString(string str)
{
    int len = str.length(), n = 0;
    for (int i = 0; i < len; i++)
        n += (int)str[i];
  
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
  
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;
  
    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
  
    return true;
}
  
// Driver code
int main()
{
    string str = "geekRam";
    if (isPrimeString(str))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find if
// string is a Prime or not.
import java.io.*;
  
class GFG {
      
    // Function that checks if
    // sum is prime or not
    static boolean isPrimeString(String str)
    {
        int len = str.length(), n = 0;
        for (int i = 0; i < len; i++)
            n += (int)str.charAt(i);
      
        // Corner cases
        if (n <= 1)
            return false;
        if (n <= 3)
            return true;
      
        // This is checked so that we can skip
        // middle five numbers in below loop
        if (n % 2 == 0 || n % 3 == 0)
            return false;
      
        for (int i = 5; i * i <= n; i = i + 6)
            if (n % i == 0 || n % (i + 2) == 0)
                return false;
      
        return true;
    }
  
    // Driver code
    public static void main (String[] args)
    {
        String str = "geekRam";
          
        if (isPrimeString(str))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
  
// This code is contributed by Ajit.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find if string is
// a Prime or not
using System;
  
class GFG
{
  
// Function that checks if sum is prime or not 
public static bool isPrimeString(string str)
{
    int len = str.Length, n = 0;
    for (int i = 0; i < len; i++)
    {
        n += (int)str[i];
    }
  
    // Corner cases 
    if (n <= 1)
    {
        return false;
    }
    if (n <= 3)
    {
        return true;
    }
  
    // This is checked so that we can skip 
    // middle five numbers in below loop 
    if (n % 2 == 0 || n % 3 == 0)
    {
        return false;
    }
  
    for (int i = 5; i * i <= n; i = i + 6)
    {
        if (n % i == 0 || n % (i + 2) == 0)
        {
            return false;
        }
    }
  
    return true;
}
  
// Driver code 
public static void Main(string[] args)
{
    string str = "geekRam";
  
    if (isPrimeString(str))
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
  
// This code is contributed by Shrikant13

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find if 
// string is a Prime or not.
  
// Function that checks 
// if sum is prime or not
function isPrimeString($str)
{
    $len = strlen($str); $n = 0;
    for ($i = 0; $i < $len; $i++)
        $n += (int)$str[$i];
  
    // Corner cases
    if ($n <= 1)
        return false;
    if ($n <= 3)
        return true;
  
    // This is checked so that 
    // we can skip middle five
    // numbers in below loop
    if ($n % 2 == 0 || $n % 3 == 0)
        return false;
  
    for ($i = 5; $i * $i <= $n; $i = $i + 6)
        if ($n % $i == 0 || 
            $n % ($i + 2) == 0)
            return false;
  
    return true;
}
  
// Driver code
$str = "geekRam";
if (isPrimeString($str))
    echo "Yes" , "\n";
else
    echo "No" , "\n";
  
// This code is contributed by aj_36
?>

chevron_right



Output :

No


My Personal Notes arrow_drop_up

Sometimes you never realize the value of a moment until it becomes a memory

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t, shrikanth13