Each of P, Q, R and S together uses the base ten digits from 1 to 9 exactly once.
What are the respective minimum and maximum value of P*Q*R*S?
DefDbl A-Z
Dim crlf$, s As String
Private Sub Form_Load()
ChDir "C:\Program Files (x86)\DevStudio\VB\projects\flooble"
Text1.Text = ""
crlf$ = Chr(13) + Chr(10)
Form1.Visible = True
DoEvents
s = "123456789": h$ = s
minval = 999999999
Do
a = Left(s, 1)
b = Mid(s, 2, 1): c = Mid(s, 3, 1): d = Mid(s, 4, 6)
GoSub evaluate
b = Mid(s, 2, 1): c = Mid(s, 3, 2): d = Mid(s, 5, 5)
GoSub evaluate
b = Mid(s, 2, 1): c = Mid(s, 3, 3): d = Mid(s, 6, 4)
GoSub evaluate
b = Mid(s, 2, 2): c = Mid(s, 4, 2): d = Mid(s, 6, 4)
GoSub evaluate
b = Mid(s, 2, 2): c = Mid(s, 4, 3): d = Mid(s, 7, 3)
GoSub evaluate
a = Left(s, 2): b = Mid(s, 3, 2): c = Mid(s, 5, 2): d = Mid(s, 7, 3)
GoSub evaluate
permute s
Loop Until s = h$
Text1.Text = Text1.Text & maxval & " =" & Str(maxa) & " *" & Str(maxb) & " *" & Str(maxc) & " *" & Str(maxd) & crlf
Text1.Text = Text1.Text & minval & " =" & Str(mina) & " *" & Str(minb) & " *" & Str(minc) & " *" & Str(mind) & crlf
Text1.Text = Text1.Text & crlf & " done"
Exit Sub
evaluate:
prod = a * b * c * d
If prod > maxval Then maxval = prod: maxa = a: maxb = b: maxc = c: maxd = d
If prod < minval Then minval = prod: mina = a: minb = b: minc = c: mind = d
Return
End Sub
finds
367856664 = 74 * 83 * 92 * 651
2740734 = 1 * 2 * 3 * 456789
|
Posted by Charlie
on 2014-08-19 18:18:00 |