Parentheses can be inserted into the expression
1/2/3/4/5/6/7/8/9/10/11
to produce a large collection of numbers, some of which are integers. What is the quotient of the largest of these integers divided by the smallest?
The program finds successively lower and higher values by using only division. There's lots of unneeded code as it is based on that for four fours, without removing the dross. A key change, besides looking for largest and smallest, is using only division or placing on the stack (For typ = 4 To 5) for every next number in turn.
The findings in RPN show that the first found integer is in fact the lowest, but the highest was replaced 4 times.
77 1,2/,3/,4/,5/,6,7/,8/,9/,A/,B//
2772 1,2/,3/,4/,5,6/,7/,8/,9/,A/,B//
69300 1,2/,3/,4,5/,6/,7/,8/,9/,A/,B//
1108800 1,2/,3,4/,5/,6/,7/,8/,9/,A/,B//
77 1,2,3//,4,5/,6/,7/,8,9///,A,B//
77 1,2,3//,4,5/,6/,7/,8,9/,A//,B//
9979200 1,2,3/,4/,5/,6/,7/,8/,9/,A/,B//
There are other ways of doing 77 and the only way some are showing up again is due to rounding errors that make it look smaller than before.
Translated from RPN and hexadecimal:
77 = 1/2/3/4/5/(6/7/8/9/10/11)
9979200 = 1/(2/3/4/5/6/7/8/9/10/11)
The largest, 9979200, divided by the smallest, 77, is 129600.
Explanation:
In the expression for 77, ultimately 2, 3, 4, 5 and 6 are in the denominator while 7, 8, 9, 10 and 11 are in the numerator, as the latter set are in the denominator of the denominator. Keeping track of the prime factors, only a single 11 and a single 7 remain after cancellation.
The largest is easier to explain, just half the product of all the numbers other than 2. It's 1/4 of 11! as 2 does not appear in the product that is the numerator, and 2 does appear in the denominator.
DefDbl A-Z
Dim crlf$, stack(15), concatable(13), fact(15), dfact(-1 To 25), solCt(90), shortest(90) As String
Dim stackPtr, wh, pz As String, expstr As String, lvl, subfact As Variant
Dim maxval, minval
Private Sub Form_Load()
Form1.Visible = True
Text1.Text = ""
crlf = Chr$(13) + Chr$(10)
pz0$ = "123456789AB"
minval = 999999
f = 1: fact(0) = f
For i = 1 To 15
f = f * i
fact(i) = f
Text1.Text = Text1.Text & f & crlf
Next
dfact(-1) = 1: dfact(0) = 1
For i = 1 To 25
f = 1
If i Mod 2 = 1 Then
For j = 1 To i Step 2: f = f * j: Next
Else
For j = 2 To i Step 2: f = f * j: Next
End If
dfact(i) = f
Text1.Text = Text1.Text & i & Str(f) & crlf
Next
subfact = Array(1, 0, 1, 2, 9, 44, 265, 1854, 14833, 133496)
pz = pz0
Text1.Text = Text1.Text & pz & crlf
h$ = pz
' Do
stackPtr = 1
stack(1) = Val(Left(pz, 1)): concatable(1) = 1
expstr = LTrim(Str(stack(1)))
wh = 2
addOn
permute pz
' Loop Until pz = h
For i = 1 To 90
Text1.Text = Text1.Text & i & " " & shortest(i) & crlf
Next
Text1.Text = Text1.Text & crlf & " done"
End Sub
Sub addOn()
ReDim sstack(13)
ReDim sconcatable(13)
DoEvents
For typ = 4 To 5
sstackPtr = stackPtr
sexpstr$ = expstr
For i = 1 To stackPtr
sstack(i) = stack(i)
sconcatable(i) = concatable(i)
swh = wh
Next
good = 1
Select Case typ
Case 0 ' concatenate
If concatable(stackPtr) And wh < 13 Then
stack(stackPtr) = 10 * stack(stackPtr) + Val(Mid(pz, wh, 1))
expstr = expstr + Mid(pz, wh, 1)
wh = wh + 1
Else
good = 0
End If
Case 1 ' add (+)
If stackPtr > 1 Then
stackPtr = stackPtr - 1
stack(stackPtr) = stack(stackPtr) + stack(stackPtr + 1)
expstr = expstr + "+"
concatable(stackPtr) = 0
Else
good = 0
End If
Case 2 ' subtract (-)
If stackPtr > 1 Then
stackPtr = stackPtr - 1
stack(stackPtr) = stack(stackPtr) - stack(stackPtr + 1)
expstr = expstr + "-"
concatable(stackPtr) = 0
Else
good = 0
End If
Case 3 ' multiply (*)
If stackPtr > 1 Then
stackPtr = stackPtr - 1
stack(stackPtr) = stack(stackPtr) * stack(stackPtr + 1)
expstr = expstr + "*"
concatable(stackPtr) = 0
Else
good = 0
End If
Case 4 ' divide (/)
If stackPtr > 1 And stack(stackPtr) <> 0 Then
stackPtr = stackPtr - 1
stack(stackPtr) = stack(stackPtr) / stack(stackPtr + 1)
expstr = expstr + "/"
concatable(stackPtr) = 0
Else
good = 0
End If
Case 5 ' comma
If wh < 13 Then
stackPtr = stackPtr + 1
stack(stackPtr) = InStr("123456789AB", (Mid(pz, wh, 1)))
concatable(stackPtr) = 1
expstr = expstr + "," + Mid(pz, wh, 1)
wh = wh + 1
Else
good = 0
End If
Case 6 ' power (^)
If stackPtr > 1 Then
If stack(stackPtr - 1) <> 0 And stack(stackPtr - 1) < 1000 And stack(stackPtr) >= 0 And stack(stackPtr) < 10 And (stack(stackPtr - 1) > 0 Or stack(stackPtr) = Int(stack(stackPtr))) Then
stackPtr = stackPtr - 1
stack(stackPtr) = stack(stackPtr) ^ stack(stackPtr + 1)
expstr = expstr + "^"
concatable(stackPtr) = 0
Else
good = 0
End If
Else
good = 0
End If
Case 7 ' factorial
If stack(stackPtr) = Int(stack(stackPtr)) And stack(stackPtr) <= 15 And stack(stackPtr) >= 0 And stack(stackPtr) <> 1 And stack(stackPtr) <> 2 Then
stack(stackPtr) = fact(stack(stackPtr))
expstr = expstr + "!"
concatable(stackPtr) = 0
Else
good = 0
End If
Case 8 ' radical
If stack(stackPtr) >= 0 And Right(expstr, 2) <> "vv" Then
stack(stackPtr) = Sqr(stack(stackPtr))
expstr = expstr + "v"
concatable(stackPtr) = 0
Else
good = 0
End If
Case 9 ' double factorial
If stack(stackPtr) = Int(stack(stackPtr)) And stack(stackPtr) <= 25 And stack(stackPtr) >= 4 Then
stack(stackPtr) = dfact(stack(stackPtr))
expstr = expstr + "(!!)"
concatable(stackPtr) = 0
Else
good = 0
End If
Case 10 ' 4/9
If wh < 4 Then
stackPtr = stackPtr + 1
stack(stackPtr) = 4 / 9
concatable(stackPtr) = 0
expstr = expstr + ",4,9/"
wh = wh + 1
Else
good = 0
End If
Case 11 ' subfactorial
If stack(stackPtr) = Int(stack(stackPtr)) And stack(stackPtr) <= 9 And stack(stackPtr) >= 4 Then
stack(stackPtr) = subfact(stack(stackPtr))
expstr = expstr + "s"
concatable(stackPtr) = 0
Else
good = 0
End If
End Select
If good Then
If stackPtr = 1 And wh = 13 Then
If (stack(stackPtr) > maxval Or stack(stackPtr) < minval) And Abs(stack(stackPtr) - Int(stack(stackPtr) + 0.5)) < 0.000000001 Then
' solCt(stack(stackPtr)) = solCt(stack(stackPtr)) + 1
' If Len(expstr$) < Len(shortest(stack(stackPtr))) Or Len(shortest(stack(stackPtr))) = 0 Then
Text1.Text = Text1.Text & mform(stack(stackPtr), "##0") & " " & expstr & crlf
If stack(stackPtr) > maxval Then maxval = stack(stackPtr)
If stack(stackPtr) < minval Then minval = stack(stackPtr)
' shortest(stack(stackPtr)) = expstr
' End If
End If
End If
If Len(expstr) < 34 Then
addOn
End If
End If
wh = swh
expstr = sexpstr
stackPtr = sstackPtr
For i = 1 To stackPtr
stack(i) = sstack(i)
concatable(i) = sconcatable(i)
Next
Next typ
End Sub
Function mform$(x, t$)
a$ = Format$(x, t$)
If Len(a$) < Len(t$) Then a$ = Space$(Len(t$) - Len(a$)) & a$
mform$ = a$
End Function
|
Posted by Charlie
on 2018-04-04 10:25:35 |