GCHQ Puzzle

Colin Legg

MrExcel MVP
Joined
Feb 28, 2008
Messages
3,507
Office Version
  1. 365
Platform
  1. Windows
The BBC website occasionally publishes brain teasers by British GCHQ. See if you can use Excel VBA to solve this puzzle which was published yesterday:

Take the digits 1,2,3 up to 9 in numerical order and put either a plus sign or a minus sign or neither between the digits to make a sum that adds up to 100. For example, one way of achieving this is: 1 + 2 + 34 - 5 + 67 - 8 + 9 = 100, which uses six plusses and minuses. What is the fewest number of plusses and minuses you need to do this?


The solution to the puzzle is on the link.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Am no VBA expert but this solution sprang to mind
Digits 1 to 9 in row 1, odd numbered columns., A C E etc
Insert a + - or null in even numbered columns
and evaluate the entire row

Code:
Sub k1()
Dim arr(3) As String
Dim q As String

arr(1) = ""
arr(2) = "+"
arr(3) = "-"

For i = 1 To 3
For j = 1 To 3
For k = 1 To 3
For l = 1 To 3
For m = 1 To 3
For n = 1 To 3
For o = 1 To 3
For p = 1 To 3

Cells(1, 2) = arr(i)
Cells(1, 4) = arr(j)
Cells(1, 6) = arr(k)
Cells(1, 8) = arr(l)
Cells(1, 10) = arr(m)
Cells(1, 12) = arr(n)
Cells(1, 14) = arr(o)
Cells(1, 16) = arr(p)

q = ""
For r = 1 To 17
q = q & Cells(1, r)
Next r
s = Evaluate(q)
If s = 100 Then Debug.Print q

Next p
Next o
Next n
Next m
Next l
Next k
Next j
Next i
MsgBox "DONE"
End Sub

Code:
123+45-67+8-9
123+4-5+67-89
123-45-67+89
123-4-5-6-7+8-9
12+3+4+5-6-7+89
12+3-4+5+67+8+9
12-3-4+5-6+7+89
1+23-4+56+7+8+9
1+23-4+5+6+78-9
1+2+34-5+67-8+9
1+2+3-4+5+6+78+9
 
Last edited:

Forum statistics

Threads
1,225,055
Messages
6,182,594
Members
453,126
Latest member
NigelExcel

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