HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

"Find the number" puzzle

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
numberthepuzzlefind

Problem

A friend gave this puzzle:

Find 3 numbers (say, A B C) such that


\$ABC+ABC+ABC = CCC\$

ABC means \$((A100) + (B10) + 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.

Solution

I'm sorry, but the best solution to this problem has to be a logic solution, not a code solution. There are only 2 digits that, when multiplied by 3, have the same last digit... 0, and 5. So, 0+0+0 is 0, and 5+5+5 is 15.

Since C cannot be 0, it means that C can only be 5. Now, if C is 5, and we know that there is a 'carry' of 1 in to the tens column, it means we need to find a number other than 5 that when added together 3 times ends in a 4.

There is only one value that does that, 8. 8 + 8 + 8 is 24, and with the carry of 1, we have 25 (and a carry of 2 in to the 100's column).

So, now we need a digit that sums three times to 3, and that's 1.

There is only one possible solution where ABC + ABC + ABC is CCC, and that's 185. It can be deduced using logic alone, and brute-forcing it is overkill.

Context

StackExchange Code Review Q#91416, answer score: 6

Revisions (0)

No revisions yet.