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=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( |
If CreateFile(0,f.s) |
||
WriteStringN( |
WriteStringN(0,"1.2") |
||
WriteStringN( |
WriteStringN(0,"2.1") |
||
CloseFile( |
CloseFile(0) |
||
Else |
Else |
||
Debug "not createfile :"+f.s |
Debug "not createfile :"+f.s |
||
EndIf |
EndIf |
||
showfactorial=# |
showfactorial=#False |
||
If showfactorial |
|||
; cw("nfactorial n ="+str(n)) |
; cw("nfactorial n ="+str(n)) |
||
Debug "nfactorial n ="+ |
Debug "nfactorial n ="+Str(n) |
||
EndIf |
|||
endif |
|||
; build up the factorial combinations |
; build up the factorial combinations |
||
For l=1 To n-2 |
|||
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["+ |
Debug "derangements["+Str(perm(n))+"] for n="+Str(n) |
||
Gosub derangements |
|||
; cw("") |
; cw("") |
||
Debug "" |
Debug "" |
||
; show the first 20 derangements |
; show the first 20 derangements |
||
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["+ |
Debug "derangements["+Str(perm(i))+"] for n="+Str(i) |
||
Next |
|||
next |
|||
end |
|||
End |
|||
derangements: |
derangements: |
||
x=0 |
x=0 |
||
If ReadFile( |
If ReadFile(0,factFile.s) And CreateFile(1,drngFile.s) |
||
Repeat |
|||
repeat |
|||
r.s = ReadString( |
r.s = ReadString(0) |
||
cs=CountString(r.s,".") |
cs=CountString(r.s,".") |
||
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 |
|||
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 |
|||
Next |
|||
next |
|||
t.s= |
t.s=RTrim(t.s,".") |
||
; show only those which are valid |
; show only those which are valid |
||
If Not hit |
|||
x+1 |
x+1 |
||
; cw(t.s+" "+str(x)) |
; cw(t.s+" "+str(x)) |
||
Debug t.s+" "+ |
Debug t.s+" "+Str(x) |
||
WriteStringN( |
WriteStringN(1,t.s+" "+Str(x)) |
||
EndIf |
|||
endif |
|||
EndIf |
|||
endif |
|||
Until Eof(0) |
|||
until eof(2112) |
|||
CloseFile( |
CloseFile(0) |
||
CloseFile( |
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( |
If ReadFile(0,factFile.s) And CreateFile(1,tempFile.s) |
||
Repeat |
|||
repeat |
|||
r.s = ReadString( |
r.s = ReadString(0) |
||
cs=CountString(r.s,".") |
cs=CountString(r.s,".") |
||
If cs |
|||
For j=1 To cs+2 |
|||
t.s="" |
t.s="" |
||
For i=1 To cs+1 |
|||
s.s=StringField(r.s,i,".") |
s.s=StringField(r.s,i,".") |
||
If i=j |
|||
t.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 |
|||
t.s= |
t.s=Trim(t.s,".") |
||
x+1 |
x+1 |
||
If cs+2=n And showfactorial |
|||
; cw(t.s+" "+str(x)) |
; cw(t.s+" "+str(x)) |
||
Debug t.s+" "+ |
Debug t.s+" "+Str(x) |
||
EndIf |
|||
endif |
|||
WriteStringN( |
WriteStringN(1,t.s) |
||
Next |
|||
next |
|||
EndIf |
|||
endif |
|||
Until Eof(0) |
|||
until eof(2112) |
|||
CloseFile( |
CloseFile(0) |
||
CloseFile( |
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> |
||