质数串
给定一个字符串 str ,任务是检查所有字符的 ASCII 值之和是否为质数。
例:
Input : geeksforgeeks
Output : Yes
Input : GeeksForGeeks
Output : No
算法
- Calculate string length
- Calculate the sum of ASCII values of all characters
- Now we can check n 1/2 because the larger factor of n must be a multiple of the smaller factor. (For details, see Check whether the number is prime )
- If the sum is prime, print "Yes", otherwise print "No"
现将上述方法的实现给出如下:
c++
// 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;
}
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.
python 3
T2T19】c#T3T23】PHPT4
Javascript
T5T31】
输出:
No
版权属于:月萌API www.moonapi.com,转载请注明出处