Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Thursday, 13 January 2011

From the Guernsey adult literacy project, page 51.

What arithmetical symbols do you have to insert in between the numbers to get the correct result?

1 3 5 7 9 = 3

1 3 5 7 9 = 47

1 3 5 7 9 = 18

Note that only the symbols +, –, * and / are used.

An interesting challenge – do they mean that operator precedence is invoked too?  ie MIDAS – Multiply before divide before add before subtract?

http://msdn.microsoft.com/en-us/library/aa691323(v=vs.71).aspx  - order of all operators

image

a very useful sanity check in google..

 

Answers I found are:

-1-3+5-7+9 = (-1) - 3 + 5 - 7 + 9 = 3

1+3*5-7+9 = 1 + (3 * 5) - 7 + 9 = 18
-1*3+5+7+9 = 181 + (3 * 5) - 7 + 9 = 18

1*3+5*7+9 =  (1 * 3) + (5 * 7) + 9 = 47
-1-3*5+7*9 =(-1) - (3 * 5) + (7 * 9) = 47

Code I used is:

string opZ = "";
string opA = "";
string opB = "";
string opC = "";
string opD = "";
for (int h = 1; h <= 2; h++) // making the first number positive or negative
{
if (h == 1) opZ = "";
if (h == 2) opZ = "-";

for (int i = 1; i <= 4; i++)
{
if (i == 1) opA = "*";
if (i == 2) opA = "/";
if (i == 3) opA = "+";
if (i == 4) opA = "-";
for (int j = 1; j <= 4; j++)
{
if (j == 1) opB = "*";
if (j == 2) opB = "/";
if (j == 3) opB = "+";
if (j == 4) opB = "-";
for (int k = 1; k <= 4; k++)
{
if (k == 1) opC = "*";
if (k == 2) opC = "/";
if (k == 3) opC = "+";
if (k == 4) opC = "-";
for (int l = 1; l <= 4; l++)
{
if (l == 1) opD = "*";
if (l == 2) opD = "/";
if (l == 3) opD = "+";
if (l == 4) opD = "-";
string expression = opZ + 1 + opA + 3 + opB + 5 + opC + 7 + opD + 9;
DataTable dummy = new DataTable();
double result = Convert.ToDouble(dummy.Compute(expression, string.Empty));
if (result == 3)
Debug.WriteLine(expression + " = 3");

if (result == 47)
Debug.WriteLine(expression + " = 47");

if (result == 18)
Debug.WriteLine(expression + " = 18");

}
}
}
}
}

thanks to http://stackoverflow.com/questions/174664/operators-as-strings for the DataTable help.

From http://stackoverflow.com/questions/4679056/code-smell-in-dynamic-expression

TDD

To help in the refactoring I used tests to make sure I hadn’t broken anything:

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data;
using System.Diagnostics;

namespace Guernsey1
{
/// <summary>
/// What arithmetical symbols do you have to insert in between the numbers to get the correct result?
/// 1 3 5 7 9 = 3
/// 1 3 5 7 9 = 47
/// 1 3 5 7 9 = 18
/// There may be more than one answer for each one.
/// </summary>
[TestClass]
public class UnitTest1
{
[TestMethod]
public void GetAnswersForResultIs3()
{
List<string> listOfExpressions = findArithmeticSymbolsInNumericOrder13579ThatGivesThisResult(3);
foreach (string expression in listOfExpressions)
Assert.AreEqual("-1-3+5-7+9", expression);
}

[TestMethod]
public void GetAnswersForResultIs47()
{
List<string> listOfExpressions = findArithmeticSymbolsInNumericOrder13579ThatGivesThisResult(47);
Assert.AreEqual(true, listOfExpressions.Contains("1*3+5*7+9"));
Assert.AreEqual(true, listOfExpressions.Contains("-1-3*5+7*9"));
}

[TestMethod]
public void GetAnswersForResultIs18()
{
List<string> listOfExpressions = findArithmeticSymbolsInNumericOrder13579ThatGivesThisResult(18);
Assert.AreEqual(true, listOfExpressions.Contains("-1*3+5+7+9"));
Assert.AreEqual(true, listOfExpressions.Contains("1+3*5-7+9"));
}



public List<string> findArithmeticSymbolsInNumericOrder13579ThatGivesThisResult(int resultExpected) // Jons
{
List<string> listOfExpressions = new List<string>();
string[] prefixes = { "", "-" };
string[] operators = { "*", "/", "+", "-" };
IEnumerable<string> expressions = from prefix in prefixes
from opA in operators
from opB in operators
from opC in operators
from opD in operators
select prefix + 1 + opA + 3 + opB + 5 + opC + 7 + opD + 9;

foreach (string expression in expressions)
{
DataTable dummy = new DataTable();
double result = Convert.ToDouble(dummy.Compute(expression, string.Empty));
if (result == resultExpected)
listOfExpressions.Add(expression);
}
return listOfExpressions;
}

public List<string> findArithmeticSymbolsInNumericOrder13579ThatGivesThisResult(int resultExpected) // Cines
{
string[] prefixes = { "", "-" };
string[] operators = { "*", "/", "+", "-" };
List<string> listOfExpressions = new List<string>();
foreach (string opA in operators)
foreach (string opB in operators)
foreach (string opC in operators)
foreach (string opD in operators)
foreach (string prefix in prefixes)
{
string expression = prefix + 1 + opA + 3 + opB + 5 + opC + 7 + opD + 9;
DataTable dummy = new DataTable();
double result = Convert.ToDouble(dummy.Compute(expression, string.Empty));
if (result == resultExpected)
listOfExpressions.Add(expression);
}
return listOfExpressions;
}


public List<string> findArithmeticSymbolsInNumericOrder13579ThatGivesThisResult(int resultExpected) // Anton1
{
List<string> listOfExpressions = new List<string>();
var calculator = new DataTable();
var operators = "*/+-";
for (int i = 0; i < 0x200; ++i)
{
string expression = String.Format("{0}1{1}3{2}5{3}7{4}9",
(i & 0x100) != 0 ? "-" : "",
operators[(i >> 0) & 3],
operators[(i >> 2) & 3],
operators[(i >> 4) & 3],
operators[(i >> 6) & 3]);

var result = calculator.Compute(expression, String.Empty);
if (Convert.ToDouble(result) == resultExpected)
listOfExpressions.Add(expression);
}
return listOfExpressions;
}

public List<string> findArithmeticSymbolsInNumericOrder13579ThatGivesThisResult(int resultExpected) // Anton2 - not working yet.. how to get the listOfExpressions?
{
List<string> listOfExpressions = new List<string>();
var operators = new Func<int, int, int>[] {
(a, b) => a + b,
(a, b) => a - b,
(a, b) => a * b,
(a, b) => a / b
};

for (int i = 0; i < 0x200; ++i)
{
var stack = 0; // max stack depth is 2 because only 2 priorities
var last = 0; // imitate + for lowest precedence
var value = (i & 0x100) != 0 ? -1 : 1;

for (int j = 0; j < 5; ++j) // extra item to force last reduction
{
var oper = (i >> j * 2) & 3; // "input" operator
if (oper / 2 <= last / 2) // reduce?
{
stack = operators[last](stack, value);
}
else // stack is empty; prepare to shift
stack = value;

if (j == 4) break;

last = oper; // shift operator
value = 3 + j * 2; // "input" number
}
}
// result in stack

return listOfExpressions;
}
}
}

| | #