Advertisement
LtStaffel

Euler 51

Nov 15th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. #include "utils.h"
  2. #include <bitset>
  3.  
  4. using namespace std;
  5.  
  6. // GOAL: Find "family" of 8 primes separated only by the same kind of digit
  7. // 13, 23, 43, 53, 73, and 83
  8. // 56003, 56113, 56333, 56443, 56663, 56773, and 56993
  9. #define MAX 1000000
  10. #define FAM 8
  11.  
  12. int main() {
  13.     set<int> primes = sieve_primes(MAX);
  14.     set<int> f;
  15.  
  16.     for (auto p = primes.begin(); p != primes.end(); ++p) {
  17.         vector<int> vp = int_to_vect(*p);
  18.         unordered_set<int> ud = unique_digits(*p);
  19.  
  20.         for (auto d = ud.begin(); d != ud.end(); ++d) {
  21.             unordered_set<int> positions;
  22.  
  23.             for (int i = 0; i < vp.size(); i++) {
  24.                 if (vp[i] == *d) {
  25.                     positions.insert(i);
  26.                 }
  27.             }
  28.  
  29.             vector<int> vpcp = vp;
  30.             f.insert(*p);
  31.  
  32.             for (int i = 0; i <= 9; i++) {
  33.                 for (auto j = positions.begin(); j != positions.end(); ++j) {
  34.                     vpcp[*j] = i;
  35.                 }
  36.  
  37.                 int n = vect_to_int(vpcp);
  38.                
  39.                 if (primes.count(n) > 0 && num_digits(n) == num_digits(*p)) {
  40.                     f.insert(n);
  41.                 }
  42.             }
  43.  
  44.             if (f.size() >= FAM) {
  45.                 for (auto num = f.begin(); num != f.end(); ++num) {
  46.                     cout << *num << ' ';
  47.                 }
  48.                 cout << endl;
  49.                 p = primes.end();
  50.                 break;
  51.             }
  52.  
  53.             f.clear();
  54.         }
  55.     }
  56.  
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement