## 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
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.
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.             }