Skip to content

Instantly share code, notes, and snippets.

@airbreather
Last active August 29, 2015 14:20
Show Gist options
  • Save airbreather/926c2d3455d14d0bc6d0 to your computer and use it in GitHub Desktop.
Save airbreather/926c2d3455d14d0bc6d0 to your computer and use it in GitHub Desktop.
using System;
using System.Text;
using System.Threading.Tasks;
using SimpleExpressionEvaluator;
namespace ConsoleApplication1
{
internal static class Program
{
private const int Target = 100;
private static readonly int[] Digits = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
private static readonly string[] Ops = { "", "+", "-" };
private static void Main()
{
int solutionCount = (int)Math.Pow(Ops.Length, Digits.Length - 1);
Parallel.For(0, solutionCount, solutionIndex =>
{
ExpressionEvaluator e = new ExpressionEvaluator();
StringBuilder sb = new StringBuilder();
sb.Append(Digits[0]);
for (int i = 1; i < Digits.Length; i++)
{
sb.Append(Ops[solutionIndex % Ops.Length]);
solutionIndex /= Ops.Length;
sb.Append(Digits[i]);
}
string exp = sb.ToString();
decimal result = e.Evaluate(exp);
if (result == Target)
{
Console.WriteLine(exp);
}
});
}
}
}
@airbreather
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment