Permutations/Derangements: Difference between revisions

Content added Content deleted
m (→‎Using a module: Add libheader)
Line 2,393: Line 2,393:


=={{header|PureBasic}}==
=={{header|PureBasic}}==
Brute Force
{{incomplete|PureBasic|Missing the subfactorial !n.}}
Brute Force: Tested up to n=10
<lang PureBasic>
<lang PureBasic>

Procedure.q perm(n)
Procedure.q perm(n)
if n=0:ProcedureReturn 1:endif
If n=0:ProcedureReturn 1:EndIf
if n=1:ProcedureReturn 0:endif
If n=1:ProcedureReturn 0:EndIf
ProcedureReturn (perm(n-1)+perm(n-2))*(n-1)
ProcedureReturn (perm(n-1)+perm(n-2))*(n-1)
EndProcedure
EndProcedure

factFile.s="factorials.txt"
factFile.s="factorials.txt"
tempFile.s="temp.txt"
tempFile.s="temp.txt"
Line 2,409: Line 2,407:
DeleteFile(tempFile.s)
DeleteFile(tempFile.s)
DeleteFile(drngFile.s)
DeleteFile(drngFile.s)

n=4
n=4

; create our storage file
; create our storage file
f.s=factFile.s
f.s=factFile.s
If CreateFile(2113,f.s)
If CreateFile(0,f.s)
WriteStringN(2113,"1.2")
WriteStringN(0,"1.2")
WriteStringN(2113,"2.1")
WriteStringN(0,"2.1")
CloseFile(2113)
CloseFile(0)
Else
Else
Debug "not createfile :"+f.s
Debug "not createfile :"+f.s
EndIf
EndIf

showfactorial=#FALSE
showfactorial=#False

if showfactorial
If showfactorial
; cw("nfactorial n ="+str(n))
; cw("nfactorial n ="+str(n))
Debug "nfactorial n ="+str(n)
Debug "nfactorial n ="+Str(n)
EndIf
endif

; build up the factorial combinations
; build up the factorial combinations
for l=1 to n-2
For l=1 To n-2
gosub nfactorial
Gosub nfactorial
Next
next

; extract the derangements
; extract the derangements
; cw("derangements["+str(perm(n))+"] for n="+str(n))
; cw("derangements["+str(perm(n))+"] for n="+str(n))
Debug "derangements["+str(perm(n))+"] for n="+str(n)
Debug "derangements["+Str(perm(n))+"] for n="+Str(n)
gosub derangements
Gosub derangements
; cw("")
; cw("")
Debug ""
Debug ""

; show the first 20 derangements
; show the first 20 derangements
for i=0 to 20
For i=0 To 20
; cw("derangements["+str(perm(i))+"] for n="+str(i))
; cw("derangements["+str(perm(i))+"] for n="+str(i))
Debug "derangements["+str(perm(i))+"] for n="+str(i)
Debug "derangements["+Str(perm(i))+"] for n="+Str(i)
Next
next
end


End
derangements:
derangements:
x=0
x=0
If ReadFile(2112,factFile.s) and CreateFile(2113,drngFile.s)
If ReadFile(0,factFile.s) And CreateFile(1,drngFile.s)
Repeat
repeat
r.s = ReadString(2112)
r.s = ReadString(0)
cs=CountString(r.s,".")
cs=CountString(r.s,".")
if cs
If cs
hit=0
hit=0
t.s=""
t.s=""
; scan for numbers at their index
; scan for numbers at their index
for i=1 to cs+1
For i=1 To cs+1
s.s=StringField(r.s,i,".")
s.s=StringField(r.s,i,".")
t.s+s.s+"."
t.s+s.s+"."
if val(s.s)=i:hit+1:endif
If Val(s.s)=i:hit+1:EndIf
Next
next
t.s=rtrim(t.s,".")
t.s=RTrim(t.s,".")
; show only those which are valid
; show only those which are valid
if not hit
If Not hit
x+1
x+1
; cw(t.s+" "+str(x))
; cw(t.s+" "+str(x))
Debug t.s+" "+str(x)
Debug t.s+" "+Str(x)
WriteStringN(2113,t.s+" "+str(x))
WriteStringN(1,t.s+" "+Str(x))
EndIf
endif
EndIf
endif
Until Eof(0)
until eof(2112)
CloseFile(2112)
CloseFile(0)
CloseFile(2113)
CloseFile(1)
Else
Else
Debug "not readfile :"+factFile.s
Debug "not readfile :"+factFile.s
Line 2,481: Line 2,480:
; cw("")
; cw("")
Debug ""
Debug ""
Return
return

nfactorial:
nfactorial:
x=0
x=0
If ReadFile(2112,factFile.s) and CreateFile(2113,tempFile.s)
If ReadFile(0,factFile.s) And CreateFile(1,tempFile.s)
Repeat
repeat
r.s = ReadString(2112)
r.s = ReadString(0)
cs=CountString(r.s,".")
cs=CountString(r.s,".")
if cs
If cs
for j=1 to cs+2
For j=1 To cs+2
t.s=""
t.s=""
for i=1 to cs+1
For i=1 To cs+1
s.s=StringField(r.s,i,".")
s.s=StringField(r.s,i,".")
if i=j
If i=j
t.s+"."+str(cs+2)+"."+s.s
t.s+"."+Str(cs+2)+"."+s.s
Else
else
t.s+"."+s.s
t.s+"."+s.s
EndIf
endif
Next
next
if j=cs+2:t.s+"."+str(cs+2):endif
If j=cs+2:t.s+"."+Str(cs+2):EndIf
t.s=trim(t.s,".")
t.s=Trim(t.s,".")
x+1
x+1
if cs+2=n and showfactorial
If cs+2=n And showfactorial
; cw(t.s+" "+str(x))
; cw(t.s+" "+str(x))
Debug t.s+" "+str(x)
Debug t.s+" "+Str(x)
EndIf
endif
WriteStringN(2113,t.s)
WriteStringN(1,t.s)
Next
next
EndIf
endif
Until Eof(0)
until eof(2112)
CloseFile(2112)
CloseFile(0)
CloseFile(2113)
CloseFile(1)
Else
Else
Debug "not readfile :"+factFile.s
Debug "not readfile :"+factFile.s
Line 2,519: Line 2,518:
CopyFile(tempFile.s,factFile.s)
CopyFile(tempFile.s,factFile.s)
DeleteFile(tempFile.s)
DeleteFile(tempFile.s)
Return
return
</lang>
</lang>