Guest User

Untitled

a guest
May 24th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. int p10[] = {1,10,100,1000,10000,100000,1000000,10000000};
  6.  
  7. const int BOUND = 1000001;
  8.  
  9. bool is_prime[BOUND];
  10.  
  11. int digits[] = {1,3,7,9};
  12.  
  13. int digit_count(int n){ return n > 9 ? 1 + digit_count(n / 10) : 1; }
  14.  
  15. bool is_circular(int n){
  16. int d = digit_count(n);
  17. for( int i = 0; i < d; i++ ){
  18. if( !is_prime[n] ) return false;
  19. n += (n%10) * p10[d];
  20. n /= 10;
  21. }
  22. return true;
  23. }
  24. int circular_count(int n = 0){
  25. int ret = 0;
  26. if( n < BOUND ){
  27. if( is_circular(n) ) ret++;
  28. for( int i = 0; i < 4; i++ ) ret += circular_count(10*n + digits[i]);
  29. }
  30. return ret;
  31. }
  32. void calc_sieve(){
  33. fill(is_prime,is_prime+BOUND,true);
  34. is_prime[0] = is_prime[1] = false;
  35. for( int p = 2; p*p < BOUND; p++ ) if( is_prime[p] ){
  36. for( int m = p*p; m < BOUND; m += p ) is_prime[m] = false;
  37. }
  38. }
  39. int main(){
  40. calc_sieve();
  41. cout << circular_count() + 2 << endl;
  42. }
Add Comment
Please, Sign In to add comment