Number Problem

Sean Stevens

Board Regular
Joined
Jul 24, 2003
Messages
123
Has anyone come across a solution to this problem.

I have 6 numbers ranging from 1 to 100 in cells A1:A6.

I have a target number in cell C1. Using any combination of the 6 numbers and any combination of the arithemtic signs +,-,* or /, can you reach the target number??

I have been able to do this with just using the + sign, but it gets difficult after that...

Any ideas??? Thanks, Sean.
 
yee388 - How does the program exit out of the

Do

Loop

if it can't find a combination to get to the target number?? I am using the numbers 75,25,4,8,1,7 and target number of 405. The program has been running for over 30 mins and from stopping and debugging the code, it looks like it is continually looping...

Removed the Do...Loop and it took about 16 minutes to find no combination.

Sean.
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Actually looking at those numbers it should have found a solution as

(75-25)*8+4+1 = 405

I guess Excel is evalutaing the calculation as 75-(25*8)+4+1 = -120.
 
Upvote 0
Good call. It's not needed. It was there for a previous version that used it, and I forgot to take it out.

[edit] Just noticed your last post... Yes. I think it would REALLY be difficult to test for all the possible locatations for parentheses....

1+2*3-4
(1+2)*3-4
(1+2*3)-4
(1+2)*(3-4)
1+(2*3)-4

It would take hours...
 
Upvote 0
You know, I hate these things... I never get any "real" work done :)

yee, you don't mind if I play, too, do you?

Instead of using Match and CountIf to determine duplicates, I tried using a collection. I keep a collection of every tried formula, and use the formula itself as the item and also as the key. So, every time I try to add to it, and can evaluate Err.Number to determine if it was already used, and that becomes be check for duplicates. This eliminates the need to write anything to the spreadsheet at all.

Here is what I have:

Code:
Sub CountDown()

Dim TriedFormulas As New Collection

Dim ValueArray(8)
Dim OperatorArray(3)
Dim r, c As Long

Application.ScreenUpdating = False

'Get Values from A2:A7
For a = 0 To 5
    ValueArray(a) = Range("A" & a + 2)
Next a

'Initialize operators
OperatorArray(0) = "+"
OperatorArray(1) = "-"
OperatorArray(2) = "/"
OperatorArray(3) = "*"

'Initialize TriedFormulas
For Each temp In TriedFormulas
    TriedFormulas.Remove temp.Index
Next

StartTime = Timer

Do

'Run possibilities for 3 value strings

For i = 0 To 5
For j = 0 To 5
For k = 0 To 5

    For v = 0 To 3
    For w = 0 To 3
    
        txtFormula = ValueArray(i) & OperatorArray(v) & _
                     ValueArray(j) & OperatorArray(w) & ValueArray(k)
        'Make sure all values are unique
        If AlreadyTried(txtFormula, TriedFormulas) Then GoTo SkipValue3
        If Evaluate(txtFormula) = Range("O5").Value Then GoTo ByeBye
    Next w
    Next v
    
SkipValue3:

Next k
Next j
Next i

'Run possibilities for 4 value strings

For i = 0 To 5
For j = 0 To 5
For k = 0 To 5
For l = 0 To 5

    For v = 0 To 3
    For w = 0 To 3
    For x = 0 To 3
    
        txtFormula = ValueArray(i) & OperatorArray(v) & _
                     ValueArray(j) & OperatorArray(w) & _
                     ValueArray(k) & OperatorArray(x) & ValueArray(l)
        
        If AlreadyTried(txtFormula, TriedFormulas) Then GoTo SkipValue4
                     
        If Evaluate(txtFormula) = Range("O5").Value Then GoTo ByeBye
    
    Next x
    Next w
    Next v
    
SkipValue4:

Next l
Next k
Next j
Next i

'Run possibilities for 5 value strings

For i = 0 To 5
For j = 0 To 5
For k = 0 To 5
For l = 0 To 5
For m = 0 To 5

    For v = 0 To 3
    For w = 0 To 3
    For x = 0 To 3
    For y = 0 To 3
    
        txtFormula = ValueArray(i) & OperatorArray(v) & _
                     ValueArray(j) & OperatorArray(w) & _
                     ValueArray(k) & OperatorArray(x) & _
                     ValueArray(l) & OperatorArray(y) & ValueArray(m)
        
        'Make sure all values are unique
        If AlreadyTried(txtFormula, TriedFormulas) Then GoTo SkipValue5
            
        If Evaluate(txtFormula) = Range("O5").Value Then GoTo ByeBye
        
    Next y
    Next x
    Next w
    Next v
    
