Prime String Last Updated : 20 Feb, 2023 Comments Improve Suggest changes Like Article Like Report 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 : geeksforgeeksOutput : Yes Input : GeeksForGeeksOutput : No Algorithm Calculate the string lengthCalculate sum of ASCII value of all charactersNow 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)If the Sum is Prime then print “Yes” otherwise “No” Now the implementation of above approach given below: 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. 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 <?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 ?> JavaScript <script> // Javascript program to find if string // is a Prime or not // Function that checks if sum is prime or not function isPrimeString(str) { let len = str.length, n = 0; for(let i = 0; i < len; i++) { n += str[i].charCodeAt(); } // 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(let i = 5; i * i <= n; i = i + 6) { if (n % i == 0 || n % (i + 2) == 0) { return false; } } return true; } // Driver code let str = "geekRam"; if (isPrimeString(str)) { document.write("Yes"); } else { document.write("No"); } // This code is contributed by decode2207 </script> OutputNo Time Complexity: O(n)Auxiliary Space: O(1), As constant extra space is used. Comment More infoAdvertise with us Next Article Prime String A ajay0007 Follow Improve Article Tags : Strings Technical Scripter DSA Basic Coding Problems Prime Number cpp-string ASCII +3 More Practice Tags : Prime NumberStrings Similar Reads Super Prime Given a positive integer n and the task is to print all the Super-Primes less than or equal to n. Super-prime numbers (also known as higher order primes) are the subsequence of prime number sequence that occupy prime-numbered positions within the sequence of all prime numbers. The first few super pr 6 min read Primorial Prime Primorial primes are a special type of prime numbers related to the product of the first few prime numbers, known as primorials. A Primorial, written as n#, is the product of the first n prime numbers. For example:The first prime number is 2.The first two prime numbers are 2 and 3, so p2# = 2 Ã 3 = 3 min read Left-Truncatable Prime A Left-truncatable prime is a prime which in a given base (say 10) does not contain 0 and which remains prime when the leading ("left") digit is successively removed. For example, 317 is left-truncatable prime since 317, 17 and 7 are all prime. There are total 4260 left-truncatable primes.The task i 13 min read Right-Truncatable Prime A Right-truncatable prime is a prime which remains prime when the last ("right") digit is successively removed. For example, 239 is right-truncatable prime since 239, 23 and 2 are all prime. There are 83 right-truncatable primes.The task is to check whether the given number (N > 0) is right-trunc 8 min read Prime or not in Python Every programmer comes across the problem of checking whether a number is prime and indeed it's one of the basic level program in any language. I also tried it in many different languages, but the best language that I found to implement it is Python. It's just one single line and the program does it 3 min read Like