📜  素数字符串

📅  最后修改于: 2022-05-13 01:57:06.317000             🧑  作者: Mango

素数字符串

给定一个字符串str ,任务是检查所有字符的 ASCII 值的总和是否是质数。

例子 :

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

算法

  1. 计算字符串长度
  2. 计算所有字符的ASCII值之和
  3. 现在我们可以检查直到 n 1/2 ,因为 n 的较大因子必须是较小因子的倍数。 (请参阅检查数字是否为质数以获取详细信息)
  4. 如果总和是素数,则打印“是”,否则打印“否”

现在实现以下给出的上述方法:

C++
// C++ program to find if string is a
// Prime or not.
#include 
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;
}


Java
// 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.


Python3
# Python Program to find if string is Prime or not
# Function that checks if sum is prime or not
def isPrimeString(str1):
    len1 = len(str1)
    n = 0
    for i in range(len1):
        n += ord(str1[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 or n % 3 == 0:
        return False
    while(i * i <= n):
        if n % i == 0 or n % (i + 2) == 0:
            return False
        i += 6
    return True
 
# Driver code
str1 = "geekRam"
if(isPrimeString(str1)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by simranjenny84


C#
// 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


PHP


Javascript


输出 :

No