For Fun and Learning Project

  • Thread starter Thread starter Legacy 98055
  • Start date Start date
Interesting Problem. I decided to attack the problem using a series of User Defined Functions, that return Collections.

The User Defined Function Math9YearOld() can be used in a spreadsheet to solve the problem:
http://www.box.net/public/3psd0h6la0

I'll post an image of the spreadsheet when I get home.

In A2 and Pasted Down
=Math9Yearold(B2,C2,D2,E2,F2)

The Math9YearOldFunction Returns a String with all Solutions to the Problem:

**************** Function Math9YearOld ************************
<font face=Courier New><SPAN style="color:#00007F">Function</SPAN> Math9YearOld(<SPAN style="color:#00007F">Optional</SPAN> GHB <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN> = 40, _
                      <SPAN style="color:#00007F">Optional</SPAN> BAC <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN> = 30, _
                      <SPAN style="color:#00007F">Optional</SPAN> CDE <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN> = 48, _
                      <SPAN style="color:#00007F">Optional</SPAN> EFG <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN> = 0, _
                      <SPAN style="color:#00007F">Optional</SPAN> Center <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN> = 14) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
StartTime = timeGetTime()
<SPAN style="color:#00007F">Dim</SPAN> PotentialSolutions <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">New</SPAN> Collection <SPAN style="color:#007F00">' Collection of Potential Solutions</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> Answers <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">New</SPAN> Collection  <SPAN style="color:#007F00">' Collection of Correct Solutions</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> GHBs <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">New</SPAN> Collection
<SPAN style="color:#00007F">Dim</SPAN> BACs <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">New</SPAN> Collection
<SPAN style="color:#00007F">Dim</SPAN> CDEs <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">New</SPAN> Collection           <SPAN style="color:#007F00">' A Colection for each triplet of the combinations that</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> EFGs <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">New</SPAN> Collection           <SPAN style="color:#007F00">' multiply to the corresponding constant above</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> GHBACs <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">New</SPAN> Collection
<SPAN style="color:#00007F">Dim</SPAN> GHBACDEs <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">New</SPAN> Collection
<SPAN style="color:#00007F">Dim</SPAN> GHBACDEFGs <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">New</SPAN> Collection

<SPAN style="color:#00007F">Set</SPAN> GHBs = Triplets(Factors(GHB), GHB)  <SPAN style="color:#007F00">'Collection of all possible Triplets Multiplying to GHB</SPAN>
<SPAN style="color:#00007F">Set</SPAN> BACs = Triplets(Factors(BAC), BAC)  <SPAN style="color:#007F00">'Collection of all possible Triplets Multiplying to BAC</SPAN>
<SPAN style="color:#00007F">Set</SPAN> CDEs = Triplets(Factors(CDE), CDE)  <SPAN style="color:#007F00">'Collection of all possible Triplets Multiplying to CDE</SPAN>
<SPAN style="color:#00007F">Set</SPAN> EFGs = Triplets(Factors(EFG), EFG)  <SPAN style="color:#007F00">'Collection of all possible Triplets Multiplying to EFG</SPAN>

<SPAN style="color:#007F00">' Possible Solutions are stored as a String of Numbers.  The first character of each String Correspods _
  to G, the second to H etc.  The collection of Possible Solutions is assembled by going through all _
  Possible combinations of one Family of Triplets with the Previous Families of Triplets.  Any _
  Solutions that repeat a digit are not included.</SPAN>
  
<SPAN style="color:#00007F">Set</SPAN> GHBACs = UniquePossibilities(GHBs, BACs)
<SPAN style="color:#00007F">Set</SPAN> GHBACDEs = UniquePossibilities(GHBACs, CDEs)
<SPAN style="color:#00007F">Set</SPAN> GHBACDEFGs = UniquePossibilities(GHBACDEs, EFGs, SkipLastDigit:=True)

