Problem Description
The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property.
Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.
C++
This problem is interesting! I use Deep First Tradegy to resolve this problem.
const int MAX_NUM = 10000; int* g_primes = NULL; int g_length = 0; void InitPrimes() { g_length = MAX_NUM / 5; g_primes = new int[g_length]; MakePrimes(g_primes, g_length, MAX_NUM); } struct NumberWithWeight { int Number; int Weight; NumberWithWeight(int num, int weight) : Number(num), Weight(weight) { } NumberWithWeight() : Number(0), Weight(1) { } }; NumberWithWeight* g_result = new NumberWithWeight[5]; NumberWithWeight GetNumberWeight(int num) { int weight = 10; while(num / weight != 0) { weight *= 10; } return NumberWithWeight(num, weight); } bool CheckIsPrime(const NumberWithWeight& num1, const NumberWithWeight& num2) { int temp = num1.Number * num2.Weight + num2.Number; if(!IsPrime(temp)) { return false; } temp = num2.Number * num1.Weight + num1.Number; return IsPrime(temp); } map<int, vector<int>*> g_map; vector<int>* g_pVec; void Problem_60() { InitPrimes(); for(int i=0; i<g_length - 1; i++) { g_pVec = NULL; for(int j=i +1; j <g_length; j++) { NumberWithWeight nw1 = GetNumberWeight(g_primes[i]); NumberWithWeight nw2 = GetNumberWeight(g_primes[j]); if(CheckIsPrime(nw1, nw2)) { if(!g_pVec) { g_pVec = new vector<int>; } g_pVec->push_back(g_primes[j]); } } if(g_pVec) { g_map.insert(pair<int, vector<int>*>(g_primes[i], g_pVec)); } } struct Step { //map<int, vector<int>*>::iterator Key; int Key; int Index; Step(int key, int index) : Key(key), Index(index) { } bool Equals(const Step& other) { return ((Key == other.Key) && (Index == other.Index)); } bool operator== (const Step& other) { return Equals(other); } bool operator != (const Step& other) { return !Equals(other); } }; list<Step> vecStep; vecStep.push_back(Step(g_map.begin()->first, 0)); while(vecStep.size() <5) { assert(!vecStep.empty()); Step lastStep = vecStep.back(); int index = lastStep.Index; int key = lastStep.Key; vector<int>* pVec = g_map[key]; int nextKey = (*pVec)[index]; bool isNextKeyOK = true; list<Step>::iterator iterStep = vecStep.begin(); list<Step>::iterator iterLastStep = vecStep.end(); iterLastStep--; while(iterStep != iterLastStep) { vector<int>* pCur = g_map[iterStep->Key]; vector<int>::iterator retIter = find(pCur->begin(), pCur->end(), nextKey); if(retIter == pCur->end()) { isNextKeyOK = false; break; } iterStep++; } isNextKeyOK = isNextKeyOK & (bool)g_map.count(nextKey); if(isNextKeyOK) { vecStep.push_back(Step(nextKey, 0)); continue; } else { index++; while(index >= pVec->size()) { vecStep.pop_back(); if(vecStep.empty()) { map<int, vector<int>*>::iterator last = g_map.find(lastStep.Key); last++; if(last != g_map.end()) { vecStep.push_back(Step(last->first, 0)); } } lastStep = vecStep.back(); index = lastStep.Index; key = lastStep.Key; pVec = g_map[key]; index++; } vecStep.back().Index = index; continue; } } int result = 0; list<Step>::iterator iterStep = vecStep.begin(); while(iterStep != vecStep.end()) { result += iterStep->Key; iterStep++; } getchar(); }
I‘m going to put Project Euler away for a while, cause more meanning work is waiting for me.