Permuted multiples | Project Euler | Problem #52
URL to the problem page: https://projecteuler.net/problem=52
It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.
Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.
It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.
Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.
#include <iostream>
using namespace std;
long long power(long long a, long long b) {
long long result = 1;
for (int i = 0; i < b; i++) {
result = result * a;
}
return result;
}
long long finddigits(long long a) {
long long counter = 1;
while (a >= 10) {
a /= 10;
counter++;
}
return counter;
}
int main()
{
long long number[6], i, j, a, b, counter, counter2, digitnumber[6], digits[6][20];
for (i = 1; i < 1000000; i++) {
counter = 0;
for (j = 0; j < 6; j++) {
number[j] = i * (j + 1);
digitnumber[j] = finddigits(number[j]);
}
for (a = 1; a < 6; a++) {
if (digitnumber[0] != digitnumber[a]) {
counter++;
break;
}
}
if (counter != 0) {
continue;
}
for (a = 0; a < 6; a++) {
for (j = digitnumber[0]; j > 0; j--) {
digits[a][j - 1] = (number[a] % power(10, j)) / (power(10, (j - 1)));
}
}
for (b = 1; b <= 5; b++) {
counter2 = 0;
for (a = 0; a < digitnumber[0]; a++) {
counter = 0;
for (j = 0; j < digitnumber[0]; j++) {
if (digits[0][a] == digits[b][j]) {
counter++;
}
}
if (counter != 1) {
counter2++;
break;
}
}
if (counter2 != 0) {
break;
}
}
if (counter2 == 0) {
cout << "Smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits is = " << i << endl;
break;
}
}
}
Comments
Post a Comment