<SPAN style="color:#007F00">' Collect the Possible Solutions that also Satisfy B+C+E+G = Center into a SolutionSet Collection</SPAN>
<SPAN style="color:#00007F">Set</SPAN> PotentialSolutions = CenterMet(GHBACDEFGs, 14)
<SPAN style="color:#007F00">' Check Solutions to make sure they meet all Criteria</SPAN>
Set Answers = VerifiedSolutions(PotentialSolutions, GHB, BAC, CDE, EFG, Center)

<SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> cc <SPAN style="color:#00007F">In</SPAN> Answers
  S = S & "G = " & Mid(cc, 1, 1) & " H = " & Mid(cc, 2, 1) & " B = " & Mid(cc, 3, 1)
  S = S & " A = " & Mid(cc, 4, 1) & " C = " & Mid(cc, 5, 1)
  S = S & " D = " & Mid(cc, 6, 1) & " E = " & Mid(cc, 7, 1)
  S = S & " F = " & Mid(cc, 8, 1) & Chr(10)
<SPAN style="color:#00007F">Next</SPAN> cc
EndTime = timeGetTime()
Debug.Print S
Debug.Print "Elapsed Time: " & (Format(EndTime - StartTime)) & " ms?"
Math9YearOld = S
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>
</FONT>

The Logic is similar to what Dennis suggested, (and I"m sure what Andrew Impletented, though I only skimmed his code). Instead of using Loops and Arrays, I used Functions that return Collections.

1. For each of the 4 Multiplication requirement, determine the possible Factors (0 to 9) of the Required Number.

**************** Function Factors ******************************
New><SPAN style="color:#00007F">Function</SPAN> Factors(N <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>
<SPAN style="color:#007F00">' Returns all Factors (0 to 9) of N</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> C <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">New</SPAN> Collection
C.Add 0
<SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> 9
   <SPAN style="color:#00007F">If</SPAN> N Mod i = 0 <SPAN style="color:#00007F">Then</SPAN> C.Add i
<SPAN style="color:#00007F">Next</SPAN> i
<SPAN style="color:#00007F">Set</SPAN> Factors = C
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN></FONT>

2. For each of the 4 Triplets, G*H*B, B*A*C, C*D*E, E*F*G, Assemble a collection of all possible 3 Factor Multiplications that yield the desired Number for Triplet.

**************** Function Triplets ************************<font face=Courier [/size]New><SPAN style="color:#00007F">Function</SPAN> Triplets(Factors <SPAN style="color:#00007F">As</SPAN> Collection, N <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>
<SPAN style="color:#007F00">' Returns a Collection of all possible 3 digit combinations of Factors that multiply to N</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> C <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">New</SPAN> Collection
<SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> Factors.Count
<SPAN style="color:#00007F">For</SPAN> j = 1 <SPAN style="color:#00007F">To</SPAN> Factors.Count
<SPAN style="color:#00007F">For</SPAN> k = 1 <SPAN style="color:#00007F">To</SPAN> Factors.Count
   <SPAN style="color:#00007F">If</SPAN> Factors(i) <> Factors(j) And Factors(i) <> Factors(k) And Factors(j) <> Factors(k) <SPAN style="color:#00007F">Then</SPAN>
   <SPAN style="color:#00007F">If</SPAN> Factors(i) * Factors(j) * Factors(k) = N <SPAN style="color:#00007F">Then</SPAN>
      C.Add Factors(i) & Factors(j) & Factors(k)
   <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
   <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<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">Set</SPAN> Triplets = C
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN></FONT>


3. Assemble a Collection of all Possible 4 Triplet Combinations by making 3 calls to the follwing Function.
A. (C1) Because the Triplets Overal (e.g. GHB and BAC share B, All combinations where the Last Digit of the Previous Triplet does not Match the Left Digit of the Current Triplet are excluded.
B. (C2) All Members of this Collection that repeat digits are excluded.
C. (C3) Any Duplicate Members are Excluded.



**************** Function UniquePossibilities ******************

<SPAN [/size]style="color:#00007F">Function</SPAN> UniquePossibilities(A <SPAN style="color:#00007F">As</SPAN> Collection, B <SPAN style="color:#00007F">As</SPAN> Collection, <SPAN style="color:#00007F">Optional</SPAN> SkipLastDigit <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN> = <SPAN style="color:#00007F">False</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> C1 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">New</SPAN> Collection <SPAN style="color:#007F00">' Collection A & B's where the Last A = Last B  (In the game they overlap)</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> C2 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">New</SPAN> Collection <SPAN style="color:#007F00">' Subset of C1 with No digits repeating</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> C3 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">New</SPAN> Collection <SPAN style="color:#007F00">' Same as C2 but no Duplicates</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> NotUnique <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>
ALen = Len(A(1))
ANum2 = A.Count
BLen = Len(B(1))
BNum2 = B.Count
LastDigit = ALen + BLen - 1
<SPAN style="color:#00007F">If</SPAN> SkipLastDigit <SPAN style="color:#00007F">Then</SPAN> LastDigit = LastDigit - 1

                   <SPAN style="color:#007F00">' C1 Collection A & B's where the Last A = Last B  (In the game they overlap)</SPAN>
<SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> ANum2
<SPAN style="color:#00007F">For</SPAN> j = 1 <SPAN style="color:#00007F">To</SPAN> BNum2
<SPAN style="color:#00007F">If</SPAN> Right(A(i), 1) = Left(B(j), 1) <SPAN style="color:#00007F">Then</SPAN> C1.Add A(i) & Right(B(j), Len(B(j)) - 1)
<SPAN style="color:#00007F">Next</SPAN> j
<SPAN style="color:#00007F">Next</SPAN> i

                   <SPAN style="color:#007F00">' C2 Subset of C1 with No digits repeating in any Solution</SPAN>
<SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> cc <SPAN style="color:#00007F">In</SPAN> C1
NotUnique = <SPAN style="color:#00007F">False</SPAN>
  <SPAN style="color:#00007F">For</SPAN> i = ALen + 1 <SPAN style="color:#00007F">To</SPAN> LastDigit
    <SPAN style="color:#00007F">If</SPAN> InStr(1, Left(cc, ALen - 1), Mid(cc, i, 1)) > 0 <SPAN style="color:#00007F">Then</SPAN> NotUnique = <SPAN style="color:#00007F">True</SPAN>
  <SPAN style="color:#00007F">Next</SPAN> i
<SPAN style="color:#00007F">If</SPAN> NotUnique = <SPAN style="color:#00007F">False</SPAN> <SPAN style="color:#00007F">Then</SPAN>
   S = Left(cc, ALen) & Right(cc, BLen - 1)
   <SPAN style="color:#00007F">If</SPAN> SkipLastDigit <SPAN style="color:#00007F">Then</SPAN> S = Left(S, Len(S) - 1)
   C2.Add S
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">Next</SPAN> cc

                   <SPAN style="color:#007F00">'C3  Same as C2 but no Duplicates</SPAN>
<SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> cc <SPAN style="color:#00007F">In</SPAN> C2
  AlreadyExists = <SPAN style="color:#00007F">False</SPAN>
  <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> cc3 <SPAN style="color:#00007F">In</SPAN> C3
     <SPAN style="color:#00007F">If</SPAN> cc3 = cc <SPAN style="color:#00007F">Then</SPAN> AlreadyExists = <SPAN style="color:#00007F">True</SPAN>
  <SPAN style="color:#00007F">Next</SPAN> cc3
  <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> AlreadyExists <SPAN style="color:#00007F">Then</SPAN> C3.Add cc
<SPAN style="color:#00007F">Next</SPAN> cc
<SPAN style="color:#00007F">Set</SPAN> UniquePossibilities = C3
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN></FONT>

4. Potential Solutions that do not meet the requirement B+C+E+G=Center are excluded

**************** Function CenterMet ************************
<font face=Courier New><SPAN style="color:#00007F">Function</SPAN> CenterMet(A <SPAN style="color:#00007F">As</SPAN> Collection, N <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>
<SPAN style="color:#007F00">' Returns a Collection of Solutions that Meet the requirement that B+C+E+G = N</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> C <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">New</SPAN> Collection
<SPAN style="color:#00007F">Dim</SPAN> S <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
<SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> cc <SPAN style="color:#00007F">In</SPAN> A
  S = 0
  S = S + Val(Mid(cc, 3, 1))
  S = S + Val(Mid(cc, 5, 1))
  S = S + Val(Mid(cc, 7, 1))
  S = S + Val(Mid(cc, 1, 1))
  <SPAN style="color:#00007F">If</SPAN> S = N <SPAN style="color:#00007F">Then</SPAN> C.Add cc
<SPAN style="color:#00007F">Next</SPAN> cc
<SPAN style="color:#00007F">Set</SPAN> CenterMet = C
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN></FONT>

5. All Potential Solutions are Checked for Correctness

**************** Function VerifiedSolutions *********************
<font face=Courier New><SPAN style="color:#00007F">Function</SPAN> VerifiedSolutions(A <SPAN style="color:#00007F">As</SPAN> Collection, GHB <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, BAC <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, CDE <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, EFG <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, Center <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>) <SPAN style="color:#00007F">As</SPAN> Collection
<SPAN style="color:#007F00">' Checks Potential Solutions in Collection A, returning a Collection of Correct Solutions</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> C <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">New</SPAN> Collection
<SPAN style="color:#00007F">Dim</SPAN> Sum <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> ConditionsMet <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>
<SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> cc <SPAN style="color:#00007F">In</SPAN> A
Sum = 0
ConditionsMet = <SPAN style="color:#00007F">True</SPAN>
   ConditionsMet = ConditionsMet And (Val(Mid(cc, 1, 1)) * Val(Mid(cc, 2, 1)) * Val(Mid(cc, 3, 1))) = GHB
   ConditionsMet = ConditionsMet And (Val(Mid(cc, 3, 1)) * Val(Mid(cc, 4, 1)) * Val(Mid(cc, 5, 1))) = BAC
   ConditionsMet = ConditionsMet And (Val(Mid(cc, 5, 1)) * Val(Mid(cc, 6, 1)) * Val(Mid(cc, 7, 1))) = CDE
   ConditionsMet = ConditionsMet And (Val(Mid(cc, 7, 1)) * Val(Mid(cc, 8, 1)) * Val(Mid(cc, 1, 1))) = EFG
   Sum = Val(Mid(cc, 3, 1)) + Val(Mid(cc, 5, 1)) + Val(Mid(cc, 7, 1)) + Val(Mid(cc, 1, 1))
   ConditionsMet = ConditionsMet And Sum = Center
   <SPAN style="color:#00007F">If</SPAN> ConditionsMet <SPAN style="color:#00007F">Then</SPAN> C.Add cc
<SPAN style="color:#00007F">Next</SPAN> cc
<SPAN style="color:#00007F">Set</SPAN> VerifiedSolutions = C
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN></FONT>




6. Finally the Lead Function Math9YearOld() returns a String containing all possible Solutions.

Using the timing function posted by Sydney, A correct answer is yielded in about 11 ms on my (slow) machine. Tweaking numbers on the example spreadsheet, I was able to create 2 similar problems, one with 2 solutions.

I hope hope the hours I put in last night add something to this discussion.


PS - Tom, thank you immensely for your Object help this summer. I'm teaching 5 classes this year, and I had to put the project down for a while, but I will come back to it eventually. (probably not till the summer, otherwise it will become too consuming.) I have been extending your object oriented philosophy in some of my smaller projects.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi

It's nice to see an alternative solution to this puzzle. I haven't gone through your code in detail yet (I will give it a whirl this weekend) but I noticed you refer to the solution in the plural - does your code produce more than one solution?

Andrew
 
Hi Andrew,
I assume that for some values of the input parameters, there could be more than one valid solution. For the Parameters in this problem,
=Math9Yearold(40,30,48,0,14) yields the single solution:

G = 1 H = 8 B = 5 A = 3 C = 2 D = 4 E = 6 F = 0

however,
=Math9Yearold(40,6,48,0,14) yields two solutions:

G = 5 H = 4 B = 2 A = 3 C = 1 D = 8 E = 6 F = 0
G = 5 H = 8 B = 1 A = 3 C = 2 D = 4 E = 6 F = 0

I suppose it is possible that some comnations may yield more.

Most combinations of input parameters GHB, BAC, CDE, EFG, Center dont' seem to yield any solutions, and for certain combinations, the function seems like it may be caught in a loop. I haven't tried that many combinations for the parameters, but I supose one could use this function in some loops to generate a list of all possible parameters, for this puzzle.

Assuming Only Numbers from 0 to 9 are used, and no digits are used more than once.
 
Interesting - I hadn't considered the possibility of multiple solutions - I was focused on just finding the one solution. What combination(s) does your solution fail on?

Andrew
 
Here is what the sheet looks like. I've added some rows with parameters to illustrate the function.

Notice, some sets of Parameters yield more than one solution.
Row 3 has 2 solutions
Row 10 has 36 solutions! (though my approach allows for multiple 0 Targets)

I believe the #Value! is resulting from the inability to form a Triplet (3 Unique factors that multiply to one of the parameters.)

I have not been able to recreate the "endless loop" that I saw earlier. Maybe the program had simply frozen when observed the behavior earlier?
 
The minimum value for one of the triplets is 6 (assuming the product is >0) given the three smallest values are 1, 2, 3 - the product of which is 6 so there are no possible solutions for rows 5 and 6. Also row 9 contains a prime number as one of the triplet values so it only has one single digit factor and again the puzzle cannot be solved. So it looks like your code is working fine.

Andrew
 
Extension of Problem: All possible "Games"?

I showed my wife the original puzzle, and she liked playing it, sort of like she plays Soduko. I thought it would be fun to generate a list of "puzzles". Same rules apply but with different constants.

Still no digits used more than once.
Still only digits 0 to 9.
Still GHB = a Constant
Still BAC = a Constant
Still CDE = a Constant
Still EFG = a Constant
Still B +C + E + G = a Constant

I am trying to assemble a list of 5 Constant Combinations that are solvable puzzles.

The largest possible Constant that a triplet can multiply to is 9*8*7 or 504.
The smallest (nonzero) is 1*2*3 or 6 as Andrew has pointed out.

The largest possible Constant that B+C+E+G can be is 9+8+7+6 or 30.
The smallest possible B + C + E + G = 0 + 1 + 2 + 3 = 6

None of the Target Constants for triplet multiplication can be a Prime Number as Andrew has also pointed out.

To start out, a brute force method would be to loop through all possible combinations using the logic above. I've started this approach, but it would take days to weeks to run on my computer.

A Sheet called Not Possible Triplets, can help cut down some unncecessary looping, but not considering certain Target Constants for Triplet Multiplications:
Math 9 Year Old Function All Possible 12-16-06.xls
ABCD
1TRUE
2TRUE
3TRUE
4TRUE
5TRUE
6FALSE
7TRUE
8FALSE
9FALSE
10FALSE
11TRUE
12FALSE
13TRUE
Not a Possible Triplet


A list of Valid Puzzles can be written to this Sheet.
Math 9 Year Old Function All Possible 12-16-06.xls
ABCDEF
1SolutionsGHBBACCDEEFGB+C+E+G
2
3
Possible Puzzles


I have a button on the "Puzzles" Sheet with the following Code:<font face=Courier New><SPAN style="color:#00007F">Private</SPAN><SPAN style="color:#00007F">Sub</SPAN> CommandButton1_Click()<SPAN style="color:#00007F">Dim</SPAN> GHB<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Integer</SPAN>, BAC<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Integer</SPAN>, CDE<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Integer</SPAN>, EFG<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Integer</SPAN>, BCEG<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Integer</SPAN><SPAN style="color:#00007F">Dim</SPAN> A<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">String</SPAN>, iRow<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Integer</SPAN><SPAN style="color:#00007F">Dim</SPAN> NotAPossibleTriplet(0<SPAN style="color:#00007F">To</SPAN> 504)<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Boolean</SPAN>
NotAPossibleTriplet(0) =<SPAN style="color:#00007F">False</SPAN><SPAN style="color:#00007F">For</SPAN> i = 1<SPAN style="color:#00007F">To</SPAN> 504
  NotAPossibleTriplet(i) = Worksheets("Not a Possible Triplet").Cells(i, 1).Value<SPAN style="color:#00007F">Next</SPAN> i
iRow = Range("B65535").End(xlUp).Row<SPAN style="color:#00007F">For</SPAN> GHB = 0<SPAN style="color:#00007F">To</SPAN> 504
                    <SPAN style="color:#00007F">If</SPAN> NotAPossibleTriplet(GHB)<SPAN style="color:#00007F">Then</SPAN><SPAN style="color:#00007F">GoTo</SPAN> ErrGHB<SPAN style="color:#00007F">For</SPAN> BAC = 0<SPAN style="color:#00007F">To</SPAN> 504
                    <SPAN style="color:#00007F">If</SPAN> NotAPossibleTriplet(BAC)<SPAN style="color:#00007F">Then</SPAN><SPAN style="color:#00007F">GoTo</SPAN> ErrBAC<SPAN style="color:#00007F">For</SPAN> CDE = 0<SPAN style="color:#00007F">To</SPAN> 504
                    <SPAN style="color:#00007F">If</SPAN> NotAPossibleTriplet(CDE)<SPAN style="color:#00007F">Then</SPAN><SPAN style="color:#00007F">GoTo</SPAN> ErrCDE<SPAN style="color:#00007F">For</SPAN> EFG = 0<SPAN style="color:#00007F">To</SPAN> 504
                    <SPAN style="color:#00007F">If</SPAN> NotAPossibleTriplet(EFG)<SPAN style="color:#00007F">Then</SPAN><SPAN style="color:#00007F">GoTo</SPAN> ErrEFG<SPAN style="color:#00007F">For</SPAN> BCEG = 6<SPAN style="color:#00007F">To</SPAN> 30
  <SPAN style="color:#00007F">On</SPAN><SPAN style="color:#00007F">Error</SPAN><SPAN style="color:#00007F">GoTo</SPAN> Err
  A = "Error"
  A = Math9YearOld(GHB, BAC, CDE, EFG, BCEG)
  <SPAN style="color:#00007F">If</SPAN> A = "Error"<SPAN style="color:#00007F">Then</SPAN><SPAN style="color:#00007F">GoTo</SPAN> Err
  iRow = iRow + 1
  Cells(iRow, 1) = A
  Cells(iRow, 2) = GHB
  Cells(iRow, 3) = BAC
  Cells(iRow, 4) = CDE
  Cells(iRow, 5) = EFG
  Cells(iRow, 6) = BCEG
  Cells(iRow, 7) = Now()
  Debug.Print GHB & " " & BAC & " " & CDE & " " & EFG & " " & BCEG & "    " & A & "  " & Format(Now(), "mm/dd/yy h:mm:ss")

Err:<SPAN style="color:#00007F">Next</SPAN> BCEG
ErrEFG<SPAN style="color:#00007F">Next</SPAN> EFG
ErrCDE<SPAN style="color:#00007F">Next</SPAN> CDE
ErrBAC:
Debug.Print GHB & " " & BAC & " " & CDE & " " & EFG & " " & BCEG & "    " & A & "  " & Format(Now(), "mm/dd/yy h:mm:ss")<SPAN style="color:#00007F">Next</SPAN> BAC
ErrGHB:<SPAN style="color:#007F00">'ThisWorkbook.Save</SPAN><SPAN style="color:#00007F">Next</SPAN> GHB<SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">Sub</SPAN></FONT>


This approach is too brute force. I'd like to refine it, by reducing unnecessary looping. Perhaps one could prescreen the constants in the loops before calling the Math9YearOld function. If we know somehow ahead of time that the constant will not yield a solution, don't call the function.

I'm also considering the possiblity that instead of looping through constants sequentially, one could loop through a collection of possible contstants. The collection of possible constants might be dependent on the current value of the more outer constant loops. For example if every possible triplet for the outermost constant (GHB) requires a 1 in every possible triplet, then we can eliminate constants for the inner loop that require a 1 in every possible triplet for that constant.

This would allow us to reduce the number of loops.


btw: I modified Math9YearOld so that it returns "Error" if the function errors out rather than #Value!. This allows these loops to keep running when the Math9YearOld function errors out.

Here's the file:
http://www.box.net/public/lj7srjiyau

thought it would take at least days to run completely as is.
 
Hi

It appears your technique for generating puzzles is starting at the possible solutions and working backwards. When I first viewed this question I thought about producing a list of all possible puzzles based on using the values 0 through 9 in the 8 positions and working forwards. However, I believe there hundreds of thousands (if not millions) of unique permutations. I think you will find there are 6,720 unique solutions for 20,160 permutations where you have the number 0 in position A and the number 1 in position B.

So, rather than producing puzzles en masse, the following code will provide the eight final values and from that you can use math to calculate the 5 'clues', for one puzzle at a time.

Code:
Option Explicit

Public Sub MakeMeAPuzzle()

Dim tempValue(8) As Single, _
    ActualValue(8) As Integer, _
    ValuesToChoose(9) As Integer, _
    LoopCount As Integer, _
    InnerLoop As Integer, _
    PickDigit As Integer, _
    DigitCounter As Integer

'Resets the random seed
Randomize

For LoopCount = 1 To 8
    'I could have merged this with the line below but kept it seperate for debugging
    tempValue(LoopCount) = Rnd()
    'Multiply the random number by the number of choices left
    'Used one extra value plus the -0.5 to give the 2 end values the same chance of
    'being selected as the other values
    tempValue(LoopCount) = CInt(((11 - LoopCount) * tempValue(LoopCount)) - 0.5)
    'This number represents the digit to choose, based on the digits not yet used
Next

'Clear the used flag for the 10 possible choices
For LoopCount = 0 To 9
    ValuesToChoose(LoopCount) = 0
Next

For LoopCount = 1 To 8
    DigitCounter = 0
    PickDigit = tempValue(LoopCount)
    For InnerLoop = 0 To 9
        If ValuesToChoose(InnerLoop) = 0 Then
            'Not used yet
            If PickDigit = DigitCounter Then
                'Use this digit
                ValuesToChoose(InnerLoop) = 1
                ActualValue(LoopCount) = InnerLoop
                'Exit the inner loop
                InnerLoop = 9
            Else
                'Keep searching
                DigitCounter = DigitCounter + 1
            End If
        End If
    Next
Next

'Display the values
For LoopCount = 1 To 8
    Cells(2, LoopCount) = ActualValue(LoopCount)
Next

'The 4 triplet values are ActualValue(1*2*3) , ActualValue(3*4*5) , _
  ActualValue(5*6*7) , ActualValue(7*8*1)
'The inner value is ActualValue(1*3*5*7) - more correctly ActualValue(1) * ActualValue(3) etc.

End Sub

Andrew
 

Forum statistics

Threads
1,222,723
Messages
6,167,852
Members
452,149
Latest member
gatje

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