[Project Euler] Problem 51
2012-06-26 17:13
435 查看
Problem Description
By replacing the 1st digit of *3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime.By replacing the 3rd and 4th digits of 56**3 with the same digit, this 5-digit number is the first example having seven primes among the ten generated numbers, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.
Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family.
C++
This problem seems a bit hard to me at the first look. However, through several steps of analysis, I simplify this problem to some extent .#pragma once const int MAX_NUM = 1000000; const int TARGET_COUNT = 8; int* g_primeArray = NULL; int g_primeLength = 0; // pre decalaration bool CheckThisPrime(int prime, char sameChar, int sameCount); void Initialize() { g_primeLength = MAX_NUM / 5; g_primeArray = new int[g_primeLength]; MakePrimes(g_primeArray, g_primeLength, MAX_NUM); } // By analysis, we can infer that the result should has at least three digits with value 0 or 1 or 2, which are not including the lowest digit of the number. void Problem_51() { Initialize(); char digitArray[10] = { 0 }; int digitCount = 0; for(int i =0; i<g_primeLength; i++) { int curPrime = g_primeArray[i]; if(curPrime < 1000) continue; sprintf(digitArray, "%d", curPrime); digitCount = strlen(digitArray); int zeroCount = 0; int oneCount = 0; int twoCount = 0; for(int j =0; j<digitCount - 1; j++) { if(digitArray[j] == '0') zeroCount++; if(digitArray[j] == '1') oneCount++; if(digitArray[j] == '2') twoCount++; } if(zeroCount < 3 && oneCount < 3 && twoCount < 3) continue; printf("%d\n", curPrime); // int temp = 0; // digitArray[0] = '0'; // sscanf(digitArray, "%d", &temp); // printf("%d\n", temp); if(zeroCount >= 3) { if(CheckThisPrime(curPrime, '0', zeroCount)) { printf("result = %d\n", curPrime); return; } } if(oneCount >=3) { if(CheckThisPrime(curPrime,'1', oneCount)) { printf("result = %d\n", curPrime); return; } } if(twoCount >=3) { if(CheckThisPrime(curPrime, '2', twoCount)) { printf("result = %d\n", curPrime); return; } } } } bool CheckThisPrime(int prime, char sameChar, int sameCount) { bool ret = false; char digitArray[10] = { 0 }; int digitCount = 0; sprintf(digitArray, "%d", prime); digitCount = strlen(digitArray); int* digitIndexArray = new int[sameCount]; int index=0; for(int j =0; j<digitCount - 1; j++) { if(digitArray[j] == sameChar) digitIndexArray[index++] = j; } // determine which three digits should be modified bool* useArr = new bool[sameCount]; useArr[0] = true; useArr[1] = true; useArr[2] = true; while(true) { char digitArrayBak[10] = { 0 }; memcpy(digitArrayBak, digitArray, 10 * sizeof(char)); int primeCount = 1; int notPrimeCount = 0; int notPrimeMax = 2 - (sameChar - '0'); for(char c=(char)(sameChar + 1); c<='9' ; c++) { for(int i = 0; i<sameCount; i++) { if(useArr[i]) { digitArrayBak[digitIndexArray[i]] = c; } } int temp = 0; sscanf(digitArrayBak, "%d", &temp); if(IsPrime(temp)) { primeCount++; } else { notPrimeCount++; } if(notPrimeCount > notPrimeMax) { break; } if(primeCount == TARGET_COUNT) { ret = true; goto LRESULT; } } int reachEndCount = 0; for(int i = sameCount -1; i>=0; i--) { if(useArr[i]) { reachEndCount++; } else { break; } } if(reachEndCount == 3) break; for(int i = sameCount -1 - reachEndCount; i>=0; i--) { if(useArr[i]) { useArr[i] = false; useArr[i+1] = true; break; } } } LRESULT: delete[] digitIndexArray; delete[] useArr; return ret; }
相关文章推荐
- Project Euler:Problem 51 Prime digit replacements
- 【Project Euler】【Problem 3】Largest prime factor
- Project Euler:Problem 47 Distinct primes factors
- [Project Euler]加入欧拉 Problem 8
- [Project Euler] Problem 6
- [Project Euler] Problem 18
- Project Euler:Problem 63 Powerful digit counts
- Project Euler:Problem 76 Counting summations
- Codeforces Problem 51D - Geometrical problem
- Project Euler: Problem 17 Number letter counts
- Project Euler:Problem 23 Non-abundant sums
- Project Euler:Problem 33 Digit cancelling fractions
- Project Euler:Problem 42 Coded triangle numbers
- Project Euler - Problem 13
- Project Euler:Problem 56 Powerful digit sum
- Project Euler:Problem 66 Diophantine equation
- Project Euler:Problem 68 Magic 5-gon ring
- Project Euler:Problem 58 Spiral primes
- 【Project Euler】【Problem 4】Largest palindrome product
- Project Euler__problem 1