Thread: Challenging Problem

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    1

    Challenging Problem

    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 &#37;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);
    
    
    
    
    
    
    
        }
    
    
    
    
    }
    Last edited by gamx; 05-25-2008 at 05:38 AM.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Why not try a basic approach like:

    (pseudo-code)
    Code:
    loop: select nb1 (4 choices)
      loop: selectOperator1 (4 choices)
        loop: select nb2 (!=nb1) (3 choices)
          loop: selectOperator2 (4 choices)
            ...
              compute* & exit if result is 24
    This is not really subtle but there is only...6144 combinations if I'm not wrong so it should not be that long to execute.

    Otherwise you could also try an algorithm that dynamically chooses the next operand/operator on order to converge...but it's getting a bit more difficult (you have to proove the algorithm does terminate).

    *evaluation order from left to right for instance, so for your example: 3 * (4 + 6) + 1, it corresponds to the case 4+6*3+1 (evaluated as ((4+6)*3)+1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM