Not Yet Answered # puzzle

Discussion Starter viv0411 iamthwee 1,547 Sky Diploma 571 StuXYZ 731 iamthwee 1,547 StuXYZ 731 sundip -3

0

no dude. you didn't get me. i want the computer to search for possible combinations.

0

Interesting little challenge...

First thoughts would be to permute through all math signs (+,-,/,*)

Maybe a crude permutation generator would do well here.

```
++++
+++-
++--
+---
----
```

etc... the parenthesis adds extra complexity though.

*Edited 6 Years Ago by iamthwee*: n/a

0

Or get through it with a recursive function, End cases would be if either you finish all available values with you or total > required total.

2

Assuming the problem is slightly more general you have a list of number e.g.`int items[]={4,4,4,4};`

and you have a target number T.

Now you need to clarify the rules:

(i) are brackets acceptable: e.g. the given solution (4 / 4 + 4 )* 4 has to have brackets to get the problem to work. Since there is no solution that exists without that I guess you are allowing that.

If that is the case, the one way to approach the problem is to encode the operations and numbers on to a stack (reverse polish notation). Then you can iterate through the placement of a fixed number of operators and a fixed number of index values and

evaluate each step:

e.g. `1 2 3 + 4 * - ==> 1-(2+3)*4 =`

Now it is just a matter of keeping the values in the list in order and iterating through the operators.

Finally, I don't think this is a good problem for recursion, simply because if the solution for some part is above/below the result it doesn't help, you could divide or subtract if you have an intermediate solution that is above the target. However, you CAN produce a recursive results. But it is has to implement a hidden stack.

Finally, what about a different language: this problem is very simple in lisp :-)

*Edited 3 Years Ago by mike_2000_17*: Fixed formatting

0

If that is the case, the one way to approach the problem is to encode the operations and numbers on to a stack (reverse polish notation). Then you can iterate through the placement of a fixed number of operators and a fixed number of index values and

evaluate each step:

Not quite sure what you mean by this? Can you clarify this please?

0

In reference to the previous post: Please clarify:

It is that you can construct a stack of operations which are either "read value" or "binary op", the effect is that you can construct a stack , then evaluated it, then make a permutation on it. Since the stack can represent all possible solutions, it is a very easy thing to compute with.

E.g Consider the problem of values item={1,2,3,4} and operators P={+ - *}, your stack is {V,V P,V,P,V,P}. this is evaluated to see if it equals T. Then just iterate the item list, and after each full loop, iterate the operators list once. Obviously, after a full step of both step you can change the operators.

That example can be optimized for cases were you have order invariance but the basic form is there.

On the other hand, if you try to make a stack with brackets e.g. ( A + B) * C etc. Then you have the problem of how many brackets etc. The stack is not obviously iterable. [It can be done, it is just seems difficult]

However, I may have missed the easy way to do this problem. [In fact I feel I have]

*Edited 6 Years Ago by StuXYZ*: n/a

0

Just a concept:-

```
char operator[] = {'+','-','*','/'};
int item[] = {4,4,4,4}
int targetValue = 20;
for operator1 in operator
{
for operator2 in operator
{
for operator3 in operator
{
if((item[0] operator1 item[1] operator2 item[2] operator3 item[3]) == targetValue)
print "item[0] operator1 item[1] operator2 item[2] operator3 item[3]";
}
}
}
```

I hope it may be written in more optimize way

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"

Here is the sample output that I ...

Hello All ...

Iam Getting An Error With try to excecute the stored procedure .

I have Have Sql database , the stored procedure like so :

```
USE [MPRS]
GO
/****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE ...
```

I don’t want at this stage work on a big separate project as I've already got plenty ...