If 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.
Input
One line containing two integers separated by space giving N1 and N2
Output
One integer M giving the number of primes P such that N1 <= P <= N2 that are such that when P is written in words, it has a prime number of letters.
Constraint
N2 <= 99999
Example 1
Input:
1 10
Output:
3
Explanation:
The primes between 1 and 10 and 2, 3, 5 and 7. Of these, 5 written in words is FIVE and has a non prime number of letters and others have prime number of letters (viz TWO, THREE and SEVEN).
Example 2
Input:
1100 1130
Output:
1
Explanation:
The primes between 1100 and 1130 are 1103, 1109, 1117, 1123 and 1129. When these are written in words, we get
ONE THOUSAND ONE HUNDRED AND THREE
ONE THOUSAND ONE HUNDRED AND NINE
ONE THOUSAND ONE HUNDRED AND SEVENTEEN
ONE THOUSAND ONE HUNDRED AND TWENTY THREE
ONE THOUSAND ONE HUNDRED AND TWENTY NINE
The count of characters in the above are 29, 28, 33, 35 and 34
Of these only for 1103 the count of characters is prime.
Answers
Answered by
1
#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;}
// 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;}
Similar questions