A friend gave this puzzle:
Find 3 numbers (say, A B C) such that
\$ABC+ABC+ABC = CCC\$
ABC means \$((A*100) + (B*10) + C)\$. Also, these 3 digits must be distinct.
I wrote a quick code to find out the answer:
int main(void) {
// your code goes here
int hun,dec,unit;
int final_num = 0;
int temp_num = 0;
for(hun = 0; hun <10; hun++)
{
for(dec = 0; dec < 10; dec++)
{
for(unit = 0; unit < 10; unit++)
{
// Calculate the number which would be formed with this unit place
final_num = (unit *100) + (unit * 10) + (unit);
// Values should not be same
if((unit!=dec) && (unit != hun) &&( dec != hun))
{
// Number with current combo of ABC
temp_num = ((hun * 100) + (dec * 10) + unit) * 3;
if(temp_num ==final_num )
{
printf("Number is %d \n",temp_num);
printf(" Hun %d Dec %d Unit %d",hun,dec,unit);
}
}
}
}
}
printf("\nEnd of File ");
return 0;
}
And the Answer was 185 (185 + 185 + 185 = 555).
That was very amateur code. Upon thinking, I realized I could have implemented equation \$300A + 30B + 3C = 111C\$. But still couldn't avoid for
loop.
What could be the better way to implement this? I know only C so I solved it with C.