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.
 
Pretty Slick!
I especially like the part where you check to make sure you're not using one of the 6 digits more than once by stringing 1's -6's in your p loop. I played around with your code a little bit and I too got it to where it seems to solve all problems in under 3 minutes.

To speed things up I:
- Turned off the screen updating
- Wrote all results to the same cell
- exited the looping when the magic number was reached

Many solutions seem to take a couple of seconds. Even if the entire sequence were completed it would take my computer just over 2 minutes to finish. Each cycle of the i loop takes my computer about 21 seconds.

I'm still relatively new at this game, but I was wondering. Any time VB writes to a sheet or requires a spreadsheet function, the code runs relatively slow, especially with many loops. (at least that has been my limited experience) Two things come to mind:

1) Is there a way to perform the function of your p loop without calling a
worksheet function. (in this case substitute)

2) Is there a way to calculate the resulting number for a given
combination of the 6 numbers and 4 operators without writing to the
spreadsheet at all. (and without requiring the spreadsheet to
calculate at all.) A way to do it using strictly VB ?
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
"...If interested, let me know via PM."

Hi yee388,

It detracts from the very purpose of the board to only disclose solutions to problems in private!
 
Upvote 0
That was my question too. I have no idea how to code the operations without hard-coding all 1,024 possibilities...

i.e. if Operator1 = "+", how do you code: 1 Operator1 2 = 3?

I noticed that the screen updating cut the time by 1/5, and I did throw a test in to exit once a goal is reached.

