interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / permutations-derangements.wiki

181 lines · 2949 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text sp " "
8text minus "-"
9text tder "derangements of 0 1 2 3:\n"
10text thead "n counted !n\n"
11text tbig "!20 = "
12global perm ulong 10
13note emit writes every byte of a to stdout; a short write is retried, an error exits 1
14fn emit a view uchar * to void
15o = ulong 0
16while lt o len a bound 4096
17k = write 1 a o sub len a o
18if le k 0
19exit 1
20end
21o = add o ulong k
22end
23end
24note emitu writes n in decimal; 20 digits hold every ulong
25fn emitu n ulong to void
26d = new uchar 20 0
27i = ulong 20
28v = n
29while true bound 20
30i = sub i 1
31put d i uchar add rem v 10 48
32v = div v 10
33if eq v 0
34break
35end
36end
37emit part d i sub 20 i
38end
39note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut)
40fn emitw n ulong w ulong to void
41d = new uchar 20 32
42i = ulong 20
43v = n
44while true bound 20
45i = sub i 1
46put d i uchar add rem v 10 48
47v = div v 10
48if eq v 0
49break
50end
51end
52if gt w sub 20 i
53i = sub 20 w
54end
55emit part d i sub 20 i
56end
57note nextp: the next permutation in lexicographic order, false after the last
58fn nextp n ulong to bool
59if lt n 2
60ret false
61end
62i = sub n 1
63while gt i 0 bound 10
64if lt get perm sub i 1 get perm i
65break
66end
67i = sub i 1
68end
69if eq i 0
70ret false
71end
72j = sub n 1
73while le get perm j get perm sub i 1 bound 10
74j = sub j 1
75end
76t = get perm sub i 1
77put perm sub i 1 get perm j
78put perm j t
79k = sub n 1
80while lt i k bound 10
81t2 = get perm i
82put perm i get perm k
83put perm k t2
84i = add i 1
85k = sub k 1
86end
87ret true
88end
89fn deranged n ulong to bool
90for i ulong n
91if eq get perm i i
92ret false
93end
94end
95ret true
96end
97note count walks all n! permutations (at most 9! = 362880) and counts those that fix no point;
98note with show set it also prints them
99fn count n ulong show bool to ulong
100for i ulong n
101put perm i i
102end
103c = ulong 0
104while true bound 362880
105if deranged n
106c = add c 1
107if show
108for i ulong n
109if gt i 0
110emit sp
111end
112emitu get perm i
113end
114emit nl
115end
116end
117if not nextp n
118break
119end
120end
121ret c
122end
123note subfactorial by the recurrence !0 = 1, !1 = 0, !n = (n-1)(!(n-1) + !(n-2)); !20 fits a ulong
124fn subfact n ulong to ulong
125a = ulong 1
126b = ulong 0
127if eq n 0
128ret a
129end
130for x ulong sub n 1
131m = add x 1
132c = mul m add a b
133a = b
134b = c
135end
136ret b
137end
138fn main to int
139emit tder
140z = count 4 true
141emit thead
142for n ulong 10
143emitu n
144emit sp
145emitw count n false 8
146emit sp
147emit sp
148emitu subfact n
149emit nl
150end
151emit tbig
152emitu subfact 20
153emit nl
154ret 0
155end
156</syntaxhighlight>
157{{out}}
158<pre>
159derangements of 0 1 2 3:
1601 0 3 2
1611 2 3 0
1621 3 0 2
1632 0 3 1
1642 3 0 1
1652 3 1 0
1663 0 1 2
1673 2 0 1
1683 2 1 0
169n counted !n
1700 1 1
1711 0 0
1722 1 1
1733 2 2
1744 9 9
1755 44 44
1766 265 265
1777 1854 1854
1788 14833 14833
1799 133496 133496
180!20 = 895014631192902121
181</pre>