Last active
August 29, 2015 14:20
-
-
Save airbreather/926c2d3455d14d0bc6d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires: https://www.nuget.org/packages/ExpressionEvaluator/