SkipValue5:

Next m
Next l
Next k
Next j
Next i

'Run possibilities for 6 value strings
For i = 0 To 5
For j = 0 To 5
For k = 0 To 5
For l = 0 To 5
For m = 0 To 5
For n = 0 To 5

    For v = 0 To 3
    For w = 0 To 3
    For x = 0 To 3
    For y = 0 To 3
    For z = 0 To 3
            
        txtFormula = ValueArray(i) & OperatorArray(v) & _
                     ValueArray(j) & OperatorArray(w) & _
                     ValueArray(k) & OperatorArray(x) & _
                     ValueArray(l) & OperatorArray(y) & _
                     ValueArray(m) & OperatorArray(z) & ValueArray(n)
                     
        If AlreadyTried(txtFormula, TriedFormulas) Then GoTo SkipValue6

        If Evaluate(txtFormula) = Range("O5").Value Then GoTo ByeBye
        
    Next z
    Next y
    Next x
    Next w
    Next v
    
SkipValue6:

Next n
Next m
Next l
Next k
Next j
Next i

Loop

MsgBox "Sorry, that goal is not possible."
Exit Sub

ByeBye:

EndTime = Timer

Application.ScreenUpdating = True

MsgBox "Process took " & EndTime - StartTime & " seconds"
MsgBox txtFormula
End Sub

Function AlreadyTried(txtData, colTried As Collection) As Boolean
On Error Resume Next
    colTried.Add txtData, txtData
    AlreadyTried = (Err.Number = 457)
On Error GoTo 0
End Function

Still trying to work out how to incorprate parentheses. But that's after lunch :)

Mike.
 
Upvote 0
I think the collection idea is great, but I'm not sure we're checking for the same thing.

Say the list of values is 3,3,5,8,50,100

The first formula it will try is 3+3+3, because ValueArray(0) is 3. I want to skip that formula b/c 3 happens 3 times; when it should only be allowed twice.

Maybe I'm misunderstanding your solution...
 
Upvote 0
Ah, I misunderstood what you were checking for...

Using your example numbers, in the for loops that are:
Code:
For i = 0 To 5 
For j = 0 To 5 
For k = 0 To 5

i=1, j=2,k=3 (or 3+3+5)is exactly the same as i=2, j=1, k=3 (3+3+5). That is what I am checking for. Duplicate combinations of the value arguments.

To cover your check, maybe just verify that all of the subscripts are different. in other words i<>j<>k<>l<>m etc.

Probably a separate function to call.

Hmmm...
Mike.
 
Upvote 0
VerIII:

Can usually find a solution in under 10 secs... if no exact solution can be found, process is under 1 min.

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> CountDown()

<SPAN style="color:#00007F">Dim</SPAN> TestArray()
<SPAN style="color:#00007F">Dim</SPAN> MaxArray(0 <SPAN style="color:#00007F">To</SPAN> 5, 0 <SPAN style="color:#00007F">To</SPAN> 5)
<SPAN style="color:#00007F">Dim</SPAN> ValArray(5)
<SPAN style="color:#00007F">Dim</SPAN> OpArray(3)
<SPAN style="color:#00007F">Dim</SPAN> r, c <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

Range("C9:O9").ClearContents

Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN>

<SPAN style="color:#007F00">'Get Values from A2:A7</SPAN>
<SPAN style="color:#00007F">For</SPAN> a = 0 <SPAN style="color:#00007F">To</SPAN> 5
    ValArray(a) = Range("A" & a + 2)
<SPAN style="color:#00007F">Next</SPAN> a

<SPAN style="color:#007F00">'Initialize operators</SPAN>
OpArray(0) = "+"
OpArray(1) = "-"
OpArray(2) = "/"
OpArray(3) = "*"

<SPAN style="color:#007F00">'Initialize row and column values</SPAN>
r = 1
c = 3

StartTime = Timer

<SPAN style="color:#007F00">'Run possibilities for 3 value strings</SPAN>

