## Recommended Posts

I was wondering if there is an efficient premade algorithm for determining if the sum/difference of a group of numbers can equal a different number. Example:

5, 8, 10, 2, using + or -, to equal 9. 5 - 8 = -3 + 10 = 7 + 2 = 9

It is somewhere nearer to Knapsack problem.

##### Share on other sites

The sum/diff of any two numbers in the group, or the sum/diff of any number of numbers in the group? Are all numbers in the group unique? Can there be negative numbers? Is there a known boundary to the numbers? Is the special number guaranteed to be between the highest and the lowest number in the group?

Roses are FF0000, violets are 0000FF... All my base are belong to you.

##### Share on other sites
```Example data:

151.15   --> is the number we need ti get by doing arithmetic operations(only add or subtract) on below set of numbers (each number can be used any number of times).

Set of numbers:

2.77

8.24

2.77

3.48

3.48

3.99

3.48

3.48

3.48

17.4

3.48

3.48

3.48

2.77

4.85

5.67

17.67

2.77

3.48

2.77

3.58

2.77

3.48

2.77

2.77

3.48

3.48

2.77

3.48

3.48

3.48

3.48

3.48

2.77

2.77

3.48

3.48

2.77

3.48

3.48

3.48

3.48

4.8

5

6.76

3.48

3.48

3.48

14.4

17.94

4.85

3.48

3.48

3.48

3.48

8.39

4.58

4.85

3.48

2.77

3.48

2.77

2.77

3.48

3.48

3.48

4.03

3.48

2.77

21.76

3.48

3.48

10.03

3.48

2.77

9.21

2.77

3.65

1.23

1.23

1.11

0.37

0.47

0.47

0.54

0.47

0.47

0.47

2.34

0.47

0.47

0.47

0.37

0.65

0.76

2.37

0.37

0.47

0.37

0.48

1.23

1.33

1.23

0.37

0.47

0.47

0.37

0.47

0.47

1.33

1.33

0.47

0.37

0.37

0.47

0.47

0.37

0.47

0.47

0.47

0.47

0.64

0.67

0.91

0.47

0.47

0.47

1.94

2.41

1.51

1.33

0.47

0.47

0.47

1.13

1.48

0.65

0.47

0.37

0.47

0.37

0.37

1.33

0.47

0.47

0.54

0.47

0.37

2.92

0.47

0.47

1.35

0.47

0.37

1.24

0.37

0.46```

Edited by Melba23

##### Share on other sites

There's nothing "premade" you're going to find here. In all case, scale up all values (including target) to integers to avoid floating-point issues (here, * 100), then feed the baby to a classical knapsack variant. Of course you know it's NP-Complete.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites

This is what I did meanwhile for your initial post:

```;Brute force method

Global \$aIntegers = [5, 8, 10, 2]
ConsoleWrite(BruteForce_Calc(\$aIntegers, 1) & "=" & 1 & @CRLF)
ConsoleWrite(BruteForce_Calc(\$aIntegers, 9) & "=" & 9 & @CRLF)
ConsoleWrite(BruteForce_Calc(\$aIntegers, 15) & "=" & 15 & @CRLF)
ConsoleWrite(BruteForce_Calc(\$aIntegers, -11) & "=" & -11 & @CRLF)

Func BruteForce_Calc(ByRef \$aIntegers, \$iTarget)
Local \$aOp = ["+", "-"], \$i = 1, \$j, \$z = UBound(\$aIntegers), \$iCalc, \$k, \$l, \$sTerm
Do
For \$j = 0 To UBound(\$aIntegers) - 1
\$l = 2^(\$z - \$j)
\$k = (Mod(Mod(\$i - 1, \$l), \$i)) < (\$l / 2) ? 0 : 1
\$sTerm &= \$aOp[\$k] & \$aIntegers[\$j]
Next
\$iCalc = Execute(\$sTerm)
If \$iCalc = \$iTarget Then Return (StringLeft(\$sTerm, 1) = "+" ? StringTrimLeft(\$sTerm, 1) : \$sTerm)
\$sTerm = ""
\$i += 1
Until \$i > 2 ^ \$z
EndFunc```

I didn't test it properly and thus I don't know whether it will work also for other variations.

Edited by UEZ

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

## Create an account

Register a new account

• ### Similar Content

