An integer N consisting of five distinct nonzero digits has the curious property that it is equal to the sum of all the different three-digit integers formed by the three digit permutations of its five digits. Find N.
I'm assuming all digits must be different, so there are no repeated digits. Nonetheless, assume we have 60 possible three digit numbers available from the the initial five digit number. This is the permutation 5!/(5-3)!.
Now the tricky part. I didn't feel like writing code to brute-force the entire thing. So, I did a little deduction to determine an easier formula. If there are 60 possible three digit numbers and 5 digits, each digit in the five-digit number must appear 12 times in each order of magnitude to give all possible three-digit numbers. This gives you the notion that (1200+120+12) times the sum of the individual digits must be equal to the five-digit number. The only two solutions that work according to my code are 0 (which is illegal) and 35964.
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
long int number = 0;
for(int i = 0; i < 100000; i++)
{
int e = i % 10;
int x = i / 10;
int d = x % 10;
x = x / 10;
int c = x % 10;
x = x / 10;
int b = x % 10;
x = x / 10;
int a = x;
int y = a + b + c + d + e;
if (y*1332 == i){
cout << i;
getchar();
}
}
}
|
Posted by Eric
on 2004-06-30 09:34:21 |