RosettaCodeData/Task/Permutations-Derangements/PureBasic/permutations-derangements-1.purebasic

124 lines
2.1 KiB
Text
Raw Permalink Normal View History

2019-09-12 10:33:56 -07:00
Procedure.q Subfactoral(n)
If n=0:ProcedureReturn 1:EndIf
If n=1:ProcedureReturn 0:EndIf
ProcedureReturn (Subfactoral(n-1)+Subfactoral(n-2))*(n-1)
2013-04-10 23:57:08 -07:00
EndProcedure
factFile.s="factorials.txt"
tempFile.s="temp.txt"
drngFile.s="derangements.txt"
DeleteFile(factFile.s)
DeleteFile(tempFile.s)
DeleteFile(drngFile.s)
n=4
; create our storage file
f.s=factFile.s
2019-09-12 10:33:56 -07:00
If CreateFile(0,f.s)
WriteStringN(0,"1.2")
WriteStringN(0,"2.1")
CloseFile(0)
2013-04-10 23:57:08 -07:00
Else
Debug "not createfile :"+f.s
EndIf
2019-09-12 10:33:56 -07:00
showfactorial=#False
2013-04-10 23:57:08 -07:00
2019-09-12 10:33:56 -07:00
If showfactorial
2013-04-10 23:57:08 -07:00
; cw("nfactorial n ="+str(n))
2019-09-12 10:33:56 -07:00
Debug "nfactorial n ="+Str(n)
EndIf
2013-04-10 23:57:08 -07:00
; build up the factorial combinations
2019-09-12 10:33:56 -07:00
For l=1 To n-2
Gosub nfactorial
Next
2013-04-10 23:57:08 -07:00
; extract the derangements
; cw("derangements["+str(perm(n))+"] for n="+str(n))
2019-09-12 10:33:56 -07:00
Debug "derangements["+Str(Subfactoral(n))+"] for n="+Str(n)
Gosub derangements
2013-04-10 23:57:08 -07:00
; cw("")
Debug ""
; show the first 20 derangements
2019-09-12 10:33:56 -07:00
For i=0 To 20
Debug "derangements["+Str(Subfactoral(i))+"] for n="+Str(i)
Next
End
2013-04-10 23:57:08 -07:00
derangements:
x=0
2019-09-12 10:33:56 -07:00
If ReadFile(0,factFile.s) And CreateFile(1,drngFile.s)
Repeat
r.s = ReadString(0)
2013-04-10 23:57:08 -07:00
cs=CountString(r.s,".")
2019-09-12 10:33:56 -07:00
If cs
2013-04-10 23:57:08 -07:00
hit=0
t.s=""
; scan for numbers at their index
2019-09-12 10:33:56 -07:00
For i=1 To cs+1
2013-04-10 23:57:08 -07:00
s.s=StringField(r.s,i,".")
t.s+s.s+"."
2019-09-12 10:33:56 -07:00
If Val(s.s)=i:hit+1:EndIf
Next
t.s=RTrim(t.s,".")
2013-04-10 23:57:08 -07:00
; show only those which are valid
2019-09-12 10:33:56 -07:00
If Not hit
2013-04-10 23:57:08 -07:00
x+1
; cw(t.s+" "+str(x))
2019-09-12 10:33:56 -07:00
Debug t.s+" "+Str(x)
WriteStringN(1,t.s+" "+Str(x))
EndIf
EndIf
Until Eof(0)
CloseFile(0)
CloseFile(1)
2013-04-10 23:57:08 -07:00
Else
Debug "not readfile :"+factFile.s
Debug "not createfile :"+drngFile.s
EndIf
; cw("")
Debug ""
2019-09-12 10:33:56 -07:00
Return
2013-04-10 23:57:08 -07:00
nfactorial:
x=0
2019-09-12 10:33:56 -07:00
If ReadFile(0,factFile.s) And CreateFile(1,tempFile.s)
Repeat
r.s = ReadString(0)
2013-04-10 23:57:08 -07:00
cs=CountString(r.s,".")
2019-09-12 10:33:56 -07:00
If cs
For j=1 To cs+2
2013-04-10 23:57:08 -07:00
t.s=""
2019-09-12 10:33:56 -07:00
For i=1 To cs+1
2013-04-10 23:57:08 -07:00
s.s=StringField(r.s,i,".")
2019-09-12 10:33:56 -07:00
If i=j
t.s+"."+Str(cs+2)+"."+s.s
Else
2013-04-10 23:57:08 -07:00
t.s+"."+s.s
2019-09-12 10:33:56 -07:00
EndIf
Next
If j=cs+2:t.s+"."+Str(cs+2):EndIf
t.s=Trim(t.s,".")
2013-04-10 23:57:08 -07:00
x+1
2019-09-12 10:33:56 -07:00
If cs+2=n And showfactorial
2013-04-10 23:57:08 -07:00
; cw(t.s+" "+str(x))
2019-09-12 10:33:56 -07:00
Debug t.s+" "+Str(x)
EndIf
WriteStringN(1,t.s)
Next
EndIf
Until Eof(0)
CloseFile(0)
CloseFile(1)
2013-04-10 23:57:08 -07:00
Else
Debug "not readfile :"+factFile.s
Debug "not createfile :"+tempFile.s
EndIf
CopyFile(tempFile.s,factFile.s)
DeleteFile(tempFile.s)
2019-09-12 10:33:56 -07:00
Return