RosettaCodeData/Task/Combinations-and-permutations/M2000-Interpreter/combinations-and-permutatio...

67 lines
1.8 KiB
Plaintext

Module PermComb {
Form 80, 50
perm=lambda (x,y) ->{
def i,z
z=1
For i=x-y+1 to x :z*=i:next i
=z
}
fact=lambda (x) ->{
def i,z
z=1
For i=2 to x :z*=i:next i
=z
}
comb=lambda (x as decimal, y as decimal) ->{
If y>x then {
=0
} else.if x=y then {
=1
} else {
if x-y<y then y=x-y
def decimal i, z=1, ym
ym=y
For i=x to x-y+1
z*=i
z=z/ym
ym-- : if ym<1 then ym=1@
next i
=round(z,0)
}
}
Document Doc$
WriteLn("-- Permutations - from 1 to 12")
For i=1 to 12
l$="" : For j=1 to i : l$+= format$("P({0},{1})={2} ",i, j,perm(i, j)) :next j
Writetext(l$)
next i
WriteLn("-- Combinations from 10 to 60")
For i=10 to 60 step 10
l$="" : For j=1 to i step i div 5 : l$+= format$("C({0},{1})={2} ",i, j,comb(i, j)) :next j
Writetext(l$)
Next i
WriteLn("-- Permutations from 5000 to 15000")
For i=5000 to 15000 step 5000
l$="" : For j=10 to 70 step 20: l$+= format$("P({0},{1})={2} ",i, j,perm(i, j)) :next j
Writetext(l$)
Next i
WriteLn("-- Combinations from 200 to 1000")
For i=200 to 1000 step 200
l$="" : For j=20 to 100 step 20: l$+= format$("C({0},{1})={2} ",i, j,comb(i, j)) :next j
Writetext(l$)
Next i
ClipBoard Doc$
Sub WriteText(a$)
doc$=a$+{
}
Report a$
End Sub
Sub WriteLn(a$)
doc$=a$+{
}
Print a$
End Sub
}
PermComb