I looked up the rules to the gameshow (since I've never seen it), and there is a pretty significant change to the code. The rules of the game allow for 6 numbers from two sets: {1:10} and {25,50,75, or 100}

Numbers in the small set are allowed twice, while there can only be one of the big numbers. So, I had to do away with the String method of testing repeats, and had to settle for writing each value to a cell and using COUNTIF. Still looking for a better solution to that.

I also have 4 loops now, one that tests for a possible 3,4,5, or 6 value statement, since in the show you don't HAVE to use all the values...

Very interesting project, though.
 
Upvote 0
This is my code ver.II:

Assumptions:
A2:A7 contain 6 values that conform to the CountDown rules
B2: =COUNTIF($A$2:$A$7,A2) and fill through B7
O5 holds a non-zero target number

The macro writes the rest of what you will see.

Yes, I know the placement is strange... I did this in a hurry.

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

<SPAN style="color:#00007F">Dim</SPAN> ValueArray(8)
<SPAN style="color:#00007F">Dim</SPAN> OperatorArray(3)
<SPAN style="color:#00007F">Dim</SPAN> r, c <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

Range("C9:M9", "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
    ValueArray(a) = Range("A" & a + 2)
<SPAN style="color:#00007F">Next</SPAN> a

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

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

StartTime = Timer

<SPAN style="color:#00007F">Do</SPAN>

<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">'Paste Values</SPAN>
        Cells(9, 3) = ValueArray(i)
        Cells(9, 5) = ValueArray(j)
        Cells(9, 7) = ValueArray(k)
        Cells(9, 4) = OperatorArray(v)
        Cells(9, 6) = OperatorArray(w)
        
        <SPAN style="color:#007F00">'Make sure all values are unique</SPAN>
        <SPAN style="color:#00007F">For</SPAN> b = 3 <SPAN style="color:#00007F">To</SPAN> 7 <SPAN style="color:#00007F">Step</SPAN> 2
            NumUses = WorksheetFunction.CountIf(Range("C9:M9"), Cells(9, b))
            MaxUses = WorksheetFunction.Index(Range("A2:B7"), WorksheetFunction.Match(Cells(9, b), Range("A2:A7"), 0), 2)
            <SPAN style="color:#00007F">If</SPAN> NumUses > MaxUses <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> SkipValue3
        <SPAN style="color:#00007F">Next</SPAN> b
        
        <SPAN style="color:#007F00">'Paste formula</SPAN>
        Cells(9, 15) = "=" & ValueArray(i) & OperatorArray(v) & ValueArray(j) & OperatorArray(w) & ValueArray(k)
        
        <SPAN style="color:#007F00">'Test for goal</SPAN>
        <SPAN style="color:#00007F">If</SPAN> Range("O9").Value = Range("O5").Value <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> ByeBye

    <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">'Paste Values</SPAN>
        <SPAN style="color:#007F00">'This is probably pretty expensive, but Countif is the only way I can</SPAN>
        <SPAN style="color:#007F00">'think of to make sure values aren't overused...</SPAN>
        Cells(9, 3) = ValueArray(i)
        Cells(9, 5) = ValueArray(j)
        Cells(9, 7) = ValueArray(k)
        Cells(9, 9) = ValueArray(l)
        Cells(9, 4) = OperatorArray(v)
        Cells(9, 6) = OperatorArray(w)
        Cells(9, 8) = OperatorArray(x)
        
        <SPAN style="color:#007F00">'Make sure all values are unique</SPAN>
        <SPAN style="color:#00007F">For</SPAN> b = 3 <SPAN style="color:#00007F">To</SPAN> 7 <SPAN style="color:#00007F">Step</SPAN> 2
            NumUses = WorksheetFunction.CountIf(Range("C9:M9"), Cells(9, b))
            MaxUses = WorksheetFunction.Index(Range("A2:B7"), WorksheetFunction.Match(Cells(9, b), Range("A2:A7"), 0), 2)
            <SPAN style="color:#00007F">If</SPAN> NumUses > MaxUses <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> SkipValue4
        <SPAN style="color:#00007F">Next</SPAN> b
        
        <SPAN style="color:#007F00">'Paste formula</SPAN>
        Cells(9, 15) = "=" & ValueArray(i) & OperatorArray(v) & ValueArray(j) & OperatorArray(w) & ValueArray(k) & OperatorArray(x) & ValueArray(l)
        
        <SPAN style="color:#007F00">'Test for goal</SPAN>
        <SPAN style="color:#00007F">If</SPAN> Range("O9").Value = Range("O5").Value <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> ByeBye
    
    <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">'Paste Values</SPAN>
        Cells(9, 3) = ValueArray(i)
        Cells(9, 5) = ValueArray(j)
        Cells(9, 7) = ValueArray(k)
        Cells(9, 9) = ValueArray(l)
        Cells(9, 11) = ValueArray(m)
        Cells(9, 4) = OperatorArray(v)
        Cells(9, 6) = OperatorArray(w)
        Cells(9, 8) = OperatorArray(x)
        Cells(9, 10) = OperatorArray(y)
        
        <SPAN style="color:#007F00">'Make sure all values are unique</SPAN>
        <SPAN style="color:#00007F">For</SPAN> b = 3 <SPAN style="color:#00007F">To</SPAN> 11 <SPAN style="color:#00007F">Step</SPAN> 2
            NumUses = WorksheetFunction.CountIf(Range("C9:M9"), Cells(9, b))
            MaxUses = WorksheetFunction.Index(Range("A2:B7"), WorksheetFunction.Match(Cells(9, b), Range("A2:A7"), 0), 2)
            <SPAN style="color:#00007F">If</SPAN> NumUses > MaxUses <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> SkipValue5
        <SPAN style="color:#00007F">Next</SPAN> b
        
        <SPAN style="color:#007F00">'Paste formula</SPAN>
        Cells(9, 15) = "=" & ValueArray(i) & OperatorArray(v) & ValueArray(j) & OperatorArray(w) & ValueArray(k) _
             & OperatorArray(x) & ValueArray(l) & OperatorArray(y) & ValueArray(m)
        
        <SPAN style="color:#007F00">'Test for goal</SPAN>
        <SPAN style="color:#00007F">If</SPAN> Range("O9").Value = Range("O5").Value <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> ByeBye
        
    <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">'Paste Values</SPAN>
        Cells(9, 3) = ValueArray(i)
        Cells(9, 5) = ValueArray(j)
        Cells(9, 7) = ValueArray(k)
        Cells(9, 9) = ValueArray(l)
        Cells(9, 11) = ValueArray(m)
        Cells(9, 13) = ValueArray(n)
        Cells(9, 4) = OperatorArray(v)
        Cells(9, 6) = OperatorArray(w)
        Cells(9, 8) = OperatorArray(x)
        Cells(9, 10) = OperatorArray(y)
        Cells(9, 12) = OperatorArray(z)
        
        <SPAN style="color:#007F00">'Make sure all values are unique</SPAN>
        <SPAN style="color:#00007F">For</SPAN> b = 3 <SPAN style="color:#00007F">To</SPAN> 13 <SPAN style="color:#00007F">Step</SPAN> 2
            NumUses = WorksheetFunction.CountIf(Range("C9:M9"), Cells(9, b))
            MaxUses = WorksheetFunction.Index(Range("A2:B7"), WorksheetFunction.Match(Cells(9, b), Range("A2:A7"), 0), 2)
            <SPAN style="color:#00007F">If</SPAN> NumUses > MaxUses <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> SkipValue6
        <SPAN style="color:#00007F">Next</SPAN> b
        
        <SPAN style="color:#007F00">'Paste formula</SPAN>
        Cells(9, 15) = "=" & ValueArray(i) & OperatorArray(v) & ValueArray(j) & OperatorArray(w) & ValueArray(k) _
             & OperatorArray(x) & ValueArray(l) & OperatorArray(y) & ValueArray(m) & OperatorArray(z) & ValueArray(n)
        
        <SPAN style="color:#007F00">'Test for goal</SPAN>
        <SPAN style="color:#00007F">If</SPAN> Range("O9").Value = Range("O5").Value <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> ByeBye
        
    <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

<SPAN style="color:#00007F">Loop</SPAN>

MsgBox "Sorry, that goal is not possible."

ByeBye:

EndTime = Timer

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
If one were to view this like a lottery, where you pick 6 possibilities. Let's think about how many possibilities there would be to select from...

1,2,3,4,5,6,7,8,9,10 ...but really twice that...
1,2,3,4,5,6,7,8,9,10
and
25,50,75,100 [so far 24 possibilities]

Each of which will have one of the four operators in front of it.
(I guess technically the first number chosen will always have the + operator)

As I see it, loosely speaking, that's 4 * 24 or 96 possibilities. (more than in a typical state lottery)

I am ashamed to say that I do not remember the rules for calculating the probability of picking 6 (non replaced) numbers from a pool of n numbers. I think it would be something like n factorial/(n-r) factorial, but its been awhile and I don't remember. Does someone remember the rules for this type of probability?

In any case, it looks like odds of cracking one of these problems in under 3 minutes is pretty slim. A solution to this problem may require less brute force and more creative coding. At least for this evening, I will have to drop back and punt.
 
Upvote 0
Well, you actually don't have to pick from all 24 possibilities. In the show you say, "I want 4 small numbers and 2 large." (Someone who has seen the show, please correct me if I'm wrong...)

So they give you (at random), 4 numbers from the small set (it would be possible to draw something like {2,2,3,3}, and then 2 numbers from the large set {75, 100}.

So your pool is always 6 values, not 24.
 
Upvote 0
yee388 - Excellent work..The only improvement I can think of is to keep track of the closest number to the target, in case it is not possible to find a combination.

So instead of saying "Sorry, that goal is not possible.", but the closest possible number to the target is....this might be quite difficult though
 
Upvote 0
I wasn't aware that "close" counted as anything... I'll give it a try. I don't think it'll be too big of a problem.
 
Upvote 0
You asked about calculating a value without using a cell...

Why not use Evaluate? I know that Evaluate("5+3*2") returns 11, so I would think that you could utilize that.
 
Upvote 0
That works wonderfully. Between ScreenUpdating = False and Evaluate(), this thing is really flying now. I'm getting most solutions in under 10 seconds now.

Sean, still working on that "closest" bit.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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