Code
Heliotrope wrote:engineer wrote:I did a brute force computer program. Takes a while, but the programming was straight forward. Ayrlass beat me to it though.
I'd be very interested to see that code.
Would you mind sharing it ?
I don't mind if you don't laugh. Since all I had at hand was Excel, I wrote it as a VB Excel macro and used spreadsheet cells as variables. Cells A1-A9 were the nine values. I set A1 to 1 since I knew the first digit in the first denominator was 1. I set A2 to 9 since I knew it was a big number and had it decrease instead of increasing. The rest I set to two before starting the macro. I set cell B1 to be the sum of the numbers in A1:A9, B2 to be the product and B3 to be the formula we wanted. That said, here is the code. You could easily replace the cells with an array in a non-Excel environment. Remember no laughing. I can write much better code, honest.
Public Sub SolvePuzzle()
Dim i As Integer
' Cells A2 through A9 hold the numbers 2 through 9
' The tens digit in the denominator of the first number must be 1
Do While Range("a2") > 1
' Count downward for the numerator of the first variable since I know it has to be big
Do While Range("a3") < 10
Do While Range("a4") < 10
Do While Range("a5") < 10
Do While Range("a6") < 10
Do While Range("a7") < 10
Do While Range("a8") < 10
Do While Range("a9") < 10
' If all the numbers from 1 to 9 are in place, the sum is 45 and the product is 362880
If Range("b1") = 45 And Range("b2") = 362880 And Range("b3") < 0.00001 Then
Stop
End If
Range("a9") = Range("a9") + 1
Loop
Range("a8") = Range("a8") + 1
Range("a9") = 2
Loop
Range("a7") = Range("a7") + 1
Range("a8") = 2
Loop
Range("a6") = Range("a6") + 1
Range("a7") = 2
Loop
Range("a5") = Range("a5") + 1
Range("a6") = 2
Loop
Range("a4") = Range("a4") + 1
Range("a5") = 2
Loop
Range("a3") = Range("a3") + 1
Range("a4") = 2
Loop
' Remember I am decrementing the value in A2
Range("a2") = Range("a2") - 1
Range("a3") = 2
Loop
End Sub