Tuesday, June 2, 2015

UVa - 10533 - Digit Primes

Problem Link: 10533 - Digit Primes

  1. /****************#####    بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم   #####******************
  2. __________________________________________________________________________
  3. ######################  Ya-Seen Arafat(ACWizard) #########################
  4. ######################        UAP-CSE-33B        #########################
  5. *************************************************************************/
  6. #include <bits/stdc++.h>
  7. #define M 1000000
  8. using namespace std;
  9. bool prime[M];
  10. int main(){
  11.     for(int i = 2; i <= sqrt(M); i++)
  12.         if(prime[i] == 0)for(int j = i+i; j <= M; j += i)prime[j] = 1;
  13.     int t1, t2, t;
  14.     cin >> t;
  15.     while(t--){
  16.         cin >> t1, t2;
  17.         int low, high;
  18.         low = min(t1, t2);
  19.         high = max(t1, t2);
  20.         int cnt = 0;
  21.         for(int i = low; i <= high; i++){
  22.             if(prime[i] == 0){
  23.                 int sum = 0, j = i, temp;
  24.                 while(> 0){
  25.                     temp = i%10;
  26.                     sum += temp;
  27.                     j /= 10;
  28.                 }
  29.                 if(sum > 0 && prime[sum] == 0)cnt++;
  30.             }
  31.         }
  32.         cout << cnt << endl;
  33.     }
  34.     return 0;
  35. }

No comments:

Post a Comment