<SPAN style="color:#00007F">For</SPAN> i = 0 <SPAN style="color:#00007F">To</SPAN> 5
<SPAN style="color:#00007F">For</SPAN> j = 0 <SPAN style="color:#00007F">To</SPAN> 5
<SPAN style="color:#00007F">For</SPAN> k = 0 <SPAN style="color:#00007F">To</SPAN> 5

    <SPAN style="color:#00007F">For</SPAN> v = 0 <SPAN style="color:#00007F">To</SPAN> 3
    <SPAN style="color:#00007F">For</SPAN> w = 0 <SPAN style="color:#00007F">To</SPAN> 3
        
        <SPAN style="color:#007F00">'Make sure all values are unique</SPAN>
        <SPAN style="color:#00007F">If</SPAN> i = j <SPAN style="color:#00007F">Or</SPAN> i = k <SPAN style="color:#00007F">Or</SPAN> j = k <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> SkipValue3
        
        <SPAN style="color:#007F00">'Test for goal & "closest"</SPAN>
        <SPAN style="color:#00007F">If</SPAN> Evaluate(ValArray(i) & OpArray(v) & ValArray(j) & OpArray(w) & ValArray(k)) = Range("Goal").Value <SPAN style="color:#00007F">Then</SPAN>
        
            <SPAN style="color:#007F00">'Paste Values</SPAN>
            Cells(9, 3) = ValArray(i)
            Cells(9, 5) = ValArray(j)
            Cells(9, 7) = ValArray(k)
            Cells(9, 4) = OpArray(v)
            Cells(9, 6) = OpArray(w)
            
            <SPAN style="color:#007F00">'Paste formula</SPAN>
            Cells(9, 15) = "=" & ValArray(i) & OpArray(v) & ValArray(j) & OpArray(w) & ValArray(k)
            <SPAN style="color:#00007F">GoTo</SPAN> ByeBye
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        
    <SPAN style="color:#00007F">Next</SPAN> w
    <SPAN style="color:#00007F">Next</SPAN> v
    
SkipValue3:

<SPAN style="color:#00007F">Next</SPAN> k
<SPAN style="color:#00007F">Next</SPAN> j
<SPAN style="color:#00007F">Next</SPAN> i

<SPAN style="color:#007F00">'Run possibilities for 4 value strings</SPAN>

<SPAN style="color:#00007F">For</SPAN> i = 0 <SPAN style="color:#00007F">To</SPAN> 5
<SPAN style="color:#00007F">For</SPAN> j = 0 <SPAN style="color:#00007F">To</SPAN> 5
<SPAN style="color:#00007F">For</SPAN> k = 0 <SPAN style="color:#00007F">To</SPAN> 5
<SPAN style="color:#00007F">For</SPAN> l = 0 <SPAN style="color:#00007F">To</SPAN> 5

    <SPAN style="color:#00007F">For</SPAN> v = 0 <SPAN style="color:#00007F">To</SPAN> 3
    <SPAN style="color:#00007F">For</SPAN> w = 0 <SPAN style="color:#00007F">To</SPAN> 3
    <SPAN style="color:#00007F">For</SPAN> x = 0 <SPAN style="color:#00007F">To</SPAN> 3
        
        <SPAN style="color:#007F00">'Make sure values are unique</SPAN>
        <SPAN style="color:#00007F">If</SPAN> i = j <SPAN style="color:#00007F">Or</SPAN> i = k <SPAN style="color:#00007F">Or</SPAN> i = l <SPAN style="color:#00007F">Or</SPAN> j = k <SPAN style="color:#00007F">Or</SPAN> j = l <SPAN style="color:#00007F">Or</SPAN> k = l <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> SkipValue4
        
        <SPAN style="color:#007F00">'Test for goal</SPAN>
        <SPAN style="color:#00007F">If</SPAN> Evaluate(ValArray(i) & OpArray(v) & ValArray(j) & OpArray(w) & ValArray(k) _
        & OpArray(x) & ValArray(l)) = Range("Goal").Value <SPAN style="color:#00007F">Then</SPAN>
        
            <SPAN style="color:#007F00">'Paste Values</SPAN>
            Cells(9, 3) = ValArray(i)
            Cells(9, 5) = ValArray(j)
            Cells(9, 7) = ValArray(k)
            Cells(9, 9) = ValArray(l)
            Cells(9, 4) = OpArray(v)
            Cells(9, 6) = OpArray(w)
            Cells(9, 8) = OpArray(x)
            
            <SPAN style="color:#007F00">'Paste formula</SPAN>
            Cells(9, 15) = "=" & ValArray(i) & OpArray(v) & ValArray(j) & OpArray(w) & ValArray(k) & OpArray(x) & ValArray(l)
            <SPAN style="color:#00007F">GoTo</SPAN> ByeBye
        
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        
    <SPAN style="color:#00007F">Next</SPAN> x
    <SPAN style="color:#00007F">Next</SPAN> w
    <SPAN style="color:#00007F">Next</SPAN> v
    
SkipValue4:

