Project Euler Problem 38
阿新 • • 發佈:2018-11-02
Problem 38 : Pandigital multiples
Take the number 192 and multiply it by each of 1, 2, and 3:
192 × 1 = 192
192 × 2 = 384
192 × 3 = 576
By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1,2,3)
The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is the concatenated product of 9 and (1,2,3,4,5).
What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2, … , n) where n > 1?
#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;
// #define UNIT_TEST
class PE0038
{
private:
int m_digits[6];
int m_productDigits[16];
int m_numOfProductDigits;
int getDigits(int number);
bool checkValidPandigital(const int productDigits[]);
bool checkPandigital(int number, int max_value);
public:
long int findLargestConcatenatingPandigital();
};
int PE0038::getDigits (int number)
{
int numOfDigits = 0;
while(number> 0)
{
m_digits[numOfDigits++] = number%10;
number /= 10;
}
return numOfDigits;
}
bool PE0038::checkValidPandigital(const int productDigits[])
{
for(int i=0; i<9; i++)
{
if (productDigits[i] != i+1)
{
return false;
}
}
return true;
}
bool PE0038::checkPandigital(int number, int max_multiplier)
{
int product;
m_numOfProductDigits = 0;
for(int i=1; i<=max_multiplier; i++)
{
product = number * i;
int numOfDigits = getDigits(product);
for(int i=numOfDigits-1; i>=0; i--)
{
m_productDigits[m_numOfProductDigits++] = m_digits[i];
}
}
if (9 == m_numOfProductDigits)
{
int tmpProductDigits[9];
memcpy(tmpProductDigits, m_productDigits, 9*sizeof(int));
sort(tmpProductDigits, tmpProductDigits+9);
if (true == checkValidPandigital(tmpProductDigits))
{
#ifdef UNIT_TEST
cout << "pandigital: " << number << " (1.." << max_multiplier << ") ";
for(int i=0; i<m_numOfProductDigits; i++)
{
cout << m_productDigits[i];
}
cout << endl;
#endif
return true;
}
}
return false;
}
long int PE0038::findLargestConcatenatingPandigital()
{
long int value;
long int largest_value = 0;
int largest_n, largest_number;
int max_number = 10000; //max number 10000 when n=2
for (int number=9;number<max_number; number++)
{
for (int n=2;n<=5;n++)
{
if (true == checkPandigital(number, n))
{
value = 0;
for(int i=0; i<9; i++)
{
value = 10*value + m_productDigits[i];
}
if (largest_value < value)
{
largest_value = value;
largest_n = n;
largest_number = number;
}
}
}
}
#ifdef UNIT_TEST
cout << "The number " << largest_number << " is multiplied by each of " << endl;
cout << "(1,...," << largest_n <<") and the concatenated pandigital is ";
cout << largest_value << endl;
#endif
return largest_value;
}
int main()
{
clock_t start = clock();
PE0038 pe0038;
cout << pe0038.findLargestConcatenatingPandigital() << " is the largest 1 to 9 " ;
cout << "pandigital 9-digit number " << endl;
cout << "that can be formed as the concatenated product of an integer " << endl;
cout << "with (1,2, ... , n) where n > 1" << endl;
clock_t finish = clock();
double duration = (double)(finish - start) / CLOCKS_PER_SEC;
cout << "C/C++ application running time: " << duration << " seconds" << endl;
return 0;
}