Computer Science, asked by KBhavithaSri, 1 year ago

f you like numbers, you may have been fascinated by prime numbers. These are numbers that have no divisors other than 1 and themselves. If we consider the primes 2 and 3, and write them in words, we write TWO and THREE. Both have a prime number of letters in their spelling. Not all prime numbers have this property.

Write a program to count the number of primes between a given pair of integers (including the given integers if they are primes) that have a prime number of characters when written in words. The blanks are not counted when we write the numbers in words. For example, ONE HUNDRED AND THREE has only 18 characters.

Answers

Answered by lakshmansai999othc0k
0
#include <bits/stdc++.h>using namespace std;
// strings at index 0 is not used, it is to make array// indexing simplestring one[] = { "", "one ", "two ", "three ", "four ",                 "five ", "six ", "seven ", "eight ",                 "nine ", "ten ", "eleven ", "twelve ",                 "thirteen ", "fourteen ", "fifteen ",                 "sixteen ", "seventeen ", "eighteen ",                 "nineteen "               };
// strings at index 0 and 1 are not used, they is to// make array indexing simplestring ten[] = { "", "", "twenty ", "thirty ", "forty ",                 "fifty ", "sixty ", "seventy ", "eighty ",                 "ninety "               };
// n is 1- or 2-digit numberstring numToWords(int n, string s){    string str = "";    // if n is more than 19, divide it    if (n > 19)        str += ten[n / 10] + one[n % 10];    else        str += one[n];
    // if n is non-zero    if (n)        str += s;
    return str;}
// Function to print a given number in wordsstring convertToWords(long n){    // stores word representation of given number n    string out;
    // handles digits at ten millions and hundred    // millions places (if any)    out += numToWords((n / 10000000), "crore ");
    // handles digits at hundred thousands and one    // millions places (if any)    out += numToWords(((n / 100000) % 100), "lakh ");
    // handles digits at thousands and tens thousands    // places (if any)    out += numToWords(((n / 1000) % 100), "thousand ");
    // handles digit at hundreds places (if any)    out += numToWords(((n / 100) % 10), "hundred ");
    if (n > 100 && n % 100)        out += "and ";
    // handles digits at ones and tens places (if any)    out += numToWords((n % 100), "");
    return out;}
// Driver code
int calculateSpaces(string str){
int count=0; for(unsigned int i=0;i<str.size();i++){
if(str[i]==' ') count++;

} return count;


}
int checkPrime(int num){
if(num%2==0) return false; for(int i=3;i<=sqrt(num);i+=2) if(num%i==0)  return false;
return true;


}int main(){ int m,n,prcount;

     cin>>m;     cin>>n;     prcount=0;     for(int i=m;i<=n;i++)      {      if(checkPrime(i))      {
   string numstr=convertToWords(i);    int len=numstr.length();   // cout<<numstr<<numstr.length()<<" spaces "<<calculateSpaces(numstr)<<endl;    int spaces=calculateSpaces(numstr);    //cout<<len-spaces<<endl;    if(checkPrime(len-spaces))     prcount++;   // cout<<;
}
}cout<<prcount<<endl;    return 0;}

lakshmansai999othc0k: ur writting tcscodevita right now i think
Similar questions