Prime String
Last Updated :
20 Feb, 2023
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
- Calculate the string length
- Calculate sum of ASCII value of all characters
- 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)
- If the Sum is Prime then print “Yes” otherwise “No”
Now the implementation of above approach given below:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrimeString(string str)
{
int len = str.length(), n = 0;
for ( int i = 0; i < len; i++)
n += ( int )str[i];
if (n <= 1)
return false ;
if (n <= 3)
return true ;
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 ;
}
int main()
{
string str = "geekRam" ;
if (isPrimeString(str))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
|
Java
import java.io.*;
class GFG {
static boolean isPrimeString(String str)
{
int len = str.length(), n = 0 ;
for ( int i = 0 ; i < len; i++)
n += ( int )str.charAt(i);
if (n <= 1 )
return false ;
if (n <= 3 )
return true ;
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 ;
}
public static void main (String[] args)
{
String str = "geekRam" ;
if (isPrimeString(str))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isPrimeString(str1):
len1 = len (str1)
n = 0
for i in range (len1):
n + = ord (str1[i])
if n < = 1 :
return False
if n < = 3 :
return True
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
str1 = "geekRam"
if (isPrimeString(str1)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
public static bool isPrimeString( string str)
{
int len = str.Length, n = 0;
for ( int i = 0; i < len; i++)
{
n += ( int )str[i];
}
if (n <= 1)
{
return false ;
}
if (n <= 3)
{
return true ;
}
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 ;
}
public static void Main( string [] args)
{
string str = "geekRam" ;
if (isPrimeString(str))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
PHP
<?php
function isPrimeString( $str )
{
$len = strlen ( $str ); $n = 0;
for ( $i = 0; $i < $len ; $i ++)
$n += (int) $str [ $i ];
if ( $n <= 1)
return false;
if ( $n <= 3)
return true;
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;
}
$str = "geekRam" ;
if (isPrimeString( $str ))
echo "Yes" , "\n" ;
else
echo "No" , "\n" ;
?>
|
Javascript
<script>
function isPrimeString(str)
{
let len = str.length, n = 0;
for (let i = 0; i < len; i++)
{
n += str[i].charCodeAt();
}
if (n <= 1)
{
return false ;
}
if (n <= 3)
{
return true ;
}
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 ;
}
let str = "geekRam" ;
if (isPrimeString(str))
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1), As constant extra space is used.
Share your thoughts in the comments
Please Login to comment...