123 lines
3.1 KiB
Text
123 lines
3.1 KiB
Text
dim d(4)
|
|
input "Enter 4 digits: "; a$
|
|
nD=0
|
|
for i =1 to len(a$)
|
|
c$=mid$(a$,i,1)
|
|
if instr("123456789",c$) then
|
|
nD=nD+1
|
|
d(nD)=val(c$)
|
|
end if
|
|
next
|
|
'for i = 1 to 4
|
|
' print d(i);
|
|
'next
|
|
|
|
'precompute permutations. Dumb way.
|
|
nPerm = 1*2*3*4
|
|
dim perm(nPerm, 4)
|
|
n = 0
|
|
for i = 1 to 4
|
|
for j = 1 to 4
|
|
for k = 1 to 4
|
|
for l = 1 to 4
|
|
'valid permutation (no dupes?)
|
|
if i<>j and i<>k and i<>l _
|
|
and j<>k and j<>l _
|
|
and k<>l then
|
|
n=n+1
|
|
'
|
|
' perm(n,1)=i
|
|
' perm(n,2)=j
|
|
' perm(n,3)=k
|
|
' perm(n,4)=l
|
|
'actually, we can as well permute given digits
|
|
perm(n,1)=d(i)
|
|
perm(n,2)=d(j)
|
|
perm(n,3)=d(k)
|
|
perm(n,4)=d(l)
|
|
end if
|
|
next
|
|
next
|
|
next
|
|
next
|
|
'check if permutations look OK. They are
|
|
'for i =1 to n
|
|
' print i,
|
|
' for j =1 to 4: print perm(i,j);:next
|
|
' print
|
|
'next
|
|
|
|
'possible brackets
|
|
NBrackets = 11
|
|
dim Brakets$(NBrackets)
|
|
DATA "4#4#4#4"
|
|
DATA "(4#4)#4#4"
|
|
DATA "4#(4#4)#4"
|
|
DATA "4#4#(4#4)"
|
|
DATA "(4#4)#(4#4)"
|
|
DATA "(4#4#4)#4"
|
|
DATA "4#(4#4#4)"
|
|
DATA "((4#4)#4)#4"
|
|
DATA "(4#(4#4))#4"
|
|
DATA "4#((4#4)#4)"
|
|
DATA "4#(4#(4#4))"
|
|
for i = 1 to NBrackets
|
|
read Tmpl$: Brakets$(i) = Tmpl$
|
|
next
|
|
|
|
'operations: full search
|
|
count = 0
|
|
Ops$="+ - * /"
|
|
dim Op$(3)
|
|
For op1=1 to 4
|
|
Op$(1)=word$(Ops$,op1)
|
|
For op2=1 to 4
|
|
Op$(2)=word$(Ops$,op2)
|
|
For op3=1 to 4
|
|
Op$(3)=word$(Ops$,op3)
|
|
'print "*"
|
|
'substitute all brackets
|
|
for t = 1 to NBrackets
|
|
Tmpl$=Brakets$(t)
|
|
'print , Tmpl$
|
|
'now, substitute all digits: permutations.
|
|
for p = 1 to nPerm
|
|
res$= ""
|
|
nOp=0
|
|
nD=0
|
|
for i = 1 to len(Tmpl$)
|
|
c$ = mid$(Tmpl$, i, 1)
|
|
select case c$
|
|
case "#" 'operations
|
|
nOp = nOp+1
|
|
res$ = res$+Op$(nOp)
|
|
case "4" 'digits
|
|
nD = nOp+1
|
|
res$ = res$; perm(p,nD)
|
|
case else 'brackets goes here
|
|
res$ = res$+ c$
|
|
end select
|
|
next
|
|
'print,, res$
|
|
'eval here
|
|
if evalWithErrCheck(res$) = 24 then
|
|
print "24 = ";res$
|
|
end 'comment it out if you want to see all versions
|
|
end if
|
|
count = count + 1
|
|
next
|
|
next
|
|
Next
|
|
Next
|
|
next
|
|
|
|
print "If you see this, probably task cannot be solved with these digits"
|
|
'print count
|
|
end
|
|
|
|
function evalWithErrCheck(expr$)
|
|
on error goto [handler]
|
|
evalWithErrCheck=eval(expr$)
|
|
exit function
|
|
[handler]
|
|
end function
|