• Hi dears
How are you?
I have a question, to you please.
How do I create an edit box for numbers only and does not accept letters? using autoit
greetings to all
I hope you help ME
• By UritOR
Hi all,
How do I allow an input control (GUICtrlCreateInput) to get only digits and dots?
\$ES_NUMBER - allows only digits, but I need to be able to type something like this only:   2.15.1598.1

• Hello. I need to perform a specific string replace, but not sure how to go about it.
The scenario is this: I have a large block of text. Within the text colons appear ":",  Sometimes the colons are used in a sentence appearing after a word. Other times they appear in between numbers like a ratio or a sport score (e.g. "6:8").
I want to replace the colons appearing between numeric values like 6:8 with the word "to", but not the ones appearing at the end of a sentence.
Is there a way that I can have StringReplace (or any other method) differentiate when to replace the colon based on it appearing between numbers?
I did try searching for a similar scenario.
Thank you for any help.
• By PCI
Hi everyone
I've read all yesterday forums , i think it's me , but i could not find a clue for this :
I need to generate large numbers and output into csv format or excel format
- Range between 3000000 and 9000000
- Need 5000000 numbers
- Numbers should not be in series ( Mixed and Random )
- I would prefere to have a check digits at the end

Thank you

• hi everyone!
I need some help with my rpn calculator.
I first translated it from vbscript to autoitscript and that's when i stumbled on problem one:
(46) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
\$Stack[ubound(\$Stack)] = \$Item
^ ERROR
After that (and i know this from testing it as a vbscript) it can't work with negative numbers:
"1 - - 2" to Reverse Polish notation = "1 - 2 -" gives me the build in error...

Can you guy's help me with my code pleas?
Func Print(\$what,\$er = 0) \$t = '' if \$err <> 0 then \$t = 'ERROR!' MsgBox(\$er,\$st,\$what) EndFunc Func RPN(\$Tokenarray) Local \$Stack = [] Local \$Result Local \$Operator_B Local \$Operator_A For \$Token = 0 To Ubound(\$Tokenarray) If Isoperator(\$Tokenarray[\$Token]) Then \$Operator_B = Pop(\$Stack) \$Operator_A = Pop(\$Stack) If \$Operator_A = "" Then Print("The user has not input sufficient values in the expression.",16) exit EndIf Switch \$Tokenarray[\$Token] Case "+" \$Result = Number(\$Operator_A,3) + Number(\$Operator_B,3) Case "-" \$Result = Number(\$Operator_A,3) - Number(\$Operator_B,3) Case "*" \$Result = Number(\$Operator_A,3) * Number(\$Operator_B,3) Case "/" \$Result = Number(\$Operator_A,3) / Number(\$Operator_B,3) Case "^" \$Result = Number(\$Operator_A,3) ^ Number(\$Operator_B,3) EndSwitch Push(\$Result,\$Stack) Else Push(\$Tokenarray[\$Token],\$Stack) EndIf Next If Ubound(\$Stack) > 1 Then Print("The user input has too many values.",16) Exit EndIf Return pop(\$Stack) EndFunc Func Push(\$Item, Byref \$Stack) If Ubound(\$Stack) > -1 Then Redim \$Stack[Ubound(\$Stack) + 1] \$Stack[Ubound(\$Stack)] = \$Item Else Local \$Stack = [\$Item] EndIf EndFunc Func Pop(\$Stack) \$pop = "" If Ubound(\$Stack) > -1 Then \$Pop = \$Stack[Ubound(\$Stack)] Redim \$Stack[Ubound(\$Stack) - 1] EndIf Return \$pop EndFunc Func Peek(\$Stack) \$peek = "" If Ubound(\$Stack) > -1 Then \$Peek = \$Stack(Ubound(\$Stack)) EndIf Return \$peek EndFunc Func Isoperator(\$Operator) Return StringInstr("+-*/^&<=>", \$Operator) <> 0 And StringLen(\$Operator) = 1 EndFunc Func Precedence(\$Operator) If Isoperator(\$Operator) Then Switch \$Operator case "^" Return 4 case "*","/" Return 3 case "+","-" Return 2 case "&" Return 1 case "<","=",">" Return 0 EndSwitch EndIf EndFunc ;################################################################################################## ; (4 - -5) = (4|-|-|5) = (4|-|5|-) ; input tokens rpn Global \$calculate_this = ["4","-","5","-"] Print(rpn(\$calculate_this)) thanks in advance ,
TheAutomator
×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...