PA HS Teacher
Well-known Member
- Joined
- Jul 17, 2004
- Messages
- 2,838
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.
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.