fork(1) download
  1. #include <iostream>
  2. #include <set>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. set<int> expand(const set<int> &current, const vector<int> &primes, int limit) {
  7. set<int> res(current.begin(), current.end());
  8. for (auto n : current) {
  9. for (auto p : primes) {
  10. int x = n*p;
  11. if (x < limit) {
  12. res.insert(x);
  13. }
  14. }
  15. }
  16. return res;
  17. }
  18.  
  19. int main() {
  20. vector<int> primes = { 2, 3, 5, 7 };
  21. set<int> s;
  22. s.insert(1);
  23. int target = 5917;
  24. int best = 2*5917;
  25. int i = 1;
  26. for (;;) {
  27. set<int> next = expand(s, primes, best);
  28. if (next.size() == s.size()) {
  29. break;
  30. }
  31. cout << "Generation " << i++ << ", best = " << best << endl;
  32. for (auto n : next) {
  33. if (n >= target && n < best) {
  34. best = n;
  35. }
  36. }
  37. s = next;
  38. }
  39. cout << "Best match: " << best << endl;
  40. return 0;
  41. }
Success #stdin #stdout 0.01s 3464KB
stdin
Standard input is empty
stdout
Generation 1, best = 11834
Generation 2, best = 11834
Generation 3, best = 11834
Generation 4, best = 11834
Generation 5, best = 11834
Generation 6, best = 6125
Generation 7, best = 6125
Generation 8, best = 6075
Generation 9, best = 6000
Generation 10, best = 6000
Generation 11, best = 6000
Generation 12, best = 6000
Best match: 6000