Using My Developer Brain Instead of My Math Brain

Tonight I had a huge flashback to high school after trying to help my step-daughter finish her math homework.  She had a problem where she was supposed to figure out which combination of operations could be used to solve the problem.

15 _ 3 _ 17 _ 11 = 205 You would replace the _ spaces with either addition, subtraction, multiplication or division.

After a few minutes of discussion we decided that she should try a few combinations and see if she could figure it out on her own.  She tried about 20 combinations and then started to get a little frustrated.  In an effort to help I decided that I would try to put together a quick Excel sheet that would compute the answer.  I quickly found out that it’s difficult to get Excel to evaluate formulas dynamically.  It works great if the numbers change, but terrible if the operators change.

This is where my flash-back kicked in.  I remember many times in high school where I was having problems math homework and would go write myself a pascal program to solve the problem.  I never was great at math, but I could break it down into small steps and brute force my way through!

Tonight, I did that again.  I put together a quick console application in C# which generates a permutation and then utilizes XPath to create a dynamic equation and evaluate the result.  If the result matches 205 it can stop.  Otherwise it must try another permutation.

Is there a more elegant solution to this problem in C#?



  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     class Program
  10.     {
  11.  
  12.         public static double Evaluate(string expression)
  13.         {
  14.             return (double)new System.Xml.XPath.XPathDocument
  15.             (new StringReader("")).CreateNavigator().Evaluate
  16.             (string.Format("number({0})", new
  17.             System.Text.RegularExpressions.Regex(@"([\+\-\*])")
  18.             .Replace(expression, " ${1} ")
  19.             .Replace("/", " div ")
  20.             .Replace("%", " mod ")));
  21.         }
  22.  
  23.         static void Main(string[] args)
  24.         {
  25.             List operations = new List();
  26.  
  27.             operations.Add("+");
  28.             operations.Add("-");
  29.             operations.Add("*");
  30.             operations.Add("/");
  31.  
  32.             double output = 0;
  33.             int ctr1 = -1;
  34.             int ctr2 = 0;
  35.             int ctr3 = 0;
  36.             string math = "";
  37.  
  38.             while (output != 205)
  39.             {
  40.                 ctr1++;
  41.  
  42.                 if (ctr1 > 3)
  43.                 {
  44.                     ctr2++;
  45.                     ctr1 = 0;
  46.                 }
  47.  
  48.                 if (ctr2 > 3)
  49.                 {
  50.                     ctr3++;
  51.                     ctr2 = 0;
  52.                 }
  53.  
  54.                 if (ctr3 > 3)
  55.                     ctr3 = 0;
  56.  
  57.                 math = "15" + operations[ctr1] + "3" + operations[ctr2] + "17" + operations[ctr3]+"11";
  58.                 output = Evaluate(math);
  59.                 Console.Write(math + "=" + output + "\n");
  60.             }
  61.             Console.ReadKey();
  62.         }
  63.     }
  64. }
  • It’s quite simple math. 15+3+17*11=205. I spent about 30 second to solve it.

    About your code, I think easier to use 3 nested cycles:

    for (ctr1 = 0; ctr1 < 3; ctr1++)
    for (ctr2 = 0; ctr2 < 3; ctr2++)
    for (ctr3 = 0; ctr3 < 3; ctr3++)
    {
    math = "15" + operations[ctr1] + "3" + operations[ctr2] + "17" + operations[ctr3]+"11";
    output = Evaluate(math);
    if (output == 205) Console.Write(math + "=" + output + "\n");
    }

    • aaroneden

      Thanks for taking the time to respond Andriy. Yes, I agree it’s simple math, but when trying to teach a 10 year old how to quickly figure out the solution that’s a different story.

      Thanks for the code recommendation, I agree nesting 3 loops probably is a little less verbose.

  • Kaushal

    I don’t know C#, but its pretty easy to do using shell scripting (other than the frustrations with escaping * in shell):

    ops=”+ – / *”
    for o1 in $ops
    do
    for o2 in $ops
    do
    for o3 in $ops
    do
    formula=”15 ${o1} 3 ${o2} 17 ${o3} 11″
    res=`eval expr $formula`
    if [ “$res” = “205” ]; then
    echo $formula = $res
    fi
    done
    done
    done