interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / si / rosetta / tasks / permutations-derangements / permutations-derangements.om

153 lines · 2547 bytes Raw Blame History
1note Rosetta Code task "Permutations/Derangements": https://rosettacode.org/wiki/Permutations/Derangements
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7text tder "derangements of 0 1 2 3:\n"
8text thead "n counted !n\n"
9text tbig "!20 = "
10global perm ulong 10
11note emit writes every byte of a to stdout; a short write is retried, an error exits 1
12fn emit a view uchar * to void
13o = ulong 0
14while lt o len a bound 4096
15k = write 1 a o sub len a o
16if le k 0
17exit 1
18end
19o = add o ulong k
20end
21end
22note emitu writes n in decimal; 20 digits hold every ulong
23fn emitu n ulong to void
24d = new uchar 20 0
25i = ulong 20
26v = n
27while true bound 20
28i = sub i 1
29put d i uchar add rem v 10 48
30v = div v 10
31if eq v 0
32break
33end
34end
35emit part d i sub 20 i
36end
37note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut)
38fn emitw n ulong w ulong to void
39d = new uchar 20 32
40i = ulong 20
41v = n
42while true bound 20
43i = sub i 1
44put d i uchar add rem v 10 48
45v = div v 10
46if eq v 0
47break
48end
49end
50if gt w sub 20 i
51i = sub 20 w
52end
53emit part d i sub 20 i
54end
55note nextp: the next permutation in lexicographic order, false after the last
56fn nextp n ulong to bool
57if lt n 2
58ret false
59end
60i = sub n 1
61while gt i 0 bound 10
62if lt get perm sub i 1 get perm i
63break
64end
65i = sub i 1
66end
67if eq i 0
68ret false
69end
70j = sub n 1
71while le get perm j get perm sub i 1 bound 10
72j = sub j 1
73end
74t = get perm sub i 1
75put perm sub i 1 get perm j
76put perm j t
77k = sub n 1
78while lt i k bound 10
79t2 = get perm i
80put perm i get perm k
81put perm k t2
82i = add i 1
83k = sub k 1
84end
85ret true
86end
87fn deranged n ulong to bool
88for i ulong n
89if eq get perm i i
90ret false
91end
92end
93ret true
94end
95note count walks all n! permutations (at most 9! = 362880) and counts those that fix no point;
96note with show set it also prints them
97fn count n ulong show bool to ulong
98for i ulong n
99put perm i i
100end
101c = ulong 0
102while true bound 362880
103if deranged n
104c = add c 1
105if show
106for i ulong n
107if gt i 0
108emit sp
109end
110emitu get perm i
111end
112emit nl
113end
114end
115if not nextp n
116break
117end
118end
119ret c
120end
121note subfactorial by the recurrence !0 = 1, !1 = 0, !n = (n-1)(!(n-1) + !(n-2)); !20 fits a ulong
122fn subfact n ulong to ulong
123a = ulong 1
124b = ulong 0
125if eq n 0
126ret a
127end
128for x ulong sub n 1
129m = add x 1
130c = mul m add a b
131a = b
132b = c
133end
134ret b
135end
136fn main to int
137emit tder
138z = count 4 true
139emit thead
140for n ulong 10
141emitu n
142emit sp
143emitw count n false 8
144emit sp
145emit sp
146emitu subfact n
147emit nl
148end
149emit tbig
150emitu subfact 20
151emit nl
152ret 0
153end