Use each of the numbers 1, 3, 4, and 6 exactly once with any of the four basic math operations (addition, subtraction, multiplication, and division) to total 24. Each number must be used once and only once, and you may define the order of operations; for example, 3 * (4 + 6) + 1 = 31 is valid, however incorrect, since it doesn't total 24.
I understand that some form of combinatorial logic can be used in order to solve the problem. The furthest i got to solving the problem was allocating an array for the numbers and manually figuring out the no. of possible solutions in which the operators could be used. My existing code is shown below. This is far too tedious and therefore am therefore requesting from help from more experienced programmers. Thanks in Advance!
Code:
#include <stdio.h>
int main()
{
int num[4]={1, 3, 4, 6};
int answer=0;
int i;
for (i=0; i<4; i++)
{
printf("Number %d in the array is %d\n", i, num[i]);
}
while(answer!=24)
{
answer = num[0] + num[1] + num[2] + num[3];
printf("\nAnswer is %d\n", answer);
answer = num[0] * num[1] * num[2] + num[3];
printf("Answer is %d\n", answer);
answer = (num[0] + num[1]) * num[2] + num[3];
printf("Answer is %d\n", answer);
answer = num[0] * num[1] + num[2] * num[3];
printf("Answer is %d\n", answer);
}
}