Prime Word Checker in C++

This C++ code implements the Sieve of Eratosthenes to check if the sum of the character values in a given word is prime. It assigns values to uppercase and lowercase letters and outputs whether the calculated sum corresponds to a prime number.
 avatar
nur
c_cpp
2 months ago
883 B
15
Indexable
#include<bits/stdc++.h>
using namespace std;
using ll = long long int;
#define mx 1000007
bool arr[mx+2]={0};
void siv()
{
    arr[0]=arr[1]=1;
    for(int i=4;i<=mx;i+=2)
    {
        arr[i]=1;
    }
    for(int i=3;i*i<=mx;i+=2)
    {
        if(arr[i]==0)
        {
            for(int j=i*i;j<=mx;j+=i)
            arr[j]=1;
        }
    }
}
int main()
{
    siv();
    string nur;
    while (cin>>nur)
    {
        int sum=0;
        for(int i=0;i<(int)nur.size();i++)
        {
            if(nur[i]<='Z')
            {
                sum+=nur[i]-'A'+27;

            }
            else
            {
                sum+=nur[i]-'a'+1;
            }
        }
        //cout << sum << "\n";
       if(arr[sum]==0)
       cout << "It is a prime word." <<"\n";
       else
       cout<< "It is not a prime word.\n";
    }
    
}
Editor is loading...
Leave a Comment