Find the sum of the ascii values of characters which are present at prime positions
Last Updated :
08 Feb, 2024
Given string str of size N, the task is to find the sum of all ASCII values of the characters that are present at prime positions.
Examples:
Input: str = “abcdef”
Output: 298
‘b’, ‘c’ and ‘e’ are the only characters which are
at prime positions i.e. 2, 3 and 5 respectively.
And sum of their ASCII values is 298.
Input: str = “geeksforgeeks”
Output: 644
Approach: An efficient approach is to traverse through the whole string and find if the particular position is prime or not. If the position of the current character is prime then add the ASCII value of the character to the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int n)
{
if (n == 0 || n == 1)
return false ;
for ( int i = 2; i * i <= n; i++)
if (n % i == 0)
return false ;
return true ;
}
int sumAscii(string str, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
if (isPrime(i + 1))
sum += ( int )(str[i]);
}
return sum;
}
int main()
{
string str = "geeksforgeeks" ;
int n = str.size();
cout << sumAscii(str, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean isPrime( int n)
{
if (n == 0 || n == 1 )
{
return false ;
}
for ( int i = 2 ; i * i <= n; i++)
{
if (n % i == 0 )
{
return false ;
}
}
return true ;
}
static int sumAscii(String str, int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (isPrime(i + 1 ))
{
sum += ( int ) (str.charAt(i));
}
}
return sum;
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
int n = str.length();
System.out.println(sumAscii(str, n));
}
}
|
Python3
from math import sqrt
def isPrime(n) :
if (n = = 0 or n = = 1 ) :
return False ;
for i in range ( 2 , int (sqrt(n)) + 1 ) :
if (n % i = = 0 ):
return False ;
return True ;
def sumAscii(string, n) :
sum = 0 ;
for i in range (n) :
if (isPrime(i + 1 )) :
sum + = ord (string[i]);
return sum ;
if __name__ = = "__main__" :
string = "geeksforgeeks" ;
n = len (string);
print (sumAscii(string, n));
|
C#
using System;
class GFG
{
static bool isPrime( int n)
{
if (n == 0 || n == 1)
{
return false ;
}
for ( int i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
return false ;
}
}
return true ;
}
static int sumAscii( string str, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
{
if (isPrime(i + 1))
{
sum += ( int ) (str[i]);
}
}
return sum;
}
public static void Main()
{
string str = "geeksforgeeks" ;
int n = str.Length;
Console.WriteLine(sumAscii(str, n));
}
}
|
Javascript
<script>
function isPrime(n)
{
if (n == 0 || n == 1)
return false ;
for (let i = 2; i * i <= n; i++)
if (n % i == 0)
return false ;
return true ;
}
function sumAscii(str, n)
{
let sum = 0;
for (let i = 0; i < n; i++) {
if (isPrime(i + 1))
sum += str.charCodeAt(i);
}
return sum;
}
let str = "geeksforgeeks" ;
let n = str.length;
document.write(sumAscii(str, n));
</script>
|
Time Complexity: O(n*sqrt(n)), where n represents the size of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Approach: Sieve of Eratosthenes
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int sumAscii(string str, int n) {
bool prime[n+1];
memset (prime, true , sizeof (prime));
prime[0] = false ;
prime[1] = false ;
for ( int i=2; i*i<=n; i++) {
if (prime[i]) {
for ( int j=i*i; j<=n; j+=i) {
prime[j] = false ;
}
}
}
int sum = 0;
for ( int i=0; i<n; i++) {
if (prime[i+1]) {
sum += ( int )str[i];
}
}
return sum;
}
int main() {
string str = "geeksforgeeks" ;
int n = str.size();
cout << sumAscii(str, n);
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
public static int sumAscii(String str, int n) {
boolean [] prime = new boolean [n + 1 ];
Arrays.fill(prime, true );
prime[ 0 ] = false ;
prime[ 1 ] = false ;
for ( int i = 2 ; i * i <= n; i++) {
if (prime[i]) {
for ( int j = i * i; j <= n; j += i) {
prime[j] = false ;
}
}
}
int sum = 0 ;
for ( int i = 0 ; i < n; i++) {
if (prime[i + 1 ]) {
sum += ( int ) str.charAt(i);
}
}
return sum;
}
public static void main(String[] args) {
String str = "geeksforgeeks" ;
int n = str.length();
System.out.println(sumAscii(str, n));
}
}
|
Python3
def sum_ascii(s):
n = len (s)
prime = [ True ] * (n + 1 )
prime[ 0 ] = prime[ 1 ] = False
for i in range ( 2 , int (n * * 0.5 ) + 1 ):
if prime[i]:
for j in range (i * i, n + 1 , i):
prime[j] = False
sum_chars = 0
for i in range (n):
if prime[i + 1 ]:
sum_chars + = ord (s[i])
return sum_chars
if __name__ = = "__main__" :
string = "geeksforgeeks"
print (sum_ascii(string))
|
C#
using System;
namespace PrimePositionSum
{
class GFG
{
static int SumAscii( string str, int n)
{
bool [] prime = new bool [n + 1];
Array.Fill(prime, true );
prime[0] = false ;
prime[1] = false ;
for ( int i = 2; i * i <= n; i++)
{
if (prime[i])
{
for ( int j = i * i; j <= n; j += i)
{
prime[j] = false ;
}
}
}
int sum = 0;
for ( int i = 0; i < n; i++)
{
if (prime[i + 1])
{
sum += ( int )str[i];
}
}
return sum;
}
static void Main( string [] args)
{
string str = "geeksforgeeks" ;
int n = str.Length;
Console.WriteLine(SumAscii(str, n));
}
}
}
|
Javascript
function sumAscii(str) {
const n = str.length;
const prime = new Array(n + 1).fill( true );
prime[0] = false ;
prime[1] = false ;
for (let i = 2; i * i <= n; i++) {
if (prime[i]) {
for (let j = i * i; j <= n; j += i) {
prime[j] = false ;
}
}
}
let sum = 0;
for (let i = 0; i < n; i++) {
if (prime[i + 1]) {
sum += str.charCodeAt(i);
}
}
return sum;
}
const str = "geeksforgeeks" ;
console.log(sumAscii(str));
|
Time Complexity: O(nlog(log(n)))
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...