<SPAN style="color:#00007F">Next</SPAN> l
<SPAN style="color:#00007F">Next</SPAN> k
<SPAN style="color:#00007F">Next</SPAN> j
<SPAN style="color:#00007F">Next</SPAN> i

<SPAN style="color:#007F00">'Run possibilities for 5 value strings</SPAN>

<SPAN style="color:#00007F">For</SPAN> i = 0 <SPAN style="color:#00007F">To</SPAN> 5
<SPAN style="color:#00007F">For</SPAN> j = 0 <SPAN style="color:#00007F">To</SPAN> 5
<SPAN style="color:#00007F">For</SPAN> k = 0 <SPAN style="color:#00007F">To</SPAN> 5
<SPAN style="color:#00007F">For</SPAN> l = 0 <SPAN style="color:#00007F">To</SPAN> 5
<SPAN style="color:#00007F">For</SPAN> m = 0 <SPAN style="color:#00007F">To</SPAN> 5

    <SPAN style="color:#00007F">For</SPAN> v = 0 <SPAN style="color:#00007F">To</SPAN> 3
    <SPAN style="color:#00007F">For</SPAN> w = 0 <SPAN style="color:#00007F">To</SPAN> 3
    <SPAN style="color:#00007F">For</SPAN> x = 0 <SPAN style="color:#00007F">To</SPAN> 3
    <SPAN style="color:#00007F">For</SPAN> y = 0 <SPAN style="color:#00007F">To</SPAN> 3
    
        <SPAN style="color:#007F00">'Make sure all values are unique</SPAN>
        <SPAN style="color:#00007F">If</SPAN> i = j <SPAN style="color:#00007F">Or</SPAN> i = k <SPAN style="color:#00007F">Or</SPAN> i = l <SPAN style="color:#00007F">Or</SPAN> i = m <SPAN style="color:#00007F">Or</SPAN> j = k <SPAN style="color:#00007F">Or</SPAN> j = l _
            <SPAN style="color:#00007F">Or</SPAN> j = m <SPAN style="color:#00007F">Or</SPAN> k = l <SPAN style="color:#00007F">Or</SPAN> k = m <SPAN style="color:#00007F">Or</SPAN> l = m <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> SkipValue5
        
        <SPAN style="color:#007F00">'Test for goal</SPAN>
        <SPAN style="color:#00007F">If</SPAN> Evaluate(ValArray(i) & OpArray(v) & ValArray(j) & OpArray(w) & ValArray(k) _
             & OpArray(x) & ValArray(l) & OpArray(y) & ValArray(m)) = Range("Goal").Value <SPAN style="color:#00007F">Then</SPAN>
        
            <SPAN style="color:#007F00">'Paste Values</SPAN>
            Cells(9, 3) = ValArray(i)
            Cells(9, 5) = ValArray(j)
            Cells(9, 7) = ValArray(k)
            Cells(9, 9) = ValArray(l)
            Cells(9, 11) = ValArray(m)
            Cells(9, 4) = OpArray(v)
            Cells(9, 6) = OpArray(w)
            Cells(9, 8) = OpArray(x)
            Cells(9, 10) = OpArray(y)
            
            <SPAN style="color:#007F00">'Paste formula</SPAN>
            Cells(9, 15) = "=" & ValArray(i) & OpArray(v) & ValArray(j) & OpArray(w) & ValArray(k) _
                 & OpArray(x) & ValArray(l) & OpArray(y) & ValArray(m)
            <SPAN style="color:#00007F">GoTo</SPAN> ByeBye
        
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        
    <SPAN style="color:#00007F">Next</SPAN> y
    <SPAN style="color:#00007F">Next</SPAN> x
    <SPAN style="color:#00007F">Next</SPAN> w
    <SPAN style="color:#00007F">Next</SPAN> v
    
SkipValue5:

<SPAN style="color:#00007F">Next</SPAN> m
<SPAN style="color:#00007F">Next</SPAN> l
<SPAN style="color:#00007F">Next</SPAN> k
<SPAN style="color:#00007F">Next</SPAN> j
<SPAN style="color:#00007F">Next</SPAN> i

<SPAN style="color:#007F00">'Run possibilities for 6 value strings</SPAN>
<SPAN style="color:#00007F">For</SPAN> i = 0 <SPAN style="color:#00007F">To</SPAN> 5
<SPAN style="color:#00007F">For</SPAN> j = 0 <SPAN style="color:#00007F">To</SPAN> 5
<SPAN style="color:#00007F">For</SPAN> k = 0 <SPAN style="color:#00007F">To</SPAN> 5
<SPAN style="color:#00007F">For</SPAN> l = 0 <SPAN style="color:#00007F">To</SPAN> 5
<SPAN style="color:#00007F">For</SPAN> m = 0 <SPAN style="color:#00007F">To</SPAN> 5
<SPAN style="color:#00007F">For</SPAN> n = 0 <SPAN style="color:#00007F">To</SPAN> 5

    <SPAN style="color:#00007F">For</SPAN> v = 0 <SPAN style="color:#00007F">To</SPAN> 3
    <SPAN style="color:#00007F">For</SPAN> w = 0 <SPAN style="color:#00007F">To</SPAN> 3
    <SPAN style="color:#00007F">For</SPAN> x = 0 <SPAN style="color:#00007F">To</SPAN> 3
    <SPAN style="color:#00007F">For</SPAN> y = 0 <SPAN style="color:#00007F">To</SPAN> 3
    <SPAN style="color:#00007F">For</SPAN> z = 0 <SPAN style="color:#00007F">To</SPAN> 3
        
        <SPAN style="color:#007F00">'Make sure all values are unique</SPAN>
        <SPAN style="color:#00007F">If</SPAN> i = j <SPAN style="color:#00007F">Or</SPAN> i = k <SPAN style="color:#00007F">Or</SPAN> i = l <SPAN style="color:#00007F">Or</SPAN> i = m <SPAN style="color:#00007F">Or</SPAN> i = n <SPAN style="color:#00007F">Or</SPAN> j = k <SPAN style="color:#00007F">Or</SPAN> j = l <SPAN style="color:#00007F">Or</SPAN> j = m _
             <SPAN style="color:#00007F">Or</SPAN> j = n <SPAN style="color:#00007F">Or</SPAN> k = l <SPAN style="color:#00007F">Or</SPAN> k = m <SPAN style="color:#00007F">Or</SPAN> k = n <SPAN style="color:#00007F">Or</SPAN> l = m <SPAN style="color:#00007F">Or</SPAN> l = n <SPAN style="color:#00007F">Or</SPAN> m = n <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> SkipValue6
            
        <SPAN style="color:#007F00">'Test for goal</SPAN>
        <SPAN style="color:#00007F">If</SPAN> Evaluate(ValArray(i) & OpArray(v) & ValArray(j) & OpArray(w) & ValArray(k) _
             & OpArray(x) & ValArray(l) & OpArray(y) & ValArray(m) & OpArray(z) & ValArray(n)) = Range("Goal").Value <SPAN style="color:#00007F">Then</SPAN>
    
            <SPAN style="color:#007F00">'Paste Values</SPAN>
            Cells(9, 3) = ValArray(i)
            Cells(9, 5) = ValArray(j)
            Cells(9, 7) = ValArray(k)
            Cells(9, 9) = ValArray(l)
            Cells(9, 11) = ValArray(m)
            Cells(9, 13) = ValArray(n)
            Cells(9, 4) = OpArray(v)
            Cells(9, 6) = OpArray(w)
            Cells(9, 8) = OpArray(x)
            Cells(9, 10) = OpArray(y)
            Cells(9, 12) = OpArray(z)
            
            <SPAN style="color:#007F00">'Paste formula</SPAN>
            Cells(9, 15) = "=" & ValArray(i) & OpArray(v) & ValArray(j) & OpArray(w) & ValArray(k) _
                 & OpArray(x) & ValArray(l) & OpArray(y) & ValArray(m) & OpArray(z) & ValArray(n)
            <SPAN style="color:#00007F">GoTo</SPAN> ByeBye
        
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        
    <SPAN style="color:#00007F">Next</SPAN> z
    <SPAN style="color:#00007F">Next</SPAN> y
    <SPAN style="color:#00007F">Next</SPAN> x
    <SPAN style="color:#00007F">Next</SPAN> w
    <SPAN style="color:#00007F">Next</SPAN> v
    
SkipValue6:

<SPAN style="color:#00007F">Next</SPAN> n
<SPAN style="color:#00007F">Next</SPAN> m
<SPAN style="color:#00007F">Next</SPAN> l
<SPAN style="color:#00007F">Next</SPAN> k
<SPAN style="color:#00007F">Next</SPAN> j
<SPAN style="color:#00007F">Next</SPAN> i

MsgBox "Sorry, that goal is not possible."

ByeBye:

EndTime = Timer

[N9] = "="

Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN>

MsgBox "Process took " & EndTime - StartTime & " seconds"

<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
A2:A7 have the values. Read through the earlier posts to get the whole story on what we're trying